当我绘制图形时,条形图的日期更改为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()

数据示例如下:

发布于 2021-02-20 00:28:39
事实证明,这是一个混合了seaborn图表和matplotlib格式的问题。一旦我删除了seaborn图表调用并使用了ax.bar()和ax2.plot(),这个问题就解决了。
https://stackoverflow.com/questions/66265384
复制相似问题