2011-03-05 45 views
6

我想创建一个使用matplot库的条形图,但我不知道该函数的参数是什么。Python MatPlot栏函数参数

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

它告诉我,高度应该是一个标量,当我把它作为数字0.51,并且如果高度是列表不会显示错误。

回答

4

一个简单的事情可以做:

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

left是酒吧的左端。你要告诉它将横条放置在哪里。这里的东西,你可以玩弄,直到你得到它:

>>> import matplotlib.pyplot as plt 
>>> plt.bar(range(10), range(20, 10, -1)) 
>>> plt.show() 
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()