1

I'm very beginner at Python and matplotlib but trying to learn! I would like to use matplotlib to plot some simple data from a CSV containing dates with a frequency. The X axis containing dates and Y containing the frequency. Example data from CSV:

2011/12/15,5
2011/12/11,4
2011/12/19,2

I checked the "matplotlib.sf.net/examples" out but appears all the test data is downloaded from a http get. I would really appreciate if someone could guide me with some example code of how to read in (presumably using CSV reader) and display data in chart.

Thank you!!

4
  • how do you want the data represented? Histogram? Is the example data a copy and paste from the CSV file? I'm assuming you have the X and Y value in the same cell? Commented Apr 17, 2012 at 22:07
  • Also, is the data all on one row? Commented Apr 17, 2012 at 22:11
  • Harpal - Thanks for the response. Ideally it be great to have it in a line chart. That is correct regarding X and Y but I can massage the way its stored in the CSV to whatever format to make if its easier another way. Thank you. Commented Apr 17, 2012 at 22:13
  • actually each line has a data and a frequency so line one: 2011/12/15,5 line two: 2011/12/10,4 Commented Apr 17, 2012 at 22:14

2 Answers 2

2

Maybe you look for something like:

import csv
import datetime as dt
import matplotlib.pyplot as plt

arch = 'C:\\Python26\\programas\\test.csv'
data = csv.reader(open(arch))

data = [(dt.datetime.strptime(item, "%Y/%m/%d"), float(value)) for item, value in data]
data.sort()
[x, y] = zip(*data)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)

ax.grid(True)
fig.autofmt_xdate()

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

I've tried to keep my code as simple as possible and this is by no means elegant, but here you go:

import csv
import matplotlib.pyplot as plt

### Making test CSV file ###
data = [['2011/12/15,5'],['2011/12/11,4'],['2011/12/19,2'],['2011/12/16,3'],['2011/12/20,8'],['2011/12/14,4'],['2011/12/10,10'],['2011/12/9,7']]
with open('test.csv', 'wb') as f:
    writer = csv.writer(f)
    for i in data:
        writer.writerow(i)


### Extract data from CSV ###
with open('test.csv', 'rb') as n:
    reader = csv.reader(n)
    dates = []
    freq = []
    for row in reader:
        values = row[0].split(',')
        dates.append(values[0])
        freq.append(values[1])          


### Do plot ###
false_x = [x for x in range(len(dates))]
plt.plot(false_x,freq, 'o-')
plt.xticks(range(len(dates)), (dates), rotation=45)
# plt.axis([xmin, xmax, ymin, ymax]) - sets axes limits on graph
plt.axis([-1, 8, 0, 11])
plt.show()

This makes:

enter image description here

1 Comment

This example is using csv.reader and manually splitting the rows, which isn't necessary, and can cause problems on some csv files. There's a question about it here for a more detailed explanation. Harpal's answer here helped me a lot getting into matplotlib though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.