Skip to content

PLT: Order of plots does not preserve the column orders in df.hist #33336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ Plotting
- :func:`.plot` for line/bar now accepts color by dictonary (:issue:`8193`).
- Bug in :meth:`DataFrame.plot.hist` where weights are not working for multiple columns (:issue:`33173`)
- Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`)
- Bug in :meth:`DataFrame.hist` where the column is sorted given the ``column`` is assigned or not. (:issue:`29235`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Bug in :meth:`DataFrame.hist` where the column is sorted given the ``column`` is assigned or not. (:issue:`29235`)
- Bug in :meth:`DataFrame.hist` where the order of the ``column`` argument was ignored (:issue:`29235`)

- Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`)


Expand Down
4 changes: 1 addition & 3 deletions pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass
from pandas.core.dtypes.missing import isna, remove_na_arraylike

import pandas.core.common as com

from pandas.io.formats.printing import pprint_thing
from pandas.plotting._matplotlib.core import LinePlot, MPLPlot
from pandas.plotting._matplotlib.tools import _flatten, _set_ticks_props, _subplots
Expand Down Expand Up @@ -403,7 +401,7 @@ def hist_frame(
)
_axes = _flatten(axes)

for i, col in enumerate(com.try_sort(data.columns)):
for i, col in enumerate(data.columns):
ax = _axes[i]
ax.hist(data[col].dropna().values, bins=bins, **kwds)
ax.set_title(col)
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/plotting/test_hist_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,30 @@ def test_hist_subplot_xrot(self):
)
self._check_ticks_props(axes, xrot=0)

@pytest.mark.parametrize(
"column, expected",
[
(None, ["width", "length", "height"]),
(["length", "width", "height"], ["length", "width", "height"]),
],
)
def test_hist_column_order_unchanged(self, column, expected):
# GH29235

df = DataFrame(
{
"width": [0.7, 0.2, 0.15, 0.2, 1.1],
"length": [1.5, 0.5, 1.2, 0.9, 3],
"height": [3, 0.5, 3.4, 2, 1],
},
index=["pig", "rabbit", "duck", "chicken", "horse"],
)

axes = _check_plot_works(df.hist, column=column, layout=(1, 3))
result = [axes[0, i].get_title() for i in range(3)]

assert result == expected


@td.skip_if_no_mpl
class TestDataFrameGroupByPlots(TestPlotBase):
Expand Down