Repository: pandas-dev/pandas
Pull request: PLT: Order of plots does not preserve the column orders in df.hist
- [x] closes #29235
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Problem statement:
Categories reordered to alphabetical when plotting histograms
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd

import matplotlib.pyplot as plt

df = pd.DataFrame({
    'width': [0.7, 0.2, 0.15, 0.2, 1.1],
    'length': [1.5, 0.5, 1.2, 0.9, 3]
    }, index= ['pig', 'rabbit', 'duck', 'chicken', 'horse'])

# Whether or not `column` is passed the order is still reordered to alphabetical.
df.hist(column=['width','length'])
plt.show()
```
#### Problem description

When plotting histograms from a DataFrame (see code-snippet above) the order of subplots is not maintained. Passing `column` does not appear to have any effect on the ordering either. 


#### Expected Output

Plots/subplots order equals column order in DataFrame.

#### Output of ``pd.show_versions()``

```
INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.24.2
pytest: 5.2.1
pip: 19.1.1
setuptools: 41.0.1
Cython: None
numpy: 1.16.4
scipy: 1.2.2
pyarrow: None
xarray: 0.12.1
IPython: 7.5.0
sphinx: None
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: 4.7.1
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
```

Hints from issue discussion:

Commit message: Merge remote-tracking branch 'upstream/master' into fix_issue_29235

Patch to review:
diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst
index 82c43811c0444..dab0bebe5d388 100644
--- a/doc/source/whatsnew/v1.1.0.rst
+++ b/doc/source/whatsnew/v1.1.0.rst
@@ -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`)
 - Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`)
 
 
diff --git a/pandas/plotting/_matplotlib/hist.py b/pandas/plotting/_matplotlib/hist.py
index 3a0cdc90dfd5c..b0ce43dc2eb36 100644
--- a/pandas/plotting/_matplotlib/hist.py
+++ b/pandas/plotting/_matplotlib/hist.py
@@ -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
@@ -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)
diff --git a/pandas/tests/plotting/test_hist_method.py b/pandas/tests/plotting/test_hist_method.py
index fba4f07f6cc0f..5a30e9fbb91c6 100644
--- a/pandas/tests/plotting/test_hist_method.py
+++ b/pandas/tests/plotting/test_hist_method.py
@@ -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):


Code review comments:
diff_hunk: @@ -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`)
path: doc/source/whatsnew/v1.1.0.rst
text: @user1:
```suggestion
- Bug in :meth:`DataFrame.hist` where the order of the ``column`` argument was ignored (:issue:`29235`)
```