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.
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. 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()
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.
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() # Here we're adding 2 subplots. The grid is set # up as one row, two columns. 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()
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.
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 manually. ind = range(N) # I'm also supplying (fake) error for each y value. # You can use numpy's standard deviation, numpy.std(data) # to calculate error bars for your own data. # Standard error is standard deviation over sqrt(N), # so you could use numpy.std(x)/numpy.sqrt(len(x)) # to get standard error for the data in a list x. err = [1.2, 1.5, 2.5, 1.2, 2.0] # See note below on the breakdown of this 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()
Note: Here’s the breakdown of the command used above,
ax.bar(ind, y, facecolor='#777777', align='center', yerr=err, ecolor='black')
indcontains the x values to plot
ycontains the y values to plot
#777777is the hexadecimal color code for a dark gray
aligntells the bar command to align the bars on the
centerof the x values we give it, instead of aligning the left side of the bar (which is the default)
yerrspecifies the error bars in the y direction.
ecoloris 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.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</code>