我正在尝试使用matplot库创建一个条形图,但我无法弄清楚该函数的参数是什么。
文档说bar(left, height)
,但我不知道如何在这里输入我的数据[这是一个名为x的数字列表]。
它告诉我,当我将其作为数字0.5
或1
时,高度应该是标量,如果高度是列表,则不会显示错误。
答案 0 :(得分:4)
你可以做一件简单的事情:
plt.bar(range(len(x)), x)
left
是酒吧的左端。你告诉它把横条轴放在哪里。这是你可以玩的东西,直到你得到它:
>>> import matplotlib.pyplot as plt
>>> plt.bar(range(10), range(20, 10, -1))
>>> plt.show()
答案 1 :(得分:2)
来自文档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()