matplotlib: Incorrect scale of axis
matplotlib, python
Solution
You can change pyplots axis' like this
plt.axis([min(x), max(x), min(y), max(y)])
Here is a cut and paste example
from StringIO import StringIO
import matplotlib.pyplot as plt
import pylab, csv, re
data = '''0,11.87772978,65.2269997,7.103221875,6.324708559
1.34E-08,17.65605321,75.09093444,8.309697828,14.87524308
2.69E-08,15.19155521,77.12878487,12.31291774,9.457125362
4.03E-08,23.85118853,88.76138941,20.10571063,8.041540228
5.38E-08,18.77440037,87.15681445,14.53884458,13.36609689
6.72E-08,19.54841939,117.9766076,16.87197928,18.50902666
8.06E-08,33.37595782,102.2086995,40.59474863,9.451430137'''
x=[]
y=[]
file_ = StringIO(data)
reader = csv.reader(file_, delimiter=',')
for row in reader:
if re.search("\d",row[0]):
x.append(float(row[0]))
y.append(float(row[1]))
print x
print y
plt.plot(x, y)
plt.axis([min(x), max(x), min(y), max(y)])
plt.show()
and here is the output
Problem
I have a csv file that looks like this: ``` Axis [m],Channel 1 [],Channel 2 [],Channel 3 [],Channel 4 [] 0,11.87772978,65.2269997,7.103221875,6.324708559 1.34E-08,17.65605321,75.09093444,8.309697828,14.87524308 2.69E-08,15.19155521,77.12878487,12.31291774,9.457125362 4.03E-08,23.85118853,88.76138941,20.10571063,8.041540228 5.38E-08,18.77440037,87.15681445,14.53884458,13.36609689 6.72E-08,19.54841939,117.9766076,16.87197928,18.50902666 8.06E-08,33.37595782,102.2086995,40.59474863,9.451430137 ``` I want to plot the values in the first two columns using matplotlib I have the following code: ``` import matplotlib.pyplot as plt import pylab, csv x=[] y=[] with open("test.csv","rU") as f: reader = csv.reader(f, delimiter=',') for row in reader: if re.search("\d",row[0]): x.append(float(row[0])) y.append(float(row[1])) fig = plt.figure() ax1 = fig.add_subplot(121) ax1.scatter(x,y,color='blue',s=5,edgecolor='none') ax1.set_aspect(1./ax1.get_data_ratio()) # make axes square pylab.savefig('test.jpg') ``` However, this plots all my x-values (122 total values), as a line around 0 (not as a curve as I would expect) see here for an example. I think that this is because the scale of the x-axis is bunching all the data together around 0. I think I will need to change the scale of the x-axis to deal with the small numbers? Any help, as always, will be highly appreciated!