简体   繁体   English

在matplotlib中显式添加x轴时,图形会变形

[英]Plot gets distorted when adding x axis explicitly in matplotlib

I'm preparing some data for analysis. 我正在准备一些数据进行分析。 The data contains timestamped pollution measurements from different sensors. 数据包含带有时间戳的来自不同传感器的污染测量值。 To get a feeling for the data and make sure I get the data correctly and that there aren't big gaps in it, I wanted to plot not only the pollution, but the pollution against the timestamp. 为了对数据有感觉,并确保我正确地获取了数据,并且其中没有很大的差距,我不仅要绘制污染,而且还要根据时间戳绘制污染。 I expect an almost identical plot like the one plotting only the pollution against the data index, but with small gaps for the periods when the sensors didn't work. 我希望有一个几乎一样的图,就像只绘制污染与数据索引的图一样,但是在传感器不工作的期间会有很小的差距。 Unfortunately something goes wrong and the two plots are completely different. 不幸的是,出了点问题,两个图完全不同。 The problem evidently lies in my approach of plotting the second plot. 问题显然出在我绘制第二个图的方法上。 The data point are somehow connected back and fourth in time, the whole thing makes no sense. 数据点以某种方式及时地前后连接,整个事情毫无意义。 How do I get this to work? 我该如何工作? 预期结果 错误的结果

This is the code producing the plots. 这是产生绘图的代码。

import matplotlib.pyplot as plt
import pandas

#load data set from csv
df = pandas.read_csv('csv_exports/measurements1207.csv')
#add column names to laoded data
df.columns = ['id', 'air_quality_index', 'from_date', 'latitude', 'longitude', 'pm_1',
              'pm_10', 'pm_25', 'pollution_level', 'sensor_id', 'source', 'till_date',
              'wind_deg', 'wind_speed']
df_pm = df[['sensor_id', 'from_date', 'pm_10', 'pm_25']]

#last 20 thousend pm10 and pm25 values
last_20k = df_pm.tail(20000)
print("first row example")
print(last_20k.iloc[0])
first_row_sensor_id = last_20k.iloc[0].loc['sensor_id']

one_sensor = df_pm.loc[df['sensor_id'] == first_row_sensor_id][['from_date', 'pm_10', 'pm_25']]
one_sensor = one_sensor.dropna()
one_sensor[['from_date']] = pandas.to_datetime(one_sensor['from_date'], format='%Y-%m-%d %H:%M:%S.%f')
#plot values
plt.plot(one_sensor[['pm_10', 'pm_25']])
plt.show() // first plot
plt.plot(one_sensor[['from_date']], one_sensor[['pm_10', 'pm_25']])
plt.show() // second faulty plot

As stated in the comment by @ImportanceOfBeingErnest the data needs to be sorted. 如@ImportanceOfBeingErnest的注释中所述,需要对数据进行排序。 Simply add a line like this to your code. 只需在代码中添加这样的行即可。

one_sensor = one_sensor.sort_values(['from_date'], ascending=[True])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM