2017-06-15 90 views
0

分類我有多個列的熊貓數據幀像以下:熊貓據幀列,同時繪製

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

我用下面的繪製根據各列直方圖,但列進行排序,但我喜歡所有直方圖中與上面數據框中寫入的列順序相同:

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

欣賞任何建議,以在繪圖時保持列的順序。

回答

1

the source code,排序定義通過_try_sort(data.columns)並且不能被參數改變。你可以做什麼Claudiu Creanga suggested。但是,在我的測試中,這不會給你一個(2, 6)佈局。如果你真的想要的佈局和什麼pandas.DataFrame.hist呢,下面的代碼可能會有所幫助:

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

+0

它的工作需要 – Ibe

1

你可以把在創建數據框上和在.hist重複各列的呼叫,既()有一個重新排序自動完成:

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()