Skip to content

Commit 05abe1f

Browse files
charlesdong1991rhshadrach
authored andcommitted
PLT: Order of plots does not preserve the column orders in df.hist (pandas-dev#33336)
1 parent 426fcb0 commit 05abe1f

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ Plotting
703703
- :func:`.plot` for line/bar now accepts color by dictonary (:issue:`8193`).
704704
- Bug in :meth:`DataFrame.plot.hist` where weights are not working for multiple columns (:issue:`33173`)
705705
- Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`)
706+
- Bug in :meth:`DataFrame.hist` where the order of ``column`` argument was ignored (:issue:`29235`)
706707
- Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`)
707708

708709

pandas/plotting/_matplotlib/hist.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass
55
from pandas.core.dtypes.missing import isna, remove_na_arraylike
66

7-
import pandas.core.common as com
8-
97
from pandas.io.formats.printing import pprint_thing
108
from pandas.plotting._matplotlib.core import LinePlot, MPLPlot
119
from pandas.plotting._matplotlib.tools import _flatten, _set_ticks_props, _subplots
@@ -423,8 +421,8 @@ def hist_frame(
423421
_axes = _flatten(axes)
424422

425423
can_set_label = "label" not in kwds
426-
427-
for i, col in enumerate(com.try_sort(data.columns)):
424+
425+
for i, col in enumerate(data.columns):
428426
ax = _axes[i]
429427
if legend and can_set_label:
430428
kwds["label"] = col

pandas/tests/plotting/test_hist_method.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,30 @@ def test_hist_subplot_xrot(self):
269269
)
270270
self._check_ticks_props(axes, xrot=0)
271271

272+
@pytest.mark.parametrize(
273+
"column, expected",
274+
[
275+
(None, ["width", "length", "height"]),
276+
(["length", "width", "height"], ["length", "width", "height"]),
277+
],
278+
)
279+
def test_hist_column_order_unchanged(self, column, expected):
280+
# GH29235
281+
282+
df = DataFrame(
283+
{
284+
"width": [0.7, 0.2, 0.15, 0.2, 1.1],
285+
"length": [1.5, 0.5, 1.2, 0.9, 3],
286+
"height": [3, 0.5, 3.4, 2, 1],
287+
},
288+
index=["pig", "rabbit", "duck", "chicken", "horse"],
289+
)
290+
291+
axes = _check_plot_works(df.hist, column=column, layout=(1, 3))
292+
result = [axes[0, i].get_title() for i in range(3)]
293+
294+
assert result == expected
295+
272296
@pytest.mark.slow
273297
@pytest.mark.parametrize("column", [None, "B"])
274298
@pytest.mark.parametrize("label", [None, "D"])

0 commit comments

Comments
 (0)