-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
AxesGrid ticks missing on x-axis #11025
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
Comments
Possibly related to #7378? |
Thanks for the bug report.
|
I would like a workaround, yes. I'm also not afraid of digging in and trying to solve it, but I just wanted to check first what the status is, whether others have already looked into it, whether a fix is at all appreciated, etc. |
Fig.Subplots is the most straightforward workaround. |
Works for me in 2.2.2 and the documentation for that version also seems ok (the only difference from the old versions plot are the direction of the ticks) |
@fredrik-1 compare the x-axis ticks in 1.5.1 to those in 2.2.2. The number of ticks is not right in 2.2.2, a lot are missing. This is how it should be (1.5.1, ignore the color scheme): |
@jklymak Ok, I thought you were thinking of a workaround using AxesGrid. Using regular subplots is not really an option for me, as that would require me to essentially write a new axisgrid toolkit from scratch :) |
egbos, I understand the problem but I don't see it when I run the code myself using 2.2.2 and I don't see it in the 2.2.2 documentation. The same ticks in all xaxis |
@fredrik-1 The problem is not a different number of ticks on different xaxis'es, it's the fact that it has only 0 and 5 on it instead of 0, 2, 4, 6 and 8 as it had before. |
I get 0, 2, 4, 6 and 8 when I make the figure larger. I thought that the problem where that there where more x-tics in the upper plots than in the lower plots. That is probably a bug in some versions of matplotlib. The number of ticks depends on the figure size and I don't see any problem with the behavior I see now. |
Making the figure larger is not a fix for the broken default. When running plots non-interactively (saving to file from a script), it is not an option to resize the figure interactively until it works. Also, interactivity is not reproducible. In any case, even though you don't see any problem, let's agree that it is a change from the v1 default behavior and that I do see this as a problem, since it changes plot scripts from 1.5.1, which is unintended and non-desired behavior. One interesting point you raise is that the upper plot ticks indeed do seem correct... Perhaps this is a hint in the direction of a fix. |
Ok, so the problem is that you think that the x and y ticks should always be the same? I guess that is good in some applications but not in all. edit: I deleted my example because I forgot that imshow show data points and not data values on the axis... |
That's probably because the |
But to be clear, I don't need the x and y ticks to always be the same. In my particular application this is actually what I want, but I can imagine other cases where I'd like it differently. What I want is for the default behavior of mpl v1's axesgrid to return, i.e. that the same formatters/locators are used for the ticks (or at least that's what I assume has changed, I may be mistaken). |
Perhaps what changed is the maximum width of a label in order to prevent overlapping labels? This would explain why the ticks return when you make the figure larger. How to change this maximum width though? |
In general it seems there are several things mixed up in this issue. Number of ticks for single axisThe number of ticks shown is determined by the
So in the case of the example this would look like
resulting in Number of ticks for different subplotsThe other issue is the differing number of ticks between the upper and lower subplot which you observe. I cannot reproduce this. When running the above code or other codes I always see the same number of ticks on all subplots with matplotlib 2.2.2. |
Thanks for that info @ImportanceOfBeingErnest. I will use On the same page, next paragraph, it says:
So, I guess the heuristic could be improved. Indeed, for the example shown on that page (https://matplotlib.org/_images/dflt_style_changes-18.png), the new behavior is clearly much better, but in the case presented here, with such small labels, I would say it is not. I guess this would be a different issue. @ImportanceOfBeingErnest what are your thoughts on this? Is it worth pursuing an improved spacing algorithm or has everything been tried and is this the best we can do? |
Just out of curiosity whats does |
The heuristic is 3*fontsize. Not very sophisticated. |
I was making a small PR just now to maybe spark some discussion :) |
Ok but just be aware that axes_grid1 is poorly maintained and documented. It seems to me you could write a three line wrapper to subplots to do all the above. You also miss out on automated layout like constrained_layout. |
Oh, I didn't know about constrained_layout, thanks for the tip! |
@ImportanceOfBeingErnest yes, fair enough, making different sized axes all have aspect equal is a bit if a chore. But, very curious how often people actually need to make plots like this, and in what fields. For the more usual cases of four same-sized axes, or two different height rows, thats pretty straight forward w/ usual tools. |
https://matplotlib.org/gallery/axes_grid1/simple_axesgrid.html#sphx-glr-gallery-axes-grid1-simple-axesgrid-py doesn't have the reported problem, and playing w/ it manually I cannot reproduce. Can we close this? |
I personally make heavy use of these kinds of plots to compare images of density fields, i.e. slices out of 3D grids. These kinds of gridded field image plots are very typical in astrophysical simulations and I expect in related fields like hydrodynamics, climate science, plasma physics, etc. as well. When comparing such slices, having clean, nicely aligned plots is critical. The noise in the image needs to be minimal and scales need to be comparable. To have this all automated in one package I think must have been the original reason to build it and imho is a reason something like this should continue to exist (whether in axesgrid1 or anywhere else in mpl). Constrained layout looks great, but it still lacks the super convenient gridding options that axesgrid1 offers. Perhaps soon it can be replaced, but not yet, I think :)
It does have the problem, it displays only 0 and 5, whereas imo it should display more numbers. The workaround above (specifying the locator parameters manually) works, but I still think the default behavior caused by the This line of thought led me to #11027. |
@EGPOS I work with gridded data sets all the time. I’m still at a loss as to what you are trying to do that requires the grid aspect ratios be one-to-one, have different limits, but ones that lineup Can you point me to a published real-world example so I can see what you mean? Because the plot made above by @ImportanceOfBeingEarnest seems hard to make sense of the application. |
Aspect ratio doesn't have to be 1:1, that's one of the parameters of axesgrid1. I'm currently working on a paper where I use this (not yet published, sorry ;) ), which is based on my PhD thesis. There I use it a lot. |
I have to download your whole thesis? 😥. How about a couple of examples here? |
No problem :) The most recent figure I was working on (which prompted me to post this issue and do PR #11027) is this one: Another example: |
Notice that these simulations have periodic boundary conditions. Having the figures next to each other as close as possible really helps in comparing the structures in different realizations of the same constrained simulation. |
Closing as this is requesting a change to the default parameters, and we aren't too likely to change those. Note that import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True,
constrained_layout=True);
fig.set_constrained_layout_pads(w_pad=0./72., h_pad=0./72.,
hspace=0., wspace=0.)
for i in range(2):
ax = axs[0, i]
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='RdBu_r')
fig.colorbar(pcm, ax=axs[0,:])
for i in range(2):
ax = axs[1, i]
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis_r')
fig.colorbar(pcm, ax=axs[1,:])
plt.show() Gives essentially what you want above. Where main matplotlib fails, and requires axes_grid1 is when you want the data aspect ratio to be one in the plots. |
Bug report
Bug summary
There seem to be missing ticks on the x-axis for AxesGrids. Looking at the examples in the documentation, this bug seems to have been introduced since version 2.0.0. Compare v2.0.0 to v1.5.1 for instance.
Code for reproduction
The first example in the docs that shows this problem is:
Actual outcome
Outcome for v2.0.0 and higher: https://matplotlib.org/2.0.0/_images/simple_axesgrid1.png
Expected outcome
Outcome for v1.5.1 and before: https://matplotlib.org/1.5.1/_images/simple_axesgrid1.png
Matplotlib version
Seeing as this happens on the matplotlib doc pages themselves, I think we can be pretty sure it's platform / build-system / version (except v1 vs v2) independent.
The text was updated successfully, but these errors were encountered: