Creating a custom bar plot in matplotlib

Bar plots are one of the simplest kinds of plots, but for some reason in many programs it’s difficult to get the labels you want, correct error bars, and control over every aspect of the plot. Here’s how to create a custom bar plot using the Python plotting module matplotlib.

Fire up IPython and matplotlib.

plain ol’ bar plot



# pylab contains matplotlib plus other goodies.
import pylab as p

#make a new figure
fig = p.figure()

# make a new axis on that figure.
# see note 1
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.
show()

Here’s what it looks like:
simple-barplot

Note 1: the “111″ refers to the number of subplots you want to add. The first number is rows of subplots, the second number is columns of subplots. In this case we’re only making a single subplot, so it’s one row and one column. The last number is which subplot (out of the ones you just created) to use now. Since we made only one, the choice is easy. See the other scripts below for more.

This is just the basic bar plot. Not that great for publication . . . it needs better colors, some labels, and maybe some error bars. The scripts below walk you through doing just that.

barplot-2.py

Now let’s tweak it out a little . . . this time we’ll make two subplots, one with the same plotting commands as above, and the other subplot with red, thinner bars.


import pylab as p
fig = p.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

x = [1,2,3]
y = [4,6,3]

# first axis, with the same bar plot as before.
ax1.bar(x,y)

# on the second axis, make the width smaller (default is 0.8)
ax2.bar(x,y,width=0.3, facecolor='red')
ax2.set_ylabel('number of widgets')

p.show()

And the result:
barplot-2

barplot-3.py

OK, time to make it look nicer. This code may look like it’s long, but it’s mostly a bunch of comments explaining what everything does.

Added 1/28/07:
By the way, here are my notes for using errorbars in general with matplotlib.

import pylab as p
fig = p.figure()
ax = fig.add_subplot(1,1,1)
# note the change: I'm only supplying y data.
y = [4, 6, 7, 3, 5]

# Calculate how many bars there will be
N = len(y)

# Generate a list of numbers, from 0 to N
# This will serve as the (arbitrary) x-axis, which
# we will then re-label.
ind = range(N)

# I'm also supplying (fake) error for each y value, see note 2
err = [1.2, 1.5, 2.5, 1.2, 2.0]

# see note 3 for details on this plotting command.
ax.bar(ind, y, facecolor='#777777', align='center', yerr=err, ecolor='black')

#Create a y label
ax.set_ylabel('Counts')

# Create a title, in italics
ax.set_title('Counts, by group',fontstyle='italic')

# This sets the ticks on the x axis to be exactly where we put
# the center of the bars.
ax.set_xticks(ind)

# Labels for the ticks on the x axis.  It needs to be the same length
# as y (one label for each bar)
group_labels = ['control', 'cold treatment',
                     'hot treatment', 'another treatment',
                     'the last one']

# Set the x tick labels to the group_labels defined above.
ax.set_xticklabels(group_labels)

# Extremely nice function to auto-rotate the x axis labels.
# It was made for dates (hence the name) but it works
# for any long x tick labels
fig.autofmt_xdate()

p.show()

Here’s what the result looks like:
barplot-3

Note 2: You can use pylab’s standard deviation, std to calculate error bars for your own data (p.std(x) if x is your data and you imported pylab as p). Standard error is standard deviation over sqrt(N), so you could use p.std(x)/p.sqrt(len(x)) to get standard error for the data in x.

Note 3: Here’s the breakdown of the command ax.bar(ind, y, facecolor='#777777', align='center', yerr=err, ecolor='black'):

  • ind contains the x values to plot
  • y contains the y values to plot
  • the #777777 is the hexadecimal color code for a dark gray
  • align tells the bar command to align the bars on the center of the x values we give it, instead of aligning the left side of the bar (which is the default)
  • yerr specifies the error bars in the y direction.
  • ecolor is the color of the error bars

By the way, here’s the un-commented, more compact version of the same exact code:

import pylab as p
fig = p.figure()
ax = fig.add_subplot(1,1,1)
y = [4, 6, 7, 3, 5]
N = len(y)
ind = range(N)
err = [1.2, 1.5, 2.5, 1.2, 2.0]
ax.bar(ind, y, facecolor='#777777', align='center', yerr=err, ecolor='black')
ax.set_ylabel('Counts')
ax.set_title('Counts, by group',fontstyle='italic')
ax.set_xticks(ind)
group_labels = ['control', 'cold treatment',
                     'hot treatment', 'another treatment',
                     'the last one']
ax.set_xticklabels(group_labels)
fig.autofmt_xdate()
p.show()

So what else can you tweak?

Just about everything. Below is the help entry for the bar function, which you can view in IPython by typing p.ax.bar? or p.bar?. Note, for example, that you can change things like the transparency of the bars, give each bar different widths, change the edge colors, change the size of the ‘caps’ on the error bars, and so on.

 BAR(left, height, width=0.8, bottom=0,
        color=None, edgecolor=None, linewidth=None,
        yerr=None, xerr=None, ecolor=None, capsize=3,
        align='edge', orientation='vertical', log=False)
    Make a bar plot with rectangles bounded by
      left, left+width, bottom, bottom+height
            (left, right, bottom and top edges)
    left, height, width, and bottom can be either scalars or sequences
    Return value is a list of Rectangle patch instances
        left - the x coordinates of the left sides of the bars
        height - the heights of the bars
    Optional arguments:
        width - the widths of the bars
        bottom - the y coordinates of the bottom edges of the bars
        color - the colors of the bars
        edgecolor - the colors of the bar edges
        linewidth - width of bar edges; None means use default
            linewidth; 0 means don't draw edges.
        xerr and yerr, if not None, will be used to generate errorbars
        on the bar chart
        ecolor specifies the color of any errorbar
        capsize (default 3) determines the length in points of the error
        bar caps
        align = 'edge' (default) | 'center'
        orientation = 'vertical' | 'horizontal'
        log = False | True - False (default) leaves the orientation
                axis as-is; True sets it to log scale
    For vertical bars, align='edge' aligns bars by their left edges in
    left, while 'center' interprets these values as the x coordinates of
    the bar centers. For horizontal bars, 'edge' aligns bars by their
    bottom edges in bottom, while 'center' interprets these values as the
    y coordinates of the bar centers.
    The optional arguments color, edgecolor, linewidth, xerr, and yerr can
    be either scalars or sequences of length equal to the number of bars.
    This enables you to use bar as the basis for stacked bar charts, or
    candlestick plots.
    Optional kwargs:
            alpha: float
            animated: [True | False]
            antialiased or aa: [True | False]
            axes: an axes instance
            clip_box: a matplotlib.transform.Bbox instance
            clip_on: [True | False]
            clip_path: an agg.path_storage instance
            edgecolor or ec: any matplotlib color
            facecolor or fc: any matplotlib color
            figure: a matplotlib.figure.Figure instance
            fill: [True | False]
            hatch: unknown
            label: any string
            linewidth or lw: float
            lod: [True | False]
            picker: [None|float|boolean|callable]
            transform: a matplotlib.transform transformation instance
            visible: [True | False]
            zorder: any number

    Addition kwargs: hold = [True|False] overrides default hold state

Tags: , , , , ,

Leave a Reply