简体   繁体   English

如何清理 x 轴?

[英]How do I clean up the x axis?

I have tried plt.gcf().autofmt_xdate() but that doesn't fix the overlapping dates on the x axis.我试过plt.gcf().autofmt_xdate()但这并不能修复 x 轴上的重叠日期。 How do I clean the x axis to every week instead of everyday?如何每周而不是每天清洁 x 轴?

# Convert string column into date
df['date'] = pd.to_datetime(df['date'], format = "%Y-%m-%d")

plt.figure(figsize=(14,10))
plt.title("Daily Tests")
plt.ylabel("Number of confirmed cases")
plt.xlabel("Date")
sns.barplot(x=df['date'], y=df['confirmed'])
plt.show()

The graph:图表: 在此处输入图片说明

any suggestions would be appreciated.任何建议,将不胜感激。

Set the interval of mdates.DayLocator(interval=14) in order to control the time series data of x-axis.设置mdates.DayLocator(interval=14)的间隔,以控制x轴的时间序列数据。 In this case, it is set to two weeks.在这种情况下,它被设置为两周。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig = plt.figure(figsize=(14,10))
ax = fig.add_subplot(111)

sns.barplot(x=df['date'], y=df['confirmed'], ax=ax)

ax.set_title("Daily Tests")
ax.set_ylabel("Number of confirmed cases")
ax.set_xlabel("Date")

days = mdates.DayLocator(interval=14)
days_fmt = mdates.DateFormatter('%m-%d')
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(days_fmt)

plt.show()

在此处输入图片说明

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

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