0

Maybe this is a recurring problem here. Although I queried old questions on the site, I was unable to solve my problem completely. Some old forum topics helped me a lot:

Show dates on seaborn heatmap

Seaborn Heatmap with Datetime Axes

Date axis in heatmap seaborn

Date axis in heatmap seaborn

But after much trying I still could not figure it out.

My problem is this:

I have a dataframe like this (but the original is much more extensive, being the date index from 2004 to 2018 at one hour intervals).

        Estreito    Furnas  Manso   Itumbiara   Curuai
2004-01-19 15:00:00 NaN NaN 3.73    NaN NaN
2004-01-19 16:00:00 NaN NaN 5.10    NaN NaN
2004-01-19 17:00:00 NaN NaN 5.10    NaN NaN
2004-01-19 18:00:00 NaN NaN NaN     NaN NaN
2004-01-19 19:00:00 NaN NaN 4.31    NaN NaN
2004-01-19 20:00:00 NaN NaN 4.90    NaN NaN
2004-01-19 21:00:00 NaN NaN 5.88    NaN NaN
2004-01-19 22:00:00 NaN NaN 7.06    NaN NaN
2004-01-19 23:00:00 NaN NaN 4.71    NaN NaN
2004-01-20 00:00:00 NaN NaN NaN     NaN NaN
2004-01-20 01:00:00 NaN NaN 7.25    NaN NaN
2004-01-20 02:00:00 NaN NaN 5.10    NaN NaN
2004-01-20 03:00:00 NaN NaN 2.55    NaN NaN
2004-01-20 04:00:00 NaN NaN 0.39    NaN NaN
2004-01-20 05:00:00 NaN NaN 0.20    NaN NaN

I'd like to show the missing data in a graph, so I'm using the heatmap of the seaborn module.

Following the documentation and old forum topics I get in the following script.

import matplotlib.pyplot as plt
import seaborn as sns

df_transposed = df.transpose()

plt.subplots(1,1,figsize=(12,6))

ax = sns.heatmap(df_transposed.isnull(), cbar=False)

x_dates = df.index.strftime('%Y-%m').sort_values().unique()

ax.set_xticklabels(labels=x_dates, rotation=45)

I got the following figure:

enter image description here

But as you can see, the xticklabels are not very good. And reading Seaborn's documentation I could not find any arguments to set the number of xticks.

I tried putting only years on xticklabels doing this:

x_dates = df.index.strftime('%Y').sort_values().unique() 

But it doesn't work as well:

enter image description here

I humbly ask for help to solve this problem. I apologize if I am off topic.

Thank you very much in advance.

0 Answers0