Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get xticks to have a monthly interval instead of daily on my matplotlib plot

I have a dataframe with the index as datetime. There are duplicate indices. Here is the 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

I've tried a few ways to get the xticks not to show every day or every index (can't even tell because there are so many). Here was my original.

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()

enter image description here

I tried using set_major_locator with matplotlib.dates.MonthLocator and formatter but that wound up not showing any 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)

enter image description here

I also tried something else that I can't remember and it made the xtick interval spaced out but only from the first date up through mid Feb.

Edit: After converting the Date column to datetime here is what the new graph looks like. If I use set_major_locator/formatter the points are on the right but the xticks are reset to every day and overlapping.

enter image description here

like image 298
user58008 Avatar asked Sep 21 '25 11:09

user58008


1 Answers

You code is correct:

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)

But you need to add one line at the end of this code to avoid matplotlib not show any ticks, you need to set xlim of the axis with:

ax.set_xlim([daveysdf_2019['Date'].iloc[0], daveysdf_2019['Date'].iloc[-1]])
like image 129
Zephyr Avatar answered Sep 23 '25 01:09

Zephyr