[英]Displaying a matplotlib bar chart not working in python
我正在嘗試為每年的銷售額創建條形圖,我將對應於每月銷售額的12個元素的列表傳遞給該函數。
我對其進行處理並執行所有相關步驟,但輸出卻不像我想要的那樣。
有什么問題的想法嗎? 非常感謝
def BarChart(self,values):
#Data
y1 = [values[0]]
y2 = [values[1]]
y3 = [values[2]]
y4 = [values[3]]
y5 = [values[4]]
y6 = [values[5]]
y7 = [values[6]]
y8 = [values[7]]
y9 = [values[8]]
y10 = [values[9]]
y11 = [values[10]]
y12= [values[11]]
x = np.arange(len(y1))
# Plot data
# use zorder to put bares in front of grid.
bar_width = 10
plt.bar(x,y1,width=bar_width,color='red',zorder=2)
plt.bar(x+bar_width,y2,width=bar_width,color='blue',zorder=2)
plt.bar(x+bar_width,y3,width=bar_width,color='green',zorder=2)
plt.bar(x+bar_width,y4,width=bar_width,color='orange',zorder=2)
plt.bar(x+bar_width,y5,width=bar_width,color='purple',zorder=2)
plt.bar(x+bar_width,y6,width=bar_width,color='yellow',zorder=2)
plt.bar(x+bar_width,y7,width=bar_width,color='white',zorder=2)
plt.bar(x+bar_width,y8,width=bar_width,color='pink',zorder=2)
plt.bar(x+bar_width,y9,width=bar_width,color='gold',zorder=2)
plt.bar(x+bar_width,y10,width=bar_width,color='black',zorder=2)
plt.bar(x+bar_width,y11,width=bar_width,color='silver',zorder=2)
plt.bar(x+bar_width,y1,width=bar_width,color='grey',zorder=2)
#Labels
#Adjust x until its in center.
plt.xticks(x+bar_width*2,["January","Feburary","March","April","May","June","July","August","September","October","November","December"])
plt.title('MOT Analysis Chart')
plt.xlabel('Months')
plt.ylabel('Amount Of MOTs')
red = mpatches.Patch(color='red',label="January")
blue = mpatches.Patch(color='blue',label="Feburary")
green = mpatches.Patch(color='green',label="March")
orange = mpatches.Patch(color='orange',label="April")
purple = mpatches.Patch(color='purple',label="May")
yellow = mpatches.Patch(color='yellow',label="June")
white = mpatches.Patch(color='white',label="July")
pink = mpatches.Patch(color='pink',label="August")
gold = mpatches.Patch(color='gold',label="September")
black = mpatches.Patch(color='black',label="October")
silver = mpatches.Patch(color='silver',label="November")
grey = mpatches.Patch(color='grey',label="December")
plt.legend(handles=[red,blue,green,orange,purple,yellow,white,pink,gold,black,silver,grey])
plt.grid(axis='y')
plt.show()
這對我有用:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
def BarChart(values):
bar_width = 10
x = np.arange(len(values))*bar_width
colors = ("red", "blue", "green", "orange", "purple", "yellow", "white", "pink", "gold", "black", "silver", "grey")
months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
handles = [mpatches.Patch(color=c,label=l) for c, l in zip(colors, months)]
# Plot data
# use zorder to put bares in front of grid.
for i, y in enumerate(values):
plt.bar(x[i], y, width=bar_width, color=colors[i], zorder=2)
#Labels
#Adjust x until its in center.
plt.xticks(x, months, rotation=90)
plt.title('MOT Analysis Chart')
plt.xlabel('Months')
plt.ylabel('Amount Of MOTs')
plt.legend(handles=handles, ncol=2)
plt.grid(axis='y')
plt.tight_layout()
plt.show()
y = np.arange(12) + 1
BarChart(y)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.