0

I have a CSV file (Data.csv):

2012-01-01,0
2012-01-02,1
2012-01-03,8
2012-01-04,1
...etc

I am trying to make a bar graph, the dates on the x axis and the values on the y axis:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

def graph():
    date, value = np.loadtxt('Data.csv', delimiter=',', unpack=True,
        converters = {0: mdates.strpdate2num('%Y-%m-%d')})

    fig = plt.figure()

    ax1 = fig.add_subplot(1,1,1, axisbg='white')

    plt.plot_date(x=date, y=value, fmt='-')

    plt.title('Title')
    plt.ylabel('Value')
    plt.xlabel('Date')
    plt.show()

graph()

Unfortunately, this is a line graph.

Could someone please assist?

Craig
  • 105
  • 1
  • 4

1 Answers1

0

The matplotlib documentation has a pretty good example: barchart_demo.py

This should do it:

fig, ax = plt.subplots()

bar1 = ax.bar(date, value)

plt.title('Title')
plt.ylabel('Value')
plt.xlabel('Date')
plt.show()
chunpoon
  • 950
  • 10
  • 17
  • Thanks - very useful. However this solution gets rid of the x-axis tick labels that came with `plt.plot_date(x=date, y=value, fmt='b')`. Is there any way they can be re-included? – Craig Oct 20 '14 at 13:57