Seaborn扩展X轴的历史可以追溯到2000年

时间:2020-01-09 13:11:14

标签: python matplotlib seaborn

我有一个pandas数据框,其中包含从2017-09-01开始的日期(我相信该格式的数据框日期格式正确)。但是X轴急剧扩展。我在LHS上没有任何异常值。

enter image description here

with sns.axes_style('whitegrid'):
g = sns.relplot(x='Date', y='PL', data=daily_PL_withDate_df, height=5, aspect=1.5)

enter image description here

1 个答案:

答案 0 :(得分:3)

熊猫和matplotlib的约会有时不太顺利。您可以按如下所示明确设置xlim:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

N = 700
daily_PL_withDate_df = pd.DataFrame({'Date':pd.date_range('2017-09-01', periods=N),
                                    'PL': np.random.normal(0, 2000, N)})

sns.relplot(x='Date', y='PL', data=daily_PL_withDate_df, height=5, aspect=1.5)

plt.xlim(daily_PL_withDate_df['Date'].iloc[0], daily_PL_withDate_df['Date'].iloc[-1])

plt.show()

resulting plot