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

Colorbar tick placement issues with ImageGrid and LogNorm #15247

Closed
samaloney opened this issue Sep 11, 2019 · 8 comments · Fixed by #15283
Closed

Colorbar tick placement issues with ImageGrid and LogNorm #15247

samaloney opened this issue Sep 11, 2019 · 8 comments · Fixed by #15283

Comments

@samaloney
Copy link

samaloney commented Sep 11, 2019

Bug report

Bug summary

The colorbar tick marks are all compressed ontop of each other for the image which has a LogNorm normalisation applied.

Code for reproduction

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid

x,y = np.ogrid[-4:4:31j,-4:4:31j]
z = 120000*np.exp(-x**2-y**2)

fig = plt.figure()
grid = ImageGrid(fig, 111,  # similar to subplot(141)
                     nrows_ncols=(2, 1),
                     axes_pad=0.05,
                     label_mode='L',
                     cbar_location='right',
                     cbar_mode='each'
                     )

im0 = grid.axes_all[0].imshow(z)
im1 = grid.axes_all[1].imshow(z, norm = LogNorm(z.min(), z.max()))

cb0 = grid.cbar_axes[0].colorbar(im0)
cb1 = grid.cbar_axes[1].colorbar(im1)

plt.show()

Actual outcome

imagegrid_cbar_issue

Expected outcome

The colorbar tick marks would be place correctly as in the the non-lognorm image.

Matplotlib version

  • Operating system: macOS 10.14.6
  • Matplotlib version: 3.1.1
  • Matplotlib backend (print(matplotlib.get_backend())): MacOSX
  • Python version: 3.7.0
  • Jupyter version (if applicable):
  • Other libraries:

pip installed

@anntzer
Copy link
Contributor

anntzer commented Sep 11, 2019

interestingly enough, this happens even after switching to the "normal" colorbar implementation (#13397), even though this doesn't happen for non-imagegrid axes...

@samaloney
Copy link
Author

It seems like it could be related to #12155 as when ImageGrid is called with the above parameters it creates a default colorbar on each row which then has to be updated. I tried to adapt the solutions in #12155 but no luck yet. I also came across #13397 but haven't had the time to install locally to test, I guess now I don't

@samaloney
Copy link
Author

Actually this kinda works cb1 = grid.cbar_axes[1].colorbar(im1, ticks=[0.001,10, 1000]) so seems like there is an extra axis somewhere ...

@ImportanceOfBeingErnest
Copy link
Member

The code also errors out when run interactively with the qt backend and moving the mouse over the axes.

AttributeError: 'Colorbar' object has no attribute 'formatter'
Traceback (most recent call last):
  File "d:\***\lib\matplotlib\cbook\__init__.py", line 196, in process
    func(*args, **kwargs)
  File "d:\***\lib\matplotlib\backend_bases.py", line 2805, in mouse_move
    data_str = a.format_cursor_data(data)
  File "d:\***\lib\matplotlib\image.py", line 972, in format_cursor_data
    + "]")
AttributeError: 'Colorbar' object has no attribute 'formatter'

@zwep
Copy link

zwep commented Sep 17, 2019

Have you tried to set vmin when performing plt.imshow?

You can see an example of this here

https://stackoverflow.com/questions/56439837/colorbar-scaling-in-imagegrid

@samaloney
Copy link
Author

Yea it doesn't help unfortunately

@zwep
Copy link

zwep commented Sep 17, 2019

Have you tried something without Imagegrid? Because I think the problem lies there...

@zwep
Copy link

zwep commented Sep 17, 2019

Here, I tried something without Imagegrid. Works better to say the least

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid

x,y = np.ogrid[-4:4:31j,-4:4:31j]
z = 120000*np.exp(-x**2-y**2)


fig, ax = plt.subplots(2,1)
map_ax0 = ax[0].imshow(z)
map_ax1 = ax[1].imshow(z,  norm=LogNorm(z.min(), z.max()))
plt.colorbar(mappable=map_ax0, ax=ax[0])
plt.colorbar(mappable=map_ax1, ax=ax[1])
plt.tight_layout()

Here is an example

github_alt_imgrid

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

Successfully merging a pull request may close this issue.

5 participants