Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MatPlot bar function arguments

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 the error if the height is a list.

like image 342
Kartik Avatar asked Mar 05 '11 23:03

Kartik


2 Answers

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()
like image 120
rlibby Avatar answered Sep 21 '22 18:09

rlibby


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()
like image 21
ssoler Avatar answered Sep 20 '22 18:09

ssoler