0

I have a pandas dataframe with multiple columns like following:

columns_all = pd.DataFrame({'m1_h':m1_hist, 'm2_h':m2_hist, ....... 'm6_h':m6_hist, 'm6_f':m6_futu})

and I use following to plot histograms based on each column but columns are sorted But I like to have all histograms in same order as columns written in above dataframe:

columns_all.hist(layout=(2,6), sharey=True, sharex=True)
plt.ylim(0, 100)
plt.xlim(0, 150)
plt.show()

Appreciate any suggestion to maintain order of columns while plotting.

2 Answers 2

1

According to the source code, the sorting is defined by _try_sort(data.columns) and cannot be changed by an argument. You can do what Claudiu Creanga suggested. However, in my test, that won't give you a (2, 6) layout. If you really want that layout and what pandas.DataFrame.hist does, the following code may be helpful:

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

columns_all = pd.DataFrame([np.random.randn(1000)] * 7).T
columns_all.columns = ['m1_h', 'm2_h', 'm3_h', 'm4_h', 'm5_h', 'm6_h', 'm6_f']
plt.clf()
fig = plt.figure(figsize=(16, 4))
axarr = []
for i, col in enumerate(columns_all.columns):
    if i // 6 > 0:
        sharex = axarr[i % 6]
        plt.setp(axarr[i % 6].get_xticklabels(), visible=False)
    else:
        sharex = None
    if i % 6 > 0:
        sharey = axarr[i // 6]
    else:
        sharey = None
    ax = fig.add_subplot(2, 6, i + 1, sharex=sharex, sharey=sharey)
    axarr.append(ax)
    if i % 6 > 0:
        plt.setp(ax.get_yticklabels(), visible=False)
    ax.hist(columns_all[col].dropna().values)
    ax.set_title(col)
    ax.grid(True)
fig.subplots_adjust(wspace=0.3, hspace=0.3)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You could make repeated calls to individual columns, as both on the creation of the dataframe and on the .hist() there is a reordering done automatically:

s = pd.DataFrame([{'B': 1.5, 'A':3, 'C': 4, 'D':2}])
s

    A   B   C   D
0   3   1.5 4   2

s = s[["B", "A", "C", "D"]] #chose your order
s

    B   A   C   D
0   1.5 3   4   2

for x in s.columns:
    s[[x]].hist(layout=(2,6), sharey=True, sharex=True)
plt.ylim(0, 100)
plt.xlim(0, 150)
plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.