Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在matlib中制作条形图_Python_Graph - Fatal编程技术网

Python 在matlib中制作条形图

Python 在matlib中制作条形图,python,graph,Python,Graph,这是我第一次使用matlib,我们希望基于给定的数据集创建一个条形图。我已经为此工作了大约一个小时,我想知道是否有人能为我指明正确的方向 def plotBarChart(u, p, g): bar1 = pyplot.bar(u[0], u[1], width=1, color='blue', align='center') bar2 = pyplot.bar(u[0], u[2], width=1, color='red', align='center

这是我第一次使用matlib,我们希望基于给定的数据集创建一个条形图。我已经为此工作了大约一个小时,我想知道是否有人能为我指明正确的方向

    def plotBarChart(u, p, g):
        bar1 = pyplot.bar(u[0], u[1], width=1, color='blue', align='center')
        bar2 = pyplot.bar(u[0], u[2], width=1, color='red', align='center')
        bar3 = pyplot.bar(u[0], u[3], width=1, color='green',     align='center')

        bar4 = pyplot.bar(p[0], p[1], width=1, color='blue', align='center')
        bar5 = pyplot.bar(p[0], p[2], width=1, color='red', align='center')
        bar6 = pyplot.bar(p[0], p[3], width=1, color='green', align='center')

        bar7 = pyplot.bar(g[0], g[1], width=1, color='blue', align='center')
        bar8 = pyplot.bar(g[0], g[2], width=1, color='red', align='center')
        bar9 = pyplot.bar(g[0], g[3], width=1, color='green', align='center')

        pyplot.legend((bar1, bar2, bar3, bar4, bar5, bar6, bar7, bar8, bar9), ('2012', '2013', '2014'), loc=1)
        pyplot.xticks(pos3, names)

        pyplot.ylabel('Amount of Students Enrolled')
        pyplot.setp(pyplot.xticks()[1], rotation=15)
        pyplot.axis([u[0], g[0], 0, 35000])
        pyplot.show()

    def main():
        u = ['Undergraduate', 30147, 29440, 29255]
        p = ['Professional', 946, 941, 947]
        g = ['Graduate', 8163, 8407, 8568]
        plotBarChart(u, p, g)
    main()

程序应该运行main并获取给定的数据,然后根据所述数据创建条形图。我知道条形图也可以通过循环来完成,但如果不需要的话,我不想太多地涉及到这一点。他们给了我们一个样本输出图像,其中有三组三个条,一组标记为本科,由u数据组成,一组标记为专业,由p数据组成,另一组标记为毕业生,由g数据组成。y轴从0-35000延伸,x轴是“本科、专业、研究生”,我只需要知道我哪里出了问题,因为它不断地给我一个错误,即杆的高度必须小于13或更高。

这应该行得通。它使用numpy中的数组函数,而不是for循环

import numpy as np
import matplotlib.pyplot as plt

def plotBarChart(u, p, g):

    # set bar index array and select a width for each bar
    bar_index = np.array([1, 2, 3])
    bar_width = 0.25

    # plot all bars for each index
    bars1 = plt.bar(bar_index, u, bar_width, color = 'b', label = 'Undergrad')
    bars2 = plt.bar(bar_index + bar_width, p, bar_width, color = 'r', label = 'Professional')
    bars3 = plt.bar(bar_index + 2 * bar_width, g, bar_width, color = 'g', label = 'Grad')

    # set labels and display plot
    plt.title('')
    plt.xlabel('')
    plt.ylabel('')
    plt.xticks(bar_index + 1.5 * bar_width, ['2012', '2013', '2014'])
    plt.legend()
    plt.show()

def main():
    # plot data
    undergrad = [30147, 29440, 29255]
    prof = [946, 941, 947]
    grad = [8163, 8407, 8568]
    plotBarChart(undergrad, prof, grad)

main()
希望这有帮助