我有一个索引为日期时间的数据框。有重复的索引。这里是df.head()
Landing Date Boat Trip Type Anglers ... Albacore Barracuda Total Caught
Date
2020-01-01 daveys-locker 2020-01-01 Freelance 3/4 Day 55 ... 0 0 223
2020-01-01 daveys-locker 2020-01-01 Western Pride 1/2 Day PM 38 ... 0 0 137
2020-01-02 daveys-locker 2020-01-02 Freelance 3/4 Day 75 ... 0 0 185
2020-01-02 daveys-locker 2020-01-02 Western Pride 1/2 Day PM 38 ... 0 0 144
2020-01-03 daveys-locker 2020-01-03 Freelance 3/4 Day 77 ... 0 0 395
我已经尝试了几种方法来让xticks
每天或每个索引都不显示(甚至无法分辨,因为有这么多)。这是我的原件。
fig, ax = plt.subplots(figsize=(40,25))
chart = sbn.scatterplot(x='Date',y='Total Caught',data=daveysdf_2019, hue='Boat', style='Trip Type',s=150)
ax.set_title('Daveys Locker 2019 Totals')
ax.legend(framealpha=0.5)
chart.set_xticklabels(labels=daveysdf_2019.Date.unique(),rotation=75)
figure = chart.get_figure()
我尝试使用set_major_locator
with matplotlib.dates.MonthLocator
,formatter
但最终没有显示任何xticks
from matplotlib.dates import MonthLocator, DateFormatter
fig, ax = plt.subplots(figsize=(40,25))
chart = sbn.scatterplot(x='Date',y='Total Caught',data=daveysdf_2019, hue='Boat', style='Trip Type',s=150)
ax.set_title('Daveys Locker 2019 Totals')
ax.legend(framealpha=0.5)
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m'))
chart.set_xticklabels(labels=daveysdf_2019.Date.unique(),rotation=75)
我还尝试了其他一些我不记得的东西,它使xtick
间隔有所间隔,但仅从第一次约会到 2 月中旬。
编辑:将 Date 列转换为 datetime 后,这是新图表的样子。如果我使用 set_major_locator/formatter 点在右边,但 xticks 被重置为每天并重叠。