我有一个数据框,其中包含每日日期和几个测站,在测站内,有几天没有测量值,但日期是完整的。我做了一些示例数据:
import pandas as pd
import numpy as np
np.random.seed(0)
rng = pd.date_range('1980-01-01', periods = 24, freq='MS')
df = pd.DataFrame({'Date':rng,'Stn1':np.random.randn(len(rng)),'Stn2':np.random.randn(len(rng)),'Stn3':np.random.randn(len(rng)) })
df.loc[df['Stn1'] > 1,'Stn1'] = np.nan
df.loc[df['Stn2'] < -1,'Stn2'] = np.nan
df.loc[df['Stn3'] > 0,'Stn3'] = np.nan
这是它的外观:
Date Stn1 Stn2 Stn3
0 1980-01-01 NaN 2.269755 -1.613898
1 1980-01-02 0.400157 NaN -0.212740
2 1980-01-03 0.978738 0.045759 -0.895467
3 1980-01-04 NaN -0.187184 NaN
4 1980-01-05 NaN 1.532779 -0.510805
5 1980-01-06 -0.977278 1.469359 -1.180632
6 1980-01-07 0.950088 0.154947 -0.028182
7 1980-01-08 -0.151357 0.378163 NaN
8 1980-01-09 -0.103219 -0.887786 NaN
9 1980-01-10 0.410599 NaN NaN
10 1980-01-11 0.144044 -0.347912 -0.634322
11 1980-01-12 NaN 0.156349 -0.362741
12 1980-01-13 0.761038 1.230291 -0.672460
13 1980-01-14 0.121675 1.202380 -0.359553
14 1980-01-15 0.443863 -0.387327 -0.813146
15 1980-01-16 0.333674 -0.302303 -1.726283
16 1980-01-17 NaN NaN NaN
17 1980-01-18 -0.205158 NaN -0.401781
18 1980-01-19 0.313068 NaN -1.630198
19 1980-01-20 -0.854096 1.950775 NaN
20 1980-01-21 -2.552990 -0.509652 -0.907298
21 1980-01-22 0.653619 -0.438074 NaN
22 1980-01-23 0.864436 NaN NaN
23 1980-01-24 -0.742165 0.777490 NaN
我想做的就是获得这样的东西:
在X轴上有日期,而在Y轴上有每个测站以及每个测站的日期,因此,当测站具有NaN时,会有一个间隙。 有什么想法吗?
答案 0 :(得分:0)
最好的选择是用其他方式更改NAN,或者完全删除它们。
您可以使用此link here删除NAN值。
答案 1 :(得分:0)
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
np.random.seed(0)
rng = pd.date_range('1980-01-01', periods = 24, freq='MS')
df = pd.DataFrame({'Date':rng,'Stn1':np.random.randn(len(rng)),'Stn2':np.random.randn(len(rng)),'Stn3':np.random.randn(len(rng)) })
df.loc[df['Stn1'] > 1,'Stn1'] = np.nan
df.loc[df['Stn2'] < -1,'Stn2'] = np.nan
df.loc[df['Stn3'] > 0,'Stn3'] = np.nan
df = df.set_index('Date')
for i, col in enumerate(df.columns):
df[col].loc[~df[col].isnull()] = i+1
fig, ax = plt.subplots(1, 1, figsize = (15, 5), dpi=300)
p = sns.pointplot(y=0,x='Date', data=df1, hue='level_1')
ax.set_ylabel('')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles[0:], labels=labels[0:])
fig.autofmt_xdate()
p.set(yticklabels=[]);
输出
答案 2 :(得分:0)
像这样吗?
import matplotlib.pylab as plt
import datetime
from matplotlib import dates as mdates
n = 3 # number of stations
stns = ['']
for i in range(n):
stn = 'Stn' + str(i+1)
stns.append(stn)
df.loc[~np.isnan(df[stn]), stn] = i+1
df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')
dates = df.Date
df.set_index('Date', inplace=True)
ax = df.plot(legend=False)
ax.set_yticks(range(n+1))
ax.set_yticklabels(stns)
ax.set_xticks(range(len(dates)))
ax.set_xticklabels(dates, rotation=45, ha='right')
plt.tight_layout()
plt.show()
有输出