【问题标题】:Python MatPlot bar function argumentsPython MatPlot 条形函数参数
【发布时间】:2011-07-09 14:56:42
【问题描述】:

我正在尝试使用 matplot 库创建条形图,但我不知道该函数的参数是什么。

文档说bar(left, height),但我不知道如何在此处输入我的数据[这是一个名为 x 的数字列表]。

当我将高度作为数字0.5 或1 时,它告诉我高度应该是一个标量,如果高度是一个列表,它不会显示错误。

【问题讨论】:

    标签: python graph matplotlib


    【解决方案1】:

    来自文档http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.bar

    bar(left, height, width=0.8, bottom=0, **kwargs)
    

    地点:

    Argument   Description
    left   --> the x coordinates of the left sides of the bars
    height --> the heights of the bars
    

    来自http://scienceoss.com/bar-plot-with-custom-axis-labels/的一个简单例子

    # pylab contains matplotlib plus other goodies.
    import pylab as p
    
    #make a new figure
    fig = p.figure()
    
    # make a new axis on that figure. Syntax for add_subplot() is
    # number of rows of subplots, number of columns, and the
    # which subplot. So this says one row, one column, first
    # subplot -- the simplest setup you can get.
    # See later examples for more.
    
    ax = fig.add_subplot(1,1,1)
    
    # your data here:     
    x = [1,2,3]
    y = [4,6,3]
    
    # add a bar plot to the axis, ax.
    ax.bar(x,y)
    
    # after you're all done with plotting commands, show the plot.
    p.show()
    

    【讨论】:

      【解决方案2】:

      你可以做的一件简单的事:

      plt.bar(range(len(x)), x)
      

      left 是条的左端。您正在告诉它在水平轴上放置条形的位置。在你得到它之前,你可以尝试以下方法:

      >>> import matplotlib.pyplot as plt
      >>> plt.bar(range(10), range(20, 10, -1))
      >>> plt.show()
      

      【讨论】:

        猜你喜欢
        • 2019-04-12
        • 1970-01-01
        • 2010-12-02
        • 2021-02-26
        • 2017-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多