开发者

Python MatPlot bar function arguments

开发者 https://www.devze.com 2023-02-15 05:26 出处:网络
I am trying to create a bar graph using the matplot library but I cant figure out what the arguments are for the function.

I am trying to create a bar graph using the matplot library but I cant figure out what the arguments are for the function.

The documentation says bar(left, height), but I don't know how to put in my data [which is a list of numbers called x] in here.

It tells me that the height should be a scalar when I put it as a number 0.5 or 1, and doesn't show me开发者_Python百科 the error if the height is a list.


A simple thing you can do:

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

left is the left ends of the bars. You're telling it where to place the bars on the horizontal axis. Here's something you can play around with until you get it:

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


From the documentation http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.bar

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

where:

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

A simple example from 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()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号