python - Plotting color changing with yaxis value -
i trying plot color changing line graph. when trying read file , plot same graph getting following error. please suggest changes made color changing graphs , markers @ same time.
input file:
a_001,12:00,65452,abcd a_002,13:00,24562,cdfa a_003,13:30,2232351,ggadg c_234,13:00,46526,fwfd d_423,14:00,97669,gage
col[1]
x axis , col[2]
y axis values.
program:
# ma masked array import csv import datetime dt numpy import logical_or, arange, sin, pi numpy import ma matplotlib.pyplot import plot, show x,y = [],[] csv_reader = csv.reader(open('input.csv')) line in csv_reader: x.append(int(line[2])) y.append(dt.datetime.strptime(line[1],'%h:%m')) upper = 30000 lower = 10000 supper = ma.masked_where(y < upper, y) slower = ma.masked_where(y > lower, y) smiddle = ma.masked_where(logical_or(y<lower, y>upper), y) plot(x, slower, 'g', x, smiddle, 'b', x, supper, 'r', 'o-') show()
error
traceback (most recent call last): file "map_line_color.py", line 22, in <module> plot(x, slower, 'g', x, smiddle, 'b', x, supper, 'r') file "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2987, in plot ret = ax.plot(*args, **kwargs) file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 4137, in plot line in self._get_lines(*args, **kwargs): file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 326, in _grab_next_args seg in self._plot_args(remaining[:isplit], kwargs): file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 295, in _plot_args x, y = self._xy_from_xy(x, y) file "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 214, in _xy_from_xy = self.axes.yaxis.update_units(y) file "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1336, in update_units converter = munits.registry.get_converter(data) file "/usr/lib/pymodules/python2.7/matplotlib/units.py", line 137, in get_converter xravel = x.ravel() file "/usr/lib/python2.7/dist-packages/numpy/ma/core.py", line 4025, in ravel r._mask = ndarray.ravel(self._mask).reshape(r.shape) valueerror: total size of new array must unchanged
there several issues in code.
- you mixed
x
andy
. - you need convert
y
into numpy array. - you had issues in
plot()
regarding formats of lines.
this whole code:
import csv import datetime dt numpy import logical_or, arange, sin, pi numpy import array, ma matplotlib.pyplot import plot, show x,y = [],[] csv_reader = csv.reader(open('input.csv')) line in csv_reader: y.append(int(line[2])) x.append(dt.datetime.strptime(line[1],'%h:%m')) y = array(y) upper = 30000 lower = 10000 supper = ma.masked_where(y < upper, y) slower = ma.masked_where(y > lower, y) smiddle = ma.masked_where(logical_or(y<lower, y>upper), y) plot(x, slower, x, smiddle, x, supper) show()
Comments
Post a Comment