I have some troubles with plotting the figure. I have a file with some results computed by application I wrote in c++ and I would like to plot the figure for the computed data.
The problem is that I don’t know the ‘max’ and ‘min’ limit on axis X and axis Y …
I tried:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
def plot_it(ox, oy, x_label, y_label, filename) :
fig = plt.figure()
axis = fig.add_subplot(111)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.ylim(min(oy),max(oy))
plt.xlim(min(ox),max(ox))
axis.plot(ox, oy, color = 'red')
plt.savefig(filename)
plt.clf()
plt.close()
filename = input("Filename (file with data)\n>")
res = []
try :
with open(filename, 'r') as file :
for line in file :
line = line.rstrip('\n')
res.append(line.split(" "))
except IOError :
print("IO error")
if len(res) != 0 :
ox = []
oy = []
x_label = str(input("OX label\n>"))
y_label = str(input("OY label\n>"))
for i in range(0,len(res)) :
ox.append(res[i][0])
oy.append(res[i][1])
plot_it(ox, oy, x_label, y_label, 'fig_' + str(filename[:len(filename)-4]) + '.png')
Where my file is here: http://pastie.org/private/4rl64ule9ymljmp6g5bfzg (just copy it and save as file.txt)
I got those errors:
Traceback (most recent call last): File “E:/plotter.py”, line 43, in
plot_it(ox, oy, x_label, y_label, ‘fig_’ + str(filename[:len(filename)-4]) + ‘.png’) File “E:/plotter.py”, line
14, in plot_it
plt.ylim(min(oy),max(oy)) File “C:\Python27\lib\site-packages\matplotlib\pyplot.py”, line 1252, in
ylim
ret = ax.set_ylim(*args, **kwargs) File “C:\Python27\lib\site-packages\matplotlib\axes.py”, line 2642, in
set_ylim
bottom, top = mtransforms.nonsingular(bottom, top, increasing=False) File
“C:\Python27\lib\site-packages\matplotlib\transforms.py”, line 2282,
in nonsingular
if (not np.isfinite(vmin)) or (not np.isfinite(vmax)): NotImplementedError: Not implemented for this type
Add:
to the second block of code you give, this will set your x and limits to the min/max of your data.