簡體   English   中英

Python Matplotlib繪制堆積條形圖

[英]Python Matplotlib Plotting Stacked Bar Chart

我正在嘗試從具有兩個系列的數據框中繪制堆積的條形圖。 第二個系列的數據非常少,但我希望它在圖表中仍顯示為一條栗色的細線。 當我將圖表另存為png文件時,前兩個條形圖不顯示第二個“驅動程序”數據集。 但是,隨后的所有條都將其顯示。 以下是我正在使用的代碼以及圖表的快照。 您可以看到“驅動程序”系列未在前兩個欄中顯示嗎? 我該如何補救?

碼:

df_trend = pd.read_csv('test_log.csv', index_col=0, skiprows=0)
df_trend.index = (pd.to_datetime(df_trend.index)).strftime("%m/%d %H:00")

fig = plt.figure()
rcParams['figure.figsize'] = df_trend.shape[0], 4
ax = fig.add_subplot(111)
y = df_trend.tail(24).plot.bar(stacked=True, color=['skyblue', 'maroon'], edgecolor="none", width=0.2)
y.legend(loc="lower left", fontsize=9)
plt.tick_params(axis='both', which='both', labelsize=9)
fig.autofmt_xdate()

plt.title('Cars vs Drivers', fontsize=10, y=1.05)
plt.savefig('cars_drivers.png', bbox_inches='tight')
plt.close()

使用的DataFrame:

               Cars  Drivers
09/27 09:00  243000      300
09/28 09:00  243970      190
09/28 13:00  267900      290
09/28 17:00  254770      180
09/28 18:00  250860      290

圖表: 繪圖圖輸出

我認為您可以做的事情,但是我想做的#2:

  1. 不要使用堆積的條形圖(這是我的首選)。
  2. 增加數字的DPI: plt.savefig('cars_drivers.png', bbox_inches='tight',dpi=1000)
  3. 更改ylim范圍: plt.ylim([200e3, 300e3])
  4. 更改驅動程序的比例並重命名以在圖例中顯示的列:

    df_trend['Drivers'] = 10*df_trend['Drivers']

    df_trend = df_trend.rename(columns={'Drivers': 'Drivers x 10'})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM