2
努力想出這一個。我有一個堆疊的條形圖,我試圖用Python/Matplotlib創建,它似乎覆蓋了數據而不是堆疊,產生不同的顏色(即紅色+黃色=橙色,而不是堆疊紅色和黃色)。任何人都可以看到我做錯了什麼?Matplotlib中的堆積條形圖;系列重疊,而不是堆疊
這裏是我的代碼:
#Stacked Bar Char- matplotlib
#Create the general blog and the "subplots" i.e. the bars
f, ax1 = plt.subplots(1, figsize=(10,5))
# Set the bar width
bar_width = 0.75
# positions of the left bar-boundaries
bar_l = [i+1 for i in range(len(df4['bikes']))]
# positions of the x-axis ticks (center of the bars as bar labels)
tick_pos = [i+(bar_width/2) for i in bar_l]
# Create a bar plot, in position bar_1
ax1.bar(bar_l, df4['bikes1'], width=bar_width, label='bikes', alpha=0.5,color='r', align='center')
# Create a bar plot, in position bar_1
ax1.bar(bar_l, df4['bikes'], width=bar_width,alpha=0.5,color='r', align='center')
# Create a bar plot, in position bar_1
ax1.bar(bar_l, df4['cats1'], width=bar_width, label='cats', alpha=0.5,color='y', align='center')
# Create a bar plot, in position bar_1
ax1.bar(bar_l, df4['cats'], width=bar_width,alpha=0.5,color='y', align='center')
# set the x ticks with names
plt.xticks(tick_pos, df4['Year'])
# Set the label and legends
ax1.set_ylabel("Count")
ax1.set_xlabel("Year")
plt.legend(loc='upper left')
ax1.axhline(y=0, color='k')
ax1.axvline(x=0, color='k')
# Set a buffer around the edge
plt.xlim([min(tick_pos)-bar_width, max(tick_pos)+bar_width])
plt.show()
多謝您的幫助! – NYCTed