3

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

user58008
  • 73
  • 7
  • can you share the data set? – Adhun Thalekkara Aug 09 '20 at 07:05
  • 1
    Your dates are coming in as strings. If you want to use locators or formatters, you need to convert to dates. – Jody Klymak Aug 09 '20 at 15:39
  • AdhunThalekkara Here is the file. FYI this does not have the index as dates yet. I did that in code after reading in. https://github.com/Sirly/FishingReports/blob/master/cleanData-daveys.csv @JodyKlymak You are correct. I had converted it to datetime before but saved to csv after cleaning up formatting. Now the graph xtick is in Years from 2000-2020 with all points shoved into the right side. Will edit comment to add image. – user58008 Aug 09 '20 at 16:48

1 Answers1

2

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]])
Zephyr
  • 11,891
  • 53
  • 45
  • 80