Skip to content

document that imshow now respects scale #7661

Closed

Description

@PrometheusPi

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

Activity

  1. added this to the milestone on Dec 22, 2016
  2. tacaswell commented on Dec 22, 2016

    @tacaswell
    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

  3. phobson commented on Dec 22, 2016

    @phobson
    Member

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

  4. efiring commented on Dec 22, 2016

    @efiring
    Member

    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.

  5. changed the title [-]imhow with yscale('log') changes scaling with 2.0.0[/-] [+]document that imshow now respects scale[/+] on Dec 22, 2016
  6. added 3 commits that reference this issue on Jan 11, 2017
    6c407a4
    25158c2
    82c4aab
  7. added a commit that references this issue on Jan 16, 2017
    54b120f
  8. added a commit that references this issue on Jan 16, 2017
    cfafd3d
  9. PrometheusPi commented on Feb 15, 2017

    @PrometheusPi
    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.

  10. mforbes commented on Feb 12, 2018

    @mforbes

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions