Skip to content

Commit 927ff7a

Browse files
PLT: Order of plots does not preserve the column orders in df.hist (#33336)
1 parent d9ecf53 commit 927ff7a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
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: 1 addition & 3 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
@@ -403,7 +401,7 @@ def hist_frame(
403401
)
404402
_axes = _flatten(axes)
405403

406-
for i, col in enumerate(com.try_sort(data.columns)):
404+
for i, col in enumerate(data.columns):
407405
ax = _axes[i]
408406
ax.hist(data[col].dropna().values, bins=bins, **kwds)
409407
ax.set_title(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

273297
@td.skip_if_no_mpl
274298
class TestDataFrameGroupByPlots(TestPlotBase):

0 commit comments

Comments
 (0)