3

Possible Duplicate:
How to plot data against specific dates on the x-axis using matplotlib
Plot with non-numerical data on x axis (for ex., dates)

I am attempting to generate a bar graph in matplotlib where the x-axis and y-axis values are tuples of (dates, integers). The data has the following form:

data = [('2012-02-26', 17), ('2012-03-07', 16), ('2012-06-30', 16), 
        ('2012-03-16', 15), ('2012-06-24', 15), ('2012-05-13', 14), 
        ('2012-03-19', 12), ('2012-02-18', 9), ('2012-04-07', 9), 
        ('2012-04-09', 8)]

I can generate a simple bar graph if I simply use integers as x values:

import scipy as sp
from matplotlib import pyplot as pl
x = sp.arange(100)
y = sp.random.randint(1,100, 100)
fig = figure()
pl.bar(x,y)

enter image description here

How can I plot a similar bar graph with string dates on the x-axis? Ideally, I would also like to have the x-axis labels show as dates. I found a similar example on the matplotlib recipe page, but I could not get it to work with my example. Thanks for the help.

Community
  • 1
  • 1
turtle
  • 7,533
  • 18
  • 68
  • 97

2 Answers2

1

Use the plot_date function instead of plot().

plot_date

reptilicus
  • 10,290
  • 6
  • 55
  • 79
0

I used

ax.xaxis.set_major_formatter(dates.DateFormatter('%d/%m'))
f p
  • 3,165
  • 1
  • 27
  • 35