I am trying to create a PNG image with some CSV data but I am getting an error related to the date column (meanwhile converted to list). The error is:
Traceback (most recent call last):
File "C:/Users/user1/Desktop/Py/AgentsStatus/testGraph.py", line 57, in <module>
plt.plot(dateCol,okCol,linewidth=5)
File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 3154, in plot
ret = ax.plot(*args, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 1812, in inner
return func(ax, *args, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\axes\_axes.py", line 1425, in plot
self.add_line(line)
File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 1708, in add_line
self._update_line_limits(line)
File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 1730, in _update_line_limits
path = line.get_path()
File "C:\Python34\lib\site-packages\matplotlib\lines.py", line 925, in get_path
self.recache()
File "C:\Python34\lib\site-packages\matplotlib\lines.py", line 612, in recache
x = np.asarray(xconv, np.float_)
File "C:\Python34\lib\site-packages\numpy\core\numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '11-04-2016'
CSV contains:
11-04-2016;37180;6;23852
18-04-2016;37341;9;24105
25-04-2016;37075;18;23788
My code is:
import csv
import matplotlib.pyplot as plt
import os
path = 'C:\\Users\\user1\\Desktop\\Py\\AgentsStatus\\data.csv'
with open (path) as csvfile:
readCSV = csv.reader(csvfile, delimiter=';')
dateCol = [] # date list
for row in readCSV:
if row:
date0 = row[0]
dateCol.append(date0)
with open (path) as csvfile:
readCSV = csv.reader(csvfile, delimiter=';')
okCol = [] # all agents list
for row in readCSV:
if row:
ok0 = row[1]
okCol.append(ok0)
with open (path) as csvfile:
readCSV = csv.reader(csvfile, delimiter=';')
heaCol = [] # healthy list
from matplotlib import pyplot as plt
from matplotlib import style
style.use('ggplot')
# can plot specifically, after just showing the defaults:
plt.plot(dateCol,okCol,linewidth=5)
plt.plot(dateCol,heaCol,linewidth=5)
plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
My goal is to create something like below:
Could you give me please some tips of what am I doing wrong? I think my problem mitght be that I am not setting the List dateCol as the Index for the graph (to not plot it). Could you please help me on that?
Thanks a lot.
For stuff like this Pandas is unbeatable:
import pandas
import matplotlib.pyplot as plt
df = pandas.read_csv('sampledata.csv', delimiter=';',
index_col=0,
parse_dates=[0], dayfirst=True,
names=['date','a','b','c'])
df.plot()
plt.savefig('sampledata.png')
Results in
Related
I use the script
import matplotlib.pyplot as plt
import numpy as np
import yoda
def readScatter2D(histname, filename):
histos = yoda.core.read(filename+".yoda")
x = []
y = []
h = histos[histname]
for b in h:
x.append(b.x())
y.append(b.y())
plt.plot([x],[y], 'bo')
plt.savefig(str(histname)+".pdf")
readScatter2D("totalCh", "properties_file")
Compiling this with python3 gives me the error:
Traceback (most recent call last):
File "enhancement.py", line 18, in <module>
readScatter2D("totalCh", "properties_file")
File "enhancement.py", line 16, in readScatter2D
plt.savefig(str(histname)+".pdf")
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 697, in savefig
res = fig.savefig(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1573, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 2252, in print_figure
**kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2519, in print_pdf
file = PdfFile(filename)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 422, in __init__
fh = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: 'totalCh.pdf'
I do not understand why matplotlib cannot name the file while saving, by following the histogram names. What is the solution to save the figure following the original histogram? I am using matplotlib version 2.0.2
I've got a problem with programming live - changing plot and histogram from csv file, where the data is saved - the record contains date ('%Y-%M-%D %H:%m:%S') and the time seperated, by tabulator sign '\t'.
However, I was searching through several toturials and documentation and simply can't fix it. Below, there are links I came across:
https://github.com/etsy/skyline/issues/123
How to fix AttributeError: 'Series' object has no attribute 'find'?
https://docs.python.org/3.4/library/datetime.html
Python/Numpy: problems with type conversion in vectorize and item
I did not find answer there and other similar websites, including sentdex toturials on YT. The code below is going to be compiled and used at Raspberry Pi 3 B+, but it was created on Ubuntu OS on standard PC - this code works perfectly there:
import serial
import pandas as pd
import matplotlib.pyplot as plt
import csv
import datetime
import matplotlib.animation as animation
fig, axes = plt.subplots(nrows=1, ncols=2)
ax1, ax2 = axes.flatten()
def update(i):
file_data = pd.read_csv("/home/pi/Desktop/score_board3", delimiter="\t",
parse_dates=[0], header=None, usecols=[0, 1])
ax1.clear()
ax2.clear()
ax1.plot(file_data[0].astype(float), file_data[1])
ax2.hist([file_data[1],], bins='auto', histtype='bar', facecolor='#FF8C00', edgecolor='red')
ax1.set_title('History')
ax1.set_xlabel('Measurement time')
ax1.set_ylabel('Reaction time [s]')
ax2.set_title('Histogram')
ax2.set_xlabel('Reaction time [s]')
ax2.set_ylabel('Number of results')
ani = animation.FuncAnimation(fig, update, interval=1000)
plt.show()
arduino = serial.Serial('/dev/ttyACM0', 9600)
file = open ('/home/pi/Desktop/Noc_Naukowcow/score_board3', 'r+')
try:
while True:
file.read(1)
new_score = arduino.readline()
print(new_score)
score = new_score.decode('utf-8').strip() + "\n"
score_time = '{:%Y-%M-%D %H:%m:%S}'.format(datetime.datetime.now())
file.write(score_time)
file.write('\t')
file.write(score)
file.flush()
except KeyboardInterrupt:
print("KeyboardInterrupt has been caught.")
file.close()
scores_to_read.close()
After compiling that code, I receive a lot of errors found:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 280, in resize
self.show()
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 351, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw
self.figure.draw(self.renderer)
File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1150, in draw
self.canvas.draw_event(renderer)
File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 1815, in draw_event
self.callbacks.process(s, event)
File "/usr/lib/python3/dist-packages/matplotlib/cbook.py", line 549, in process
proxy(*args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/cbook.py", line 416, in __call__
return mtd(*args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/animation.py", line 831, in _start
self._init_draw()
File "/usr/lib/python3/dist-packages/matplotlib/animation.py", line 1490, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "/usr/lib/python3/dist-packages/matplotlib/animation.py", line 1512, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
File "/home/pi/Desktop/kod testowy.py", line 16, in update
parse_dates=[0], header=None, usecols=[0, 1])
File "/home/pi/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "/home/pi/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/home/pi/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 787, in __init__
self._make_engine(self.engine)
File "/home/pi/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/home/pi/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 1756, in __init__
_validate_usecols_names(usecols, self.names)
File "/home/pi/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 1134, in _validate_usecols_names
"columns expected but not found: {missing}".format(missing=missing)
ValueError: Usecols do not match columns, columns expected but not found: [1]
UPDATE:
I think I've just overcame that problem using thread library. However I need to figure out how to set up them together:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import serial
import datetime
import threading
fig, axes = plt.subplots(nrows=2, ncols=1)
ax1, ax2 = axes.flatten()
scores_to_read = serial.Serial ('/dev/ttyACM0', 9600)
file = open ("/home/pi/Desktop/Noc_Naukowcow/score_board.csv", 'r+')
def update(i):
file_data = pd.read_csv("/home/pi/Desktop/Noc_Naukowcow/score_board.csv", delimiter="\t",
parse_dates=[0], header=None, usecols=[0, 1])
ax1.clear()
ax2.clear()
ax1.plot(file_data[0],file_data[1])
ax2.hist([file_data[1],], bins='auto', histtype='bar')
#ax1.set_title('History')
ax1.set_xlabel('Measurement time')
ax1.set_ylabel('Reaction time [s]')
#ax2.set_title('Histogram')
ax2.set_xlabel('Reaction time [s]')
ax2.set_ylabel('Number of results')
ani = animation.FuncAnimation(fig, update, interval=1000)
plt.plot()
def serial_port(file):
file.read()
new_score = scores_to_read.readline()
print(new_score)
score = new_score.decode('utf-8').strip() + "\n"
score_time = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
file.write(score_time)
file.write('\t')
file.write(score)
file.flush()
thread1 = threading.Thread(target=serial_port, args=(file))
thread1.start()
I'm trying to plot time series data using matplotlib using the following code
import matplotlib.pyplot as plt
x, y = data.shape
y_metrics = alarms_metrics + udp_alarms_metrics_names
print x, y
for j in range(1, y):
time_stamp = []
metric = []
plt.figure()
for i in range(x):
time_stamp.append(data_time[i][0])
metric.append(data[i][j])
plt.xlabel("Time")
plt.ylabel(str(y_metrics[j-1]))
plt.plot(time_stamp, metric)
plt.savefig(str(j))
The format of time_stamp is
['2017:02:01:14:44:00', '2017:02:01:14:44:01',...etc]
time_stamp is a list of type 'numpy.string_'
I'm getting the following error
Traceback (most recent call last):
File "/home/joseph/PycharmProjects/demo/src/alarm_log/alarm_db_plot.py", line 23, in <module>
plt.plot(time_stamp, metric)
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3154, in plot
ret = ax.plot(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1814, in inner
return func(ax, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 1425, in plot
self.add_line(line)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1708, in add_line
self._update_line_limits(line)
File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1730, in _update_line_limits
path = line.get_path()
File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 925, in get_path
self.recache()
File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 612, in recache
x = np.asarray(xconv, np.float_)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: invalid literal for float(): 2017:02:01:14:44:00
I think I might need to used matplotlib dateformatter to parse the time_stamp. I tried so but I couldn't figure out how. Any help please?
Not exactly sure what you're trying to plot without seeing the data, but you could try to convert the string to a datetime object. See here for a list with the format codes.
from datetime import datetime
time_stamp = ['2017:02:01:14:44:00', '2017:02:01:14:44:01']
time_stamp = [datetime.strptime(i, '%Y:%m:%d:%H:%M:%S') for i in time_stamp]
print(time_stamp)
I keep getting an error using the numpy loadtxt converter.
Your help is greatly appreciated
import numpy as np
import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
from matplotlib.dates import strpdate2num
import urllib2
## global variables
eachStock = 'AAPL','GOOG','MSFT','AMZN','CMG'
for stock in eachStock:
stockFile = stock+'.txt'
date, closep, highp, lowp, openp, volume = np.loadtxt(eachStock, delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})
dFrame = Series(closep)
here is the first line in my text file
20040322,13.5200,13.6800,12.6100,12.6850,15850720
here is the error I keep getting
Traceback (most recent call last):
File "C:\Users\antoniozeus\Desktop\BuyAndHold.py", line 27, in <module>
converters={ 0: mdates.strpdate2num('%Y%m%d')})
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 796, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 233, in __call__
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))
File "C:\Python27\lib\_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "C:\Python27\lib\_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'AAPL' does not match format '%Y%m%d'
It seems like you mistyped stockFile (filename) as eachStock.
date, closep, highp, lowp, openp, volume = np.loadtxt(
stockFile, delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})
I wrote a Python script (below) which load data from a text file (using pandas) and checks the values in the columns.
import sys
import pandas as pd
import numpy as np
from numpy import ndarray
import math
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from skimage import data
from skimage.feature import match_template
if __name__ == '__main__':
data = pd.read_csv('Fe_PSI_spt_refined.txt', sep=" ", header = None)
data.columns = ["Angle_number", "Omega", "Intensity", "X", "Y", "Address", "ID"]#, "flag"]
Number_of_projections = 181
Number_of_lines_in_txt = 3493
numrows = len(data)
counter_array = []
correlation_threshold_value = 0.7
a = np.zeros(Number_of_lines_in_txt)
output_file = ("output.txt")
for i in range(2, (Number_of_projections + 1)):
filename_cutouts_combined = ("cutouts_combined_%03i.txt" % (i))
filename_cutouts_combined_tag = ("cutouts_combined_tag_%03i.txt" % (i))
image = np.loadtxt(filename_cutouts_combined)
image_tagged = np.loadtxt(filename_cutouts_combined_tag)
for j in range(0, Number_of_lines_in_txt - 1):
print data.Angle_number[j], i
After one iteration of j I get the error below. Do you spot any error I should fix? Thanks
`Traceback (most recent call last):
File "Hyperbola_search.py", line 46, in <module>
print data.Angle_number[j], i
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 491, in __getitem__
result = self.index.get_value(self, key)
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1032, in get_value
return self._engine.get_value(s, k)
File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas/index.c:2661)
File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:2476)
File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3215)
File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6450)
File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6394)
KeyError: 3491`
You load files into image and image_tagged, while a remains unused.
I don't know what data.Angle_number and numrows are, but they appear to be from libraries, not related to your files.