Skip to content

Instantly share code, notes, and snippets.

@vpetro
Created May 13, 2011 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpetro/971186 to your computer and use it in GitHub Desktop.
Save vpetro/971186 to your computer and use it in GitHub Desktop.
plotting bar graphs
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
def plot_state_history(filename):
fname = filename
# get the values
# values = [0, 1, 0, 3, 0, 5 ...]
values = [float(i) for i in open(fname).readlines()[0].split()]
data = [values.count(i) for i in range(1, 6)]
# set up the labels and their locations
labels = list(range(1, 6))
xlocations = np.array(range(len(data)))+0.875
width = 0.25
# set up the figure
fig = Figure(dpi=300)
canvas = FigureCanvas(fig)
axes = fig.add_subplot(111)
axes.clear()
# set up the labels
axes.set_xlabel("States")
axes.set_ylabel("Number of turns in state")
# plot the actual data at the specified locations
axes.bar(xlocations, data, width=width)
# put in all the requred ticks on the axis
axes.set_xticks(xlocations+width/2, labels)
yticks = tuple(sorted(data + list(xrange(0, 1000, 100))))
axes.set_yticks(yticks)
# set the limits on the axis
axes.set_xlim((0,6))
axes.set_ylim((-1,1000))
# set the font size
for idx, tick in enumerate(axes.xaxis.get_major_ticks()):
tick.label1.set_fontsize(9)
for tick in axes.yaxis.get_major_ticks():
tick.label1.set_fontsize(9)
# save the figure
fig_name = fname.replace('dat', 'pdf')
fig.savefig(fig_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment