当我绘制图表时,条形图的日期更改为 1970 年代,而叠加的线图保持正确。如果我从 twinx() 开始删除代码,则条形图会以正确的日期范围正确显示。我也尝试从线图中删除 x 参数,但它只会导致线图不显示。
df = filter_df.toPandas()
df2 = df[['date', 'num_changed', '7_day_avg']]
df2['date'] = pd.to_datetime(df2.date).dt.date
df2 = df2.sort_values('date')
plt.figure(figsize=(12.33, 6.5))
ax = sns.barplot(data=df2, x='date', y='num_changed', color='lightgrey')
ax.set_title('Chart title', pad=10)
ax.grid(linestyle='--', axis='y')
ax.spines['right'].set_visible(True)
ax.set_xticklabels(df2.date, rotation=45)
ax.xaxis.set_major_locator(mdates.DayLocator(interval=2))
ax.yaxis.set_label_position("left")
ax.yaxis.tick_left()
ax.set_ylabel('Total 7 Day Change')
ax.set_xlabel('')
ax2 = ax.twinx() # instantiate a second axes that shares the same x-axis
ax2 = sns.lineplot(data=df2, x='date', y='7_day_avg')
ax2.yaxis.set_label_position("right")
ax2.yaxis.tick_right()
ax2.set_ylim(bottom=0)
ax2.set_ylabel('Avg Conc')
ax2.spines['top'].set_visible(False)
plt.show()