The text file file :(x range is from 0 to 35) has four columns
x h1 fL0 fR0
I need to plot the 3 functions h1
, fl0
and fR0
in the same graph. I also need normalise
x values by dividing by the maximum. So that all x
values in both files range from 0 to 1
.
But i need help in taking the input from the text file and plotting them ,
The sample text is -
0 0 2.64834E-76 2.538822025
0.343137255 7.09437E-16 1.4726E-75 0.643489985
0.68627451 1.50251E-15 8.1884E-75 0.163099011
1.37254902 3.73658E-15 2.53178E-73 0.010477807
2.058823529 7.79212E-15 7.82801E-72 0.000673115
The code I have written is ---
import matplotlib.pyplot as plt
import csv
x = []
y = []
z = []
q = []
with open('c1.txt','r') as csvfile:
plots = csv.reader(csvfile, delimiter=' ')
for row in plots:
x.append((row[0]))
y.append((row[1]))
z.append((row[2]))
q.append((row[3]))
plt.plot(x,y, label='h1')
pl.plot(x,z, label='fL0')
p1.plot(x,q, label='fR0')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
The problem from this code is :IndexError : list index out of range
Try this.
import matplotlib.pyplot as plt
For easier reading and manipulating of the data I use the pandas library.
import pandas as pd
Read the data
data = pd.read_csv('c1.txt', delimiter=' ')
Re-label the columns and normalise for x
data.columns = ['x', 'h1', 'fL0', 'fR0']
data['xnorm'] = data.x / max(data.x)
Create the plot
fig, ax1 = plt.subplots()
ax1.plot(data.xnorm, data.h1, color='red')
ax2 = ax1
ax2.plot(data.xnorm, data.fL0, color='blue')
ax3 = ax1
ax3.plot(data.xnorm, data.fR0, color='green')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.