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.

5/1/2012 6:01:25 PM

Accepted Answer

A simple thing you can do:, 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
>>>, range(20, 10, -1))
3/6/2011 12:07:34 AM

From the documentation

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

A simple example from

# 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.,y)

# after you're all done with plotting commands, show the plot.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow