2015-11-12 66 views
0

我正在嘗試使用Pandas matplotlib API在同一個圖上繪製條形圖和線條圖。但是,它不會很好。我正在使用twinx(),這似乎是完成此操作的普遍接受的方式。熊貓與twinx繪圖

請注意,這是在一個Jupyter筆記本中完成的,圖中顯示的是內聯。感謝您的幫助!

fig0, ax0 = matplotlib.pyplot.subplots() 
ax1 = ax0.twinx() 

trend_df_hours.plot(kind='bar', stacked=True, color=color_list, ax=ax0) 
trend_df_qty.plot(kind='line', secondary_y=True, ax=ax1) 
matplotlib.pyplot.show() 
matplotlib.pyplot.close() 

回答

2

什麼似乎是問題?你的代碼看起來工作正常。

%matplotlib inline 
from matplotlib import pyplot as plt 

trend_df_hours = pd.Series(np.random.rand(10)) 
trend_df_qty = pd.Series(np.random.rand(10)) 

fig0, ax0 = plt.subplots() 
ax1 = ax0.twinx() 

trend_df_hours.plot(kind='bar', stacked=True, ax=ax0) 
trend_df_qty.plot(kind='line', secondary_y=True, ax=ax1) 
plt.show() 
plt.close() 

enter image description here

+0

我發現了這個問題。我正在使用日期索引(對於這兩個數據集),並且似乎並不那樣。我可以將數據集合並在一起,但是我認爲沒有一種好的方法可以提取一列並將其與其他列進行區分。可以使用相同的繪圖風格來完成:http://pandas.pydata.org/pandas-docs/stable/visualization.html#plotting-on-a-secondary-y-axis – um8ra