使用yaxis值

时间:2015-06-14 16:12:32

标签: python matplotlib graph plot

我正在尝试绘制Color changing折线图。但是当我试图读取文件并绘制相同的图形时会出现以下错误。请建议进行哪些更改以同时获取变色图和标记。

输入文件:

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轴和Col[2]是y轴值。

程序:

# ma masked array
import csv
import datetime as dt
from numpy import logical_or, arange, sin, pi
from numpy import ma
from matplotlib.pyplot import  plot, show


x,y = [],[]
csv_reader = csv.reader(open('Input.csv'))
for 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()

错误

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
    for line in self._get_lines(*args, **kwargs):
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 326, in _grab_next_args
    for 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
    by = 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 be unchanged

1 个答案:

答案 0 :(得分:0)

您的代码中存在多个问题。

  1. 您混淆了xy
  2. 您需要将y转换为NumPy数组。
  3. plot()中有关于行格式的问题。
  4. 这是整个代码:

    import csv
    import datetime as dt
    from numpy import logical_or, arange, sin, pi
    from numpy import array, ma
    from matplotlib.pyplot import  plot, show
    
    
    x,y = [],[]
    csv_reader = csv.reader(open('Input.csv'))
    for 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()