问题
thank you for reading my question
I have to plot my data like that. When I plot my data
I am having an output like this. Code for reading the data from the file and plotting is below
#!/usr/bin/env python3.8
import matplotlib.pyplot as plt
import numpy as np
with open('data.txt','r') as file2:
y= [line.rstrip('\n') for line in file2]
notf2=y[2:]
z=[a.rstrip(' ') for a in notf2]
x_data=[]
y_data=[]
for j in range(0, len(z)):
x_data += [z[j][:3]]
y_data += [z[j][5:]]
x__data=[]
y__data=[]
for k in range(0, len(z)):
x__data += [x_data[k]]
y__data += [y_data[k]]
rx=x__data.reverse()
ry=y__data.reverse()
#plt.plot(x__data[::-1],y__data, '.')
plt.plot(x__data,y__data, '.')
plt.show()
I don't understand why it is plotting it different. It is reversing x axis but when I try to reverse it
plt.plot(x__data[::-1],y__data, '.')
it is not fixing it , just showing the same thing. When I plot
plt.plot(x__data[::-1],y__data, '.')
plt.plot(x__data,y__data, '.')
both of those it is reversing and showing . And I am not even talking about those numbers (black lines) written as if they are labels
回答1:
Your data are strings. You need to convert them to float
来源:https://stackoverflow.com/questions/64877175/plotting-wrong-on-x-axis