2011-03-05 66 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()