Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve layout of plots with DateTimeIndex #1360

Closed
rpgoldman opened this issue Aug 21, 2020 · 9 comments
Closed

Improve layout of plots with DateTimeIndex #1360

rpgoldman opened this issue Aug 21, 2020 · 9 comments
Labels
Beginner Enhancement Improvements to ArviZ

Comments

@rpgoldman
Copy link
Contributor

Tell us about it

Plots with dates on their x axis can be typeset poorly. Here is an example from https://docs.pymc.io/notebooks/getting_started.html

image

The x coordinate here is a DateTimeIndex:

{'date': DatetimeIndex(['2008-05-02', '2008-05-05', '2008-05-06', '2008-05-07',
                '2008-05-08', '2008-05-09', '2008-05-12', '2008-05-13',
                '2008-05-14', '2008-05-15',
                ...
                '2019-11-01', '2019-11-04', '2019-11-05', '2019-11-06',
                '2019-11-07', '2019-11-08', '2019-11-11', '2019-11-12',
                '2019-11-13', '2019-11-14'],
               dtype='datetime64[ns]', name='Date', length=2906, freq=None)}

Looking at the web, I see that there are techniques for better formatting such axes: https://www.delftstack.com/howto/matplotlib/how-to-rotate-x-axis-tick-label-text-in-matplotlib/#fig-autofmt-xdate-rotation-to-rotate-xticks-label-text

Revised using a rotation argument for the xticklabels:

image

Thoughts on implementation

We look at the label of the x axis, and if it is a DateTimeIndex, we automatically use one of these techniques.

Note: this advice applies only to the matplotlib back-end -- I don't know enough about bokeh to know if there is a corresponding technique there.

@rpgoldman
Copy link
Contributor Author

P.S. to fix this I had to peek into the plots in a very clumsy way, which might be another argument for returning a DataArray of axes, instead of a numpy array of them:

with disaster_model:
    axes_arr = az.plot_trace(trace)
plt.draw() # this forces population of the xticklabels so that the get_text() works later.
for ax in axes_arr.flatten():
    if ax.get_title() == 'switchpoint': # find the right axes
        labels = [label.get_text() for label in ax.get_xticklabels()] # collect the existing labels.
        ax.set_xticklabels(labels, rotation=45, ha='right') # rotation here
        break
plt.draw()      

This also shows it's harder to fix after the fact, than have ArviZ do it right to begin with!

@aloctavodia
Copy link
Contributor

This used to work

ax = az.plot_trace(trace);
[ticks.set_rotation(45) for ticks in ax[0, 0].get_xticklabels()]

but now az.plot_trace return something else, right @agustinaarroyuelo ?

This problem could happen with long labels not necessarily DateTimeIndex. So we should work on a general solution at the matplotlib level, or revert what az.plot_trace returns.

@aloctavodia
Copy link
Contributor

Yeah, we should fix az.plot_trace, currently is only returning the last axes instead of all of them. Making further tweaking of the plot not possible. @agustinaarroyuelo Could you check this?

@agustinaarroyuelo
Copy link
Contributor

Hi! I am taking a look at this right now. Meanwhile, this should work:

ax = az.plot_trace(trace_cat)
[ticks.set_rotation(45) for ticks in ax.figure.axes[0].get_xticklabels()]

@abhisht51
Copy link
Contributor

Hi! Can i take this up ?

@ahartikainen
Copy link
Contributor

I'm not sure if we should check datetime stuff. Or would it be ok to just call pandas datetime autoformat.

We really should have an easy way to change the defaults if user wants it.

@aloctavodia
Copy link
Contributor

#1361 will allow this to work again.

ax = az.plot_trace(trace)
[ticks.set_rotation(45) for ticks in ax.figure.axes[0].get_xticklabels()]

I think this should be enough, as allow full control on the user side. Unless someone offers a general default solution (not only for dates).

@ahartikainen
Copy link
Contributor

@OriolAbril
Copy link
Member

Hi @abhisht51! Are you searching for matplotlib related issues? Or more general plotting issues? ArviZ has both matplotlib and bokeh backends. If you prefer we can also talk in Gitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Beginner Enhancement Improvements to ArviZ
Projects
None yet
Development

No branches or pull requests

6 participants