显示不在python中工作的matplotlib条形图

时间:2018-04-02 09:45:21

标签: python matplotlib

我正在尝试按月创建每年销售额的条形图,我将12个元素的列表传递给该函数,该元素对应于每月的销售额。

我处理它并执行所有相关步骤,但输出与我想要的完全不同。

关于什么错误的想法?非常感谢

Result Of Code enter image description here

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()

My Code

1 个答案:

答案 0 :(得分:0)

这对我有用:

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)