[英]Displaying a matplotlib bar chart not working in python
I am trying to create a bar chart for the amount of sales per year by month, I pass into the function a list of 12 elements corresponding to the amount of sales per month. 我正在尝试为每年的销售额创建条形图,我将对应于每月销售额的12个元素的列表传递给该函数。
I process it and do all the relevant steps but the output is nothing like I want it to be. 我对其进行处理并执行所有相关步骤,但输出却不像我想要的那样。
Any ideas on whats wrong? 有什么问题的想法吗? Many Thanks
非常感谢
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()
This works for me: 这对我有用:
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.