i have file containing data below. see first column dates. need plot other columns against dates in first column. tried use code below receive error message. code error 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 dt import matplotlib.pyplot plt lis1=[] lis2=[] lis3=[] lis4=[] lis5=[] open('/home/omar/desktop/finall.csv', 'ru') f: reader=csv.reader(f, delimiter=',') 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 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 >= 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 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 >= 1
the error in plot
command.
you need repeat on x axis each field plot on 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-')
Comments
Post a Comment