Matplotlib Bar Chart

21 Dec
Simple Bar Chart

Simple Bar Chart

I did a post on Pandas and plotting. In that post, Matplotlib was used from Pandas. In this post, I will show a simple bar chart in Matplotlib- sans Pandas.

Here is the code:

import matplotlib.pylab as plt

fig = plt.figure()            #create a figure
ax = fig.add_subplot(1,1,1)    # add plot
x = [2,4,6,8]         #x axis value
y = [4,8,12,16]   #Y axis: height,y, align=’center’, color=’green’) #required,y)
fig.savefig(‘paul2.jpg’)      #This saves a file       This shows it on the screen and gives you the option to save.

That’s it. I like centering my bars, but leave the options out and it will align the left corner with the value of X.  Don’t forget a title – I did. Put fig.suptitle(‘Sample With YERR=2′, fontsize=14, fontweight=’bold’) before adding the subplot. And lastly I like to add the error bars using,y, yerr=2, align=’center’, color=’green’). The result looks like this:

With a Title.

With a Title.

UPDATE: I hate that the bars go to the top of the chart. fix that by setting the Y Limit. Drop this line in your code: p.ylim(2,20)

Y Limit Set: Much Nicer

Y Limit Set: Much Nicer


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: