Thanks to visit codestin.com
Credit goes to github.com

Skip to content

document that imshow now respects scale #7661

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

Closed
PrometheusPi opened this issue Dec 22, 2016 · 5 comments
Closed

document that imshow now respects scale #7661

PrometheusPi opened this issue Dec 22, 2016 · 5 comments
Milestone

Comments

@PrometheusPi
Copy link

PrometheusPi commented Dec 22, 2016

With the introduction of matplotlib 2.0.0 using plt.imshow() together with plt.yscale('log') changed its behavior.

The following example code:

import numpy as np
import matplotlib.pyplot as plt

tmp = np.random.random((5,5))
plt.imshow(tmp, aspect="auto", extent=(0,5, 1e0, 1e5), interpolation='nearest')
plt.yscale("log")

plt.show()

produces with python 3.4.3 and matplotlib 1.5.1
plt_imshow_2

and the following warning:

../.local/lib/python3.4/site-packages/matplotlib/image.py:375: UserWarning: Images are not supported on non-linear axes.
  warnings.warn("Images are not supported on non-linear axes.")

With python 3.4.3 and matplotlib 2.0.0rc1 it produces
index2

Thus, 2.0.0 assumes that underneath the linear bin id of the numpy array tmp lies a linear data extend (y-axis) scaling.

If however my logarithmic extent is mapped correctly onto the linear bin id of the numpy array - the old behavior is exactly what represents the data.

Is it possible to reproduce the old behavior without relabeling the y-axis by hand?
Is there an option, the tell imshow directly that a specify data axis is already log-scaled?

and if so:
Is it possible to have the same behavior independent of the matplotlib version used?

System:
Ubuntu 14.04.5 - Linux-Kernel 4.4.0-38
Matplotlib and Python are available via the HPC module system (probably via pip)

cc'ing: @s0vereign

@tacaswell
Copy link
Member

To be clear, you were relying on a bug / undefined behavior. The new behavior is in arguably correct.

I think you want to be using http://matplotlib.org/examples/pylab_examples/image_nonuniform.html in 2.0+ but not sure if that will work as expected in <2.0.

I think pcolor and friends will work in all cases, see http://matplotlib.org/examples/pylab_examples/pcolor_demo.html

@phobson
Copy link
Member

phobson commented Dec 22, 2016

Agree with @tacaswell about pcolor. This works in 1.5 and the 2.0 RC:
http://stackoverflow.com/a/20844584/1552748

@efiring
Copy link
Member

efiring commented Dec 22, 2016

Don't use pcolor--it's slow, and has no advantages in most cases. Use pcolormesh. Supply the cell boundaries explicitly:

import numpy as np
import matplotlib.pyplot as plt

tmp = np.arange(30).reshape(5, 6)
x = np.linspace(0, 6, 7)
y = 10**np.linspace(0, 5, 6)
X, Y = np.meshgrid(x, y)
fig, ax = plt.subplots()
ax.set_yscale('log')
ax.pcolormesh(x, y, tmp)
ax.autoscale('tight')

plt.show()

It should be possible to do this more efficiently with Axes.pcolorfast, but it doesn't work with a log scale. That's a bug.
When testing this sort of thing, use a rectangular array (not a square), and a regular pattern, so you can be sure you are getting the orientation the way you intend it.

@tacaswell tacaswell changed the title imhow with yscale('log') changes scaling with 2.0.0 document that imshow now respects scale Dec 22, 2016
phobson added a commit to phobson/matplotlib that referenced this issue Jan 11, 2017
phobson added a commit to phobson/matplotlib that referenced this issue Jan 11, 2017
efiring added a commit that referenced this issue Jan 16, 2017
DOC: explain non-linear scales and imshow (closes #7661)
tacaswell pushed a commit that referenced this issue Jan 16, 2017
DOC: explain non-linear scales and imshow (closes #7661)
@PrometheusPi
Copy link
Author

Thanks a lot for your help. I finally had the time to implement the pcolormesh approach in our code. It works like a charm.

@mforbes
Copy link

mforbes commented Feb 12, 2018

What about when loading an RGB image from file? pcolormesh does not obviously accept RGB data...

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

No branches or pull requests

5 participants