plotting wrong on x axis

こ雲淡風輕ζ 提交于 2021-02-05 10:59:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!