2

I have a file containing data as below. you see the first column is dates. I need to plot all other columns against dates in the first column. I tried to use the code below but I receive an error message. the code an error are provided.

Data

2010-01-01,1.628,0.7063157895,0,0.9216842105
2010-01-03,1.602631579,0.6901052632,0,0.9125263158
2010-01-04,1.5818947369,0.6775789474,0,0.9043157895
2010-01-05,1.5755789473,0.6716842105,0,0.9038947368
2010-01-06,1.5605263158,0.6622105263,0,0.8983157895
2010-01-07,1.5611578948,0.6608421053,0,0.9003157895
2010-01-08,1.5598947369,0.6593684211,0,0.9005263158
2010-01-09,1.5576842105,0.6569473684,0,0.9007368421
2010-01-10,1.5462105263,0.6543157895,0,0.8918947368
2010-01-11,1.5656842105,0.6666315789,0,0.8990526316
2010-01-12,1.5517894736,0.6546315789,0,0.8971578947
2010-01-13,1.5558947368,0.6551578947,0,0.9007368421
2010-01-14,1.5638947369,0.6588421053,0,0.9050526316
2010-01-15,1.5375789474,0.6432631579,0,0.8943157895
2010-01-16,1.522631579,0.6352631579,0,0.8873684211
2010-01-17,1.5056842105,0.6254736842,0,0.8802105263
2010-01-18,1.4881052632,0.6157894737,0,0.8723157895
2010-01-19,1.4889842789,0.6251948052,0,0.8637894737
2010-01-20,1.4733383459,0.6182857143,0,0.8550526316
2010-01-21,1.4507368421,0.6009473684,0,0.8497894737

Code

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

lis1=[]
lis2=[]
lis3=[]
lis4=[]
lis5=[]

with open('/home/omar/Desktop/finall.csv', 'rU') as f:
reader=csv.reader(f, delimiter=',')
for row in reader:

    lis1.append(dt.datetime.strptime(row[0],'%Y-%m-%d'))

    lis2.append(row[1])
    lis3.append(row[2])
    lis4.append(row[3])
    lis5.append(row[4])

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(lis1,lis2,lis3,lis4,lis5,'o-')
fig.autofmt_xdate()

plt.show()

Error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widge/externalshell/sitecustomize.py", line 540, in runfile execfile(filename, namespace)
File "/home/omar/python/plot_txt.py", line 37, in <module>
    fig.autofmt_xdate()
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 431, in autofmt_xdate
    for label in self.axes[0].get_xticklabels():
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2614, in get_xticklabels
self.xaxis.get_ticklabels(minor=minor))
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1161, in get_ticklabels
    return self.get_majorticklabels()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1145, in get_majorticklabels
ticks = self.get_major_ticks()
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1244, in get_major_ticks
numticks = len(self.get_major_locator()())
File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 802, in __call__
self.refresh()
File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 819, in refresh
dmin, dmax = self.viewlim_to_dt()
File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 564, in viewlim_to_dt
return num2date(vmin, self.tz), num2date(vmax, self.tz)
File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 311, in num2date
return _from_ordinalf(x, tz)
File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 214, in _from_ordinalf
dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1
>>> Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_qt4.py", line 374, in idle_draw
self.draw()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_qt4agg.py", line 154, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1091, in draw    ticks_to_draw = self._update_ticks(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 945, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 889, in iter_ticks
    majorLocs = self.major.locator()
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 802, in __call__
    self.refresh()
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 819, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 564, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 311, in num2date
    return _from_ordinalf(x, tz)
  File "/usr/lib/pymodules/python2.7/matplotlib/dates.py", line 214, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix)
ValueError: ordinal must be >= 1
1
  • The traceback is formatted very bad so it is hard to read. There are multiple issues in your code at ax.plot(lis1,lis2,lis3,lis4,lis5,'o-'). 1) plot() may only plot a graph with x- and y- values. You can only plot on graph at a time. 2) plot does not accept date objects as x values. You have to put in scalars. Commented Jan 29, 2016 at 10:39

2 Answers 2

2

The error is in your plot command.

You need to repeat what is on the x axis for each field you plot on the y axis.

So change this:

ax.plot(lis1,lis2,lis3,lis4,lis5,'o-')

to this:

ax.plot(lis1,lis2,lis1,lis3,lis1,lis4,lis1,lis5,'o-')
Sign up to request clarification or add additional context in comments.

2 Comments

@ tom: I had to repeat lis1. However, I need more info on giving legend and make the graph as a scatter plot
you should probably ask a new question, showing how your plot look now, and describe how you would like to change it.
1

try:

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

lis1=[]
lis2=[]
lis3=[]
lis4=[]
lis5=[]

with open('/home/omar/Desktop/finall.csv', 'rU') as f:
reader=csv.reader(f, delimiter=',')
for row in reader:

    lis1.append(dt.datetime.strptime(row[0],'%Y-%m-%d'))

    lis2.append(row[1])
    lis3.append(row[2])
    lis4.append(row[3])
    lis5.append(row[4])

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot_date(lis1,lis2,'o-',
             lis1,lis3,'o-',
             lis1,lis4,'o-',
             lis1,lis5,'o-')

plt.show()

see documentation here: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot

Comments

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.