Repository navigation
Description
Bug report
Bug summary
An image displayed with NonUniformImage does not respond to scaling the axis with ax.set_xscale('log'), other than changing the axis, not the image itself.
Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import NonUniformImage
# Linear x array for cell centers:
x = np.linspace(1, 10, 10)
# Highly nonlinear x array:
x2 = x**3
# Linear y-array
y = np.linspace(1, 10, 10)
z = np.sqrt(x[np.newaxis, :]**2 + y[:, np.newaxis]**2)
fig, axs = plt.subplots(nrows=2, ncols=2)
fig.subplots_adjust(bottom=0.07, hspace=0.3)
# Uniform Grid, linear x-axis
ax = axs[0, 0]
im = ax.imshow(z, extent=(1, 10, 1, 10), aspect='auto',origin='lower')
ax.set_title("Uniform Grid, linear x-axis")
# Uniform Grid, log x-axis (image changes)
ax = axs[0, 1]
im = ax.imshow(z, extent=(1, 10, 1, 10),aspect='auto',origin='lower')
ax.set_xscale('log')
ax.set_title('Uniform Grid, log x-axis')
# Correct ticklabel formatting
from matplotlib.ticker import StrMethodFormatter, NullFormatter
ax.xaxis.set_major_formatter(StrMethodFormatter('{x:.0f}'))
ax.xaxis.set_minor_formatter(NullFormatter())
# NonUniform Grid, linear x-axis
ax = axs[1, 0]
im = NonUniformImage(ax, interpolation='nearest', extent=(1, 1000, 1, 10))
im.set_data(x2, y, z)
ax.images.append(im)
ax.set_xlim(1, 1000)
ax.set_ylim(1, 10)
ax.set_title('NonUniform Grid, lin x-axis')
# NonUniform Grid, logarithmic x-axis (this doesn't work as intended)
ax = axs[1, 1]
im = NonUniformImage(ax, interpolation='nearest', extent=(1, 1000, 1, 10))
im.set_data(x2, y, z)
ax.images.append(im)
ax.set_xlim(1, 1000)
ax.set_ylim(1, 10)
ax.set_xscale('log')
ax.set_title('NonUniform Grid, log x-axis')
plt.show()Actual outcome
Expected outcome
The bottom panels are made using NonUniformImage. I expected a change similar to the top two panels (made with imshow) when setting ax.set_xscale('log'), however as can be seen in the bottom two panels, the image does not change from the left to the right hand side, only the axis.
The issue is briefly discussed in this link, and suggested by stackoverflow user Aaron to originate from the following:
Basically the problem is that the image is rendered using square coords which are not transformed by the axis transformation, then the bounding box is linearly scaled to axes coords so the data lines up (if the axes coords are also linear). In this case the lack of a second transform misaligns the data. This could be considered a bug that mpl.image._ImageBase does not query the axes transforms for the purpose of aligning all the data, only the bounding box...
Matplotlib version
- Operating system: OS X 10.14
- Matplotlib version: 3.0.2
- Matplotlib backend (
print(matplotlib.get_backend())): Qt5Agg - Python version: Python 3.7.1
Python installed via default conda channel

Activity
jklymak commentedon Feb 15, 2019
What are you trying to do with
NonUniformImagethat can't be done withimshoworpcolormesh? So far as I can seeNonUniformImageis almost completely un-documented.jacobsvensmark commentedon Feb 18, 2019
Thanks for your answer.
imshowassumes a uniform grid of constant spacing in thexandydirections, which I do not have. I have data analogous to the data in the bottom row, where you can see that the width of pixels along thex-axis varies i.e. the grid is non uniform.I was not aware of
pcolormesh, it does, essentially the thing I need, but I need to specify the corners of each pixel, rather than the midpoint of the pixel. So for an image of sizeNx,NyI would need an array ofx-values of sizeNx+1and foryof sizeNy+1. WithNonUniformImageI don't need the extra grid value, I provide the points at which I have data and it assumes some way of finding pixel borders, which is a behaviour I would like.Anyways, the point is that
NonUniformImagecould/should, as far as I can see, meaningfully adjust to the axes when they are set to logarithmic scaling, analogous to other image plotting routines. In the example above I show that it ignores the axes.jklymak commentedon Feb 18, 2019
#9629 does what you want with
pcolormesh; right now you can pass N, M vectors, but then the data gets trimmed, which is suboptimal in my opinion.Skeptical that anyone will get
NonUniformImageto work with log axes. Your better bet for now would be to providepcolormeshwith the appropriate x,y vectors that are N+1, M+1 long.jacobsvensmark commentedon Feb 18, 2019
Alright, thanks for the reference and help.
jklymak commentedon Feb 18, 2019
You're welcome to keep this open - maybe someone else thinks
NonUniformImageshould be kept around and fixed.jacobsvensmark commentedon Feb 18, 2019
Done! Thanks again.
efiring commentedon Feb 18, 2019
This bug is shared by the
PcolorImageused bypcolorfastunder some circumstances. It would be nice to fix it for both of these, since they provide a substantial performance boost compared topcolormesh. Or at a minimum, an exception should be raised when they are used with other than linear scales. Unfortunately, when I looked at this some time ago my impression was that the fix would be non-trivial, and I didn't pursue it.anntzer commentedon Feb 18, 2022
I think the patch to fix this is along the lines of
(i.e. just get the data coordinates corresponding to pixel values). There may just be some off-by-1 or off-by-0.5 things...
github-actions commentedon Jun 1, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!