读取CSV和绘制彩色线图 [英] Read CSV and plot colored line graph

查看:152
本文介绍了读取CSV和绘制彩色线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制带有彩色标记的阈值前后的图表。如果我使用for循环读取解析输入文件与时间 H:M 我可以绘制和颜色两点。但对于所有的点,我不能绘图。

I am trying to plot a graph with colored markers before and after threshold value. If I am using for loop for reading the parsing the input file with time H:M I can plot and color only two points. But for all the points I cannot plot.

输入

akdj 12:00 34515 sdfg
sgqv 13:00 34626 ssfgb
dfbb 13:00 14215 gghgws
ajdf 13:30 14224 gdgva
dsfb 13:45 25672 FW 
sfhh 14:00 85597 adsfb

程序

# ma masked array
import csv
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import  plot

threshold = 30000
x,y = [],[]
csv_reader = csv.reader(open('Count_Time.csv'))

for line in csv_reader:
    y.append(int(line[2]))
    x.append(dt.datetime.strptime(line[1],'%H:%M'))

#plt.figure()

plt.plot(x,y, color='blue')

#Add below threshold markers
below_threshold = y < threshold
plt.scatter(x[below_threshold], y[below_threshold], color='green') 

# Add above threshold markers
above_threshold = np.logical_not(below_threshold)
plt.scatter(x[above_threshold], y[above_threshold], color='red')


plt.show()

错误输出

当我使用下面的代码来读取文件没有显示任何错误,空白图形布局。

When I used the below code to read file that did not show any error but showed a blank graph layout.

data = np.genfromtxt('Count_Time.csv', delimiter=",")
x = data[:,1]
y = data[:,2]

data = np.loadtxt('Count_Time.csv', delimiter=',', dtype='str, time, int, str')
x = data[:,1]
y = data[:,2]


b $ b

错误

Error

 data = np.loadtxt('Count_Time.csv', delimiter=',', dtype='str, time, int, str')
  File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 798, in loadtxt
    dtype = np.dtype(dtype)
TypeError: data type "time" not understood


推荐答案

您需要将 x y 转换为 np .array ,然后计算 above_threshold below_threshold 在你的版本中,你不会得到一个bool数组,但只是 False True

you need to turn x and y into type np.array before you calculate above_threshold and below_threshold, and then it works. In your version, you don't get an array of bools, but just False and True.

我添加逗号分隔符到您的输入csv文件,使其工作(我认为应该在第一个位置。)

I added comma delimiters to your input csv file to make it work (I assume the should be there in the first place?)

import csv
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import  plot

threshold = 30000
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'))

fig=plt.figure()

below_threshold = y < threshold
above_threshold = np.logical_not(below_threshold)

print below_threshold
# False

print above_threshold
# True

x=np.array(x)
y=np.array(y)

plt.plot(x,y, color='blue')

#Add below threshold markers
below_threshold = y < threshold
print below_threshold
# [False False  True  True  True False]

plt.scatter(x[below_threshold], y[below_threshold], color='green') 

# Add above threshold markers
above_threshold = np.logical_not(below_threshold)
print above_threshold
# [ True  True False False False  True]

plt.scatter(x[above_threshold], y[above_threshold], color='red')

fig.autofmt_xdate()

plt.show()

这篇关于读取CSV和绘制彩色线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆