I am plotting a joyplot where the x-axis is a range of dates. However, joyplot only accepts dates arguments as numbers... As a result, the chart is plotted correctly, but the x-axis displays dates as their serial number, which is not very friendly to the end user.
I have tried various methods to format the axis (like this one: Set x axis labels for joyplot), but none worked so far!
How can I format the x-axis so that it displays dates in the 'mm.yyyy' format (eg.: 01.2022)?
I am using the following database to plot this example (saved as a .csv to my computer): https://www.boxofficemojo.com/genre/sg4242469121/?ref_=bo_gs_table_24
Table example (copy it and paste in a .csv file, naming it "Example", as in code below):
Title | Release Date | Date | Distributor |
---|---|---|---|
Incredibles 2 | Jun 15, 2018 | 43252 | Walt Disney Studios Motion Pictures |
The Lion King | Jul 19, 2019 | 43647 | Walt Disney Studios Motion Pictures |
Finding Dory | Jun 17, 2016 | 42522 | Walt Disney Studios Motion Pictures |
Frozen II | Nov 22, 2019 | 43770 | Walt Disney Studios Motion Pictures |
Shrek 2 | May 19, 2004 | 38108 | DreamWorks |
Toy Story 4 | Jun 21, 2019 | 43617 | Walt Disney Studios Motion Pictures |
Toy Story 3 | Jun 18, 2010 | 40330 | Walt Disney Studios Motion Pictures |
Frozen | Nov 22, 2013 | 41579 | Walt Disney Studios Motion Pictures |
The Secret Life of Pets | Jul 8, 2016 | 42552 | Universal Pictures |
Despicable Me 2 | Jul 3, 2013 | 41456 | Universal Pictures |
Inside Out | Jun 19, 2015 | 42156 | Walt Disney Studios Motion Pictures |
Zootopia | Mar 4, 2016 | 42430 | Walt Disney Studios Motion Pictures |
Finding Nemo | May 30, 2003 | 37742 | Walt Disney Studios Motion Pictures |
Minions | Jul 10, 2015 | 42186 | Universal Pictures |
Shrek the Third | May 18, 2007 | 39203 | DreamWorks |
The Lion King | Jun 15, 1994 | 34486 | Walt Disney Studios Motion Pictures |
Up | May 29, 2009 | 39934 | Walt Disney Studios Motion Pictures |
Minions: The Rise of Gru | Jul 1, 2022 | 44743 | Universal Pictures |
The Grinch | Nov 9, 2018 | 43405 | Universal Pictures |
Sing | Dec 21, 2016 | 42705 | Universal Pictures |
Monsters University | Jun 21, 2013 | 41426 | Walt Disney Studios Motion Pictures |
Code:
import joypy
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv(r'C:\Users\Downloads\Example.csv', delimiter=';') #replace file name and directory with your file, created from the table above
print(df.shape)
df.head()
fig, ax = joypy.joyplot(df,
by = 'Distributor',
column='Date',
colormap=cm.autumn,
figsize = (10,6),
fade = True)
Output plot:
First, please accept that the x-axis tick values in my answer may not be correct; it is unclear what the value obtained by ax[-1].get_xticks()
indicates. Assuming it represents days, I can change it to a date in the following way: since the time series in maplotlib is Gregorian standard, I convert it from a number to a date.
df['Release Date'] = pd.to_datetime(df['Release Date'], format='%b %d, %Y')
import joypy
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.dates as mdates
%matplotlib inline
fig, ax = joypy.joyplot(df,
by = 'Distributor',
column='Date',
colormap=cm.autumn,
figsize = (10,6),
xrot=45,
fade = True)
print(ax[-1].get_xticks())
new_dates = [mdates.num2date(x + mdates.date2num(np.datetime64('1900-01-01'))) for x in ax[-1].get_xticks()]
new_dates = [t.strftime('%b-%Y') for t in new_dates]
ax[-1].set_xticklabels(new_dates)
fig.show()