How to plot data from different files? - python

I'm trying to plot data from different text files.
I've had to manipulate data so I could construct the graphic that I desire for just one document. All other documents are in the same way. But I can't see how I can plot all in one panel. The code that I tried for the loop of all files was:
import numpy as np
import matplotlib.pyplot as plt
filenames=["b_10.txt","b_100.txt","b_500.txt","b_1000.txt"]
for i in filenames:
with open(i) as f:
data = f.read()
data = data.split('\n')
x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[-1] for row in data]
x
a=list(map(str.strip, y))
trip_list = [item.strip('\tall\t') for item in y]
yy = np.array(trip_list[1:12])
yy
xx= np.array(x[21:32])
xx
fig = plt.figure()
plt.hold(True)
plt.ylabel('Precisão Interpolada')
plt.xlabel('Recall')
plt.plot(xx,yy,'-',label="Precisão Interpolada vs Recall")
plt.show()
It gave me an error:
ValueError: could not convert string to float:
and a blank panel
enter image description here

Related

Data Points not being plotted on a Matplotlib plot

Hello I am attempting to write a program that allows the plotting of the graph from various data sets from a excel database.(The x axis is a fixed set of values while the data values from other columns can be selected). However, the graph that is plotted only contains the axes of the graph, while the data points are completely missing. The code I have used is as such:
import xlrd
import matplotlib.pyplot as plt
from matplotlib.figure import *
loc = ("C:\\Users\\yeoho\\DCO_Raw_Data.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0,0)
x = [[sheet.cell_value(r,0)]for r in range(6,sheet.nrows)]
checkOn = True
while checkOn:
FileName = [[sheet.cell_value(0,c)]for c in range(1,13)]
print(FileName)
print("Enter the Integer (1-n) corresponding to the file name that you would like to plot")
z = int(input())
y = [[sheet.cell_value(r,z)]for r in range(6,sheet.nrows)]
fig = plt.figure()
ax = fig.add_subplot(111)
assert len(x) == len(y)
for i in range(len(x)):
plt.plot(x[i],y[i],color='black')
plt.show()
break
The code in lines 16-21 were taken from another stackoverflow page. How to plot two lists of tuples with Matplotlib
The original code did not have a color parameter but I have found out that that is not the source of the issue.
I am unsure of what the issue here is. Thank you for taking your time to read this and I hope you can help me with this issue.

ValueError: could not convert string to float: plotting a graph on python

I've imported a csv file which all contain decimals with exponents for example (5.5006250364943992**02). I keep getting ValueError: could not convert string to float. This is what I have done:
import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('DNSdata.csv', 'r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(float(row[0]))
y.append(float(row[1]))
plt.plot(x, y, label='DNSdata')
plt.xlabel('x')
plt.ylabel('y')
plt.title('DNSdata')
plt.show()
Is the Syntax you wrote the Syntax that is used in the file? I don't think Python can intepret "5.5**02".
If by "**" mean "10^" then you would need to manually make that replacement.
tmp = row[0].replace("**","e")
x.append(tmp)

Matplotlib plots no line plots; crosses, circles and triangles work

I'm trying to plot some measurement data with Matplotlib.
With the code shown below i get the plot window and gui but no plot is drawn. If i change the plot kind to circles or crosses it works just fine.
# coding=utf-8
import matplotlib.pyplot as plt
import csv
with open("AgPVP8.2.171g1L#2.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
frequencies = []
phases = []
for row in reader:
frequency = float(row[0])
phase = float(row[4])
frequencies.append(frequency)
phases.append(phase)
plt.plot([frequencies], [phases], "b-")
plt.xscale("log")
plt.show()
The problem is that frequencies and phases only exist in the scope of with open(..., you must place it within this:
import matplotlib.pyplot as plt
import csv
with open("AgPVP8.2.171g1L#2.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
frequencies = []
phases = []
for row in reader:
frequency = float(row[0])
phase = float(row[4])
frequencies.append(frequency)
phases.append(phase)
plt.plot(frequencies, phases, "-b")
plt.xscale("log")
plt.show()
plt.plot(x,y) requires x and y to be lists (or in general sequences) or arrays.
Here, you are trying to plot a list of a list, i.e. [x] is not the same as x.
So in your code you need to replace plt.plot([frequencies], [phases], "b-") with
plt.plot(frequencies, phases, "-b")
The complete code should then look like:
import matplotlib.pyplot as plt
import csv
with open("AgPVP8.2.171g1L#2.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
frequencies = []
phases = []
for row in reader:
frequency = float(row[0])
phase = float(row[4])
frequencies.append(frequency)
phases.append(phase)
plt.plot(frequencies, phases, "b-")
plt.xscale("log")
plt.show()
I would suggest to have a look at numpy.loadtxt or numpy.genfromtxt. Both would make is much easier to read in a csv file, e.g. in this case
import matplotlib.pyplot as plt
import numpy as np
frequencies, phases = np.loadtxt("AgPVP8.2.171g1L#2.csv", unpack=True, usecols = (0,4), delimiter=",")
plt.plot(frequencies, phases, "b-")
plt.xscale("log")
plt.show()

HDF5 file to diagram in python

I'm trying to generate some diagrams from an .h5 file but I don't know how to do it.
I'm using pytables, numpy and matplotlib.
The hdf5 files I use contains 2 sets of data, 2 differents curves.
My goal is to get diagrams like this one.
This is what I managed to do for the moment:
import tables as tb
import numpy as np
import matplotlib.pyplot as plt
h5file = tb.openFile(args['FILE'], "a")
for group in h5file.walkGroups("/"):
for array in h5file.walkNodes("/","Array"):
if(isinstance(array.atom.dflt, int)):
tab = np.array(array.read())
x = tab[0]
y = tab[1]
plt.plot(x, y)
plt.show()
x and y values are good but I don't know how to use them, so the result is wrong. I get a triangle instead of what I want ^^
Thank you for your help
EDIT
I solved my problem.
Here is the code :
fig = plt.figure()
tableau = np.array(array.read())
x = tableau[0]
y = tableau[1]
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(x)
ax2.plot(y)
plt.title(array.name)
plt.show()

python plotting overrides data

I have lot of binary and ascii files in one folder. I am reading them using glob module. Doing processing of the binary data so that I can plot them. And finally, I am trying to plot simplified binary data in one subplot and normal ascii file in another subplot. The problem I am facing is that it can generate plots for the corresponding binary files. But for the ascii files it just simply override the previous files and always generates the same plot. Here is the simplied version of the code for an example-
import glob
import numpy as np
from struct import unpack
import matplotlib.pyplot as plt
chi = sorted(glob.glob('C:/Users/Desktop/bin/*.chi'))
for index,fh in enumerate(chi):
data = np.genfromtxt(fh, dtype = float)
x = [row[0] for row in data]
y = [row[1] for row in data]
binary = sorted(glob.glob('C:/Users/Desktop/bin/*.bin'))
for count,FILE in enumerate(binary):
F = open(FILE,'rb')
B = unpack('f'*1023183, F.read(4*1023183))
A = np.array(B).reshape(1043, 981)
F.close()
#a = something column 1 # some further processing
#b = something column 2 # and generates 1D data
fig = plt.figure(figsize=(11, 8.0))
ax1 =fig.add_subplot(211,axisbg='w')
ax1.plot(a,b)
ax2 =fig.add_subplot(212, axisbg ='w')
ax2.plot(x,y)
plt.show()
Can somebody please explain why the files are replacing each other during plotting only for one set of data where the other set is plotting correctly?
the structures of the loops is not correct in your example, you must have the plot command inside the loop over the ascii file, else only the last one is plotted. This should work:
try it like this:
import glob
import numpy as np
from struct import unpack
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(11, 8.0))
chi = sorted(glob.glob('C:/Users/Desktop/bin/*.chi'))
for index,fh in enumerate(chi):
data = np.genfromtxt(fh, dtype = float)
x = [row[0] for row in data]
y = [row[1] for row in data]
ax1 =fig.add_subplot(211, axisbg ='w')
ax1.plot(x,y)
binary = sorted(glob.glob('C:/Users/Desktop/bin/*.bin'))
for count,FILE in enumerate(binary):
F = open(FILE,'rb')
B = unpack('f'*1023183, F.read(4*1023183))
A = np.array(B).reshape(1043, 981)
F.close()
#a = something column 1 # some further processing
#b = something column 2 # and generates 1D data
ax2 =fig.add_subplot(212,axisbg='w')
ax2.plot(a,b)
plt.show()

Categories