Using Matplotlib, visualize CSV data

☆樱花仙子☆ 提交于 2020-04-19 22:34:38

问题


Using matplotlib/pandas/python, I cannot visualize data as values per 30mins and per days is a new question, which is strongly related to this question.

I want to visualize CSV data with Matplotlib.

Following is my code named 1.30mins.py

import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np

style.use('ggplot')

x,y =np.loadtxt('total_watt.csv',
                unpack = True,
                delimiter = ',')

plt.plot(x,y)

plt.title('Example')
plt.ylabel('Y axis')
plt.xlabel('X axis')

plt.show()

When I implemtented 1.30mins.py, I got a following error message.

(DataVizProj)Soma-Suzuki:Soma Suzuki$ python 1.30mins.py
Traceback (most recent call last):
  File "1.30mins.py", line 10, in <module>
    delimiter = ',')
  File "/Users/Suzuki/Envs/DataVizProj/lib/python2.7/site-packages/numpy/lib/npyio.py", line 856, in loadtxt
    items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: invalid literal for float(): 2011-04-18 13:22:00

This is my total_watt.csv

2011-04-18 21:22:00 659.670303375527
2011-04-18 21:52:00 576.304871428571
2011-04-18 22:22:00 2,497.20620579196
2011-04-18 22:52:00 2,790.20392088608
2011-04-18 23:22:00 1,092.20906629318
2011-04-18 23:52:00 825.994417375886
2011-04-19 00:22:00 2,397.16672089666
2011-04-19 00:52:00 1,411.66659265233

As far as I searched by myself, I need to add converters or, %y-%m-%t to my program.

My python version is 2.76 My matpltlib version is 1.42


回答1:


Your data

2011-04-18 21:22:00 659.670303375527
2011-04-18 21:52:00 576.304871428571
...

is not delimited by spaces or commas. It could be regarded as having fixed-width columns however. np.genfromtxt can read fixed-width data. Instead of passing a string to delimiter, pass a sequence of ints representing the width of each field.


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib import style
style.use('ggplot')

x, y = np.genfromtxt('total_watt.csv',
                     unpack=True,
                     delimiter=[19, 10**6], dtype=None)

x = mdates.datestr2num(x)
y = np.array(np.char.replace(y, ',', ''), dtype=float)

fig, ax = plt.subplots()
ax.plot(x, y)

plt.title('Example')
plt.ylabel('Y axis')
plt.xlabel('X axis')
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

fig.autofmt_xdate()
plt.show()

yields enter image description here




回答2:


I don't know whether numpy has the functionality to read datetime objects directly. However, if you are NOT looking for an elegant solution, here is some quick and dirty code to do what you want using two other modules csv and datetime.

I use the file 'sample.csv' (note where I have placed commas):

     2011-04-18 21:22:00, 659.670303375527
     2011-04-18 21:52:00, 576.304871428571

And the code is

     from matplotlib import style
     from matplotlib import pylab as plt
     import numpy as np

     style.use('ggplot')

     filename='sample.csv'
     date=[]
     number=[]

     import csv
     with open(filename, 'rb') as csvfile:
         csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
         for row in csvreader:
             if len(row) ==2 :
                 date.append(row[0])
                 number.append(row[1])

     number=np.array(number)

     import datetime
     for ii in range(len(date)):
         date[ii]=datetime.datetime.strptime(date[ii], '%Y-%m-%d %H:%M:%S')

     plt.plot(date,number)

     plt.title('Example')
     plt.ylabel('Y axis')
     plt.xlabel('X axis')

     plt.show()

Giving me the following graph. Graph of Result

If you are looking for a more elegant solution using numpy, I'm sure someone will know a better way.



来源:https://stackoverflow.com/questions/31241702/using-matplotlib-visualize-csv-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!