Here is my plot:
<code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.dates as mdates
dates = pd.date_range('2020-03-31', '2024-06-30', freq='Q')
my_df =pd.DataFrame({'dates': dates,
'type_a': [np.nan]*10 + list(range(0, len(dates)-10)),
'type_b': range(len(dates), 2*len(dates))})
my_df = my_df.melt(id_vars='dates', value_vars = ['type_a', 'type_b'])
my_df.dates = my_df.dates.dt.date
gfg = sns.lineplot(x='dates', y='value', hue='variable',
data=my_df, estimator=None)
gfg.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
gfg.set_xticklabels(gfg.get_xticklabels(), rotation=90)
gfg.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
</code>
<code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.dates as mdates
dates = pd.date_range('2020-03-31', '2024-06-30', freq='Q')
my_df =pd.DataFrame({'dates': dates,
'type_a': [np.nan]*10 + list(range(0, len(dates)-10)),
'type_b': range(len(dates), 2*len(dates))})
my_df = my_df.melt(id_vars='dates', value_vars = ['type_a', 'type_b'])
my_df.dates = my_df.dates.dt.date
gfg = sns.lineplot(x='dates', y='value', hue='variable',
data=my_df, estimator=None)
gfg.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
gfg.set_xticklabels(gfg.get_xticklabels(), rotation=90)
gfg.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
</code>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.dates as mdates
dates = pd.date_range('2020-03-31', '2024-06-30', freq='Q')
my_df =pd.DataFrame({'dates': dates,
'type_a': [np.nan]*10 + list(range(0, len(dates)-10)),
'type_b': range(len(dates), 2*len(dates))})
my_df = my_df.melt(id_vars='dates', value_vars = ['type_a', 'type_b'])
my_df.dates = my_df.dates.dt.date
gfg = sns.lineplot(x='dates', y='value', hue='variable',
data=my_df, estimator=None)
gfg.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
gfg.set_xticklabels(gfg.get_xticklabels(), rotation=90)
gfg.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
It does not display dates as they are in dataframe:
I also tried to replace gfg.xaxis.set_major_locator(mdates.MonthLocator(interval=3))
with gfg.xaxis.set_major_locator(mdates.DayLocator(interval=90))
, but it is also not what I am looking for.
How can I get dates exactly as in dataframe?
5