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

silx.gui.plot: Added support for non-uniform images in matplotlib backend #4153

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

malte-storm
Copy link
Contributor

Added support for matplotlib NonUniformImages in the silx.gui.plot module.

Motivation for this change: When changing for example 2d-integration results from Q/2theta to d-spacing for analysis, the Q/2theta axis becomes non-linear and could not be displayed property for 2d-data.

A new addNonUniformImage(x, y, data) method which follows the syntax of matplotlib's NonUniformImage.set_data(x, y, data) syntax has been added to the PlotWidget with minor refactoring of the addImage to allow reusing code for both addImage and addNonUniformImage.

The change has also been integrated in the Backends (and the OpenGL backend gives a warning and displays plot as "normal" images) and in the ImageData class.

Possible open issue: The addNonUniformImage is not implemented for RGB(A) data, I could not easily find out if this would really be used anywhere.

@t20100
Copy link
Member

t20100 commented Jul 31, 2024

Thanks for the Pull Request!
It would be great to have support for non-uniform images.

However, from a first look at it, it sounds that it will more complicated to fully support non uniform image: We need to make sure all provided data (x and y) can be retrieved from the item, and that all features (displaying the value below the mouse cursor, profile, mask...) fully supports it.

To do so, instead of adding this support to the ImageData, a new plot NonUniformImageData item would be needed (something a bit like https://github.com/silx-kit/silx/blob/main/src/silx/gui/plot/items/image_aggregated.py).
Also, for new items, we tend to not add extra methods to PlotWidget.add*, but rather use PlotWidget.addItem.

Finally, here is a code snippet that uses numpy.meshgrid and a Scatter item to provide a similar feature:

import numpy as np
from silx.gui import qt
from silx.gui.plot import PlotWidget
from silx.gui.plot.items import Scatter

x = [1, 2, 4]
y = [0, 2, 3]
values = np.arange(9).reshape(3, 3)

app = qt.QApplication([])

scatter = Scatter()

scatter_x, scatter_y = np.meshgrid(x, y)
scatter.setData(scatter_x.ravel(), scatter_y.ravel(), values.ravel())
scatter.setVisualization(scatter.Visualization.IRREGULAR_GRID)

plot = PlotWidget()
plot.addItem(scatter)
plot.resetZoom()
plot.show()

app.exec()
image

The Scatter code uses the backend's addTriangles method to render the image:

def addTriangles(self, x, y, triangles, color, alpha):
"""Add a set of triangles.
:param numpy.ndarray x: The data corresponding to the x axis
:param numpy.ndarray y: The data corresponding to the y axis
:param numpy.ndarray triangles: The indices to make triangles
as a (Ntriangle, 3) array
:param numpy.ndarray color: color(s) as (npoints, 4) array
:param float alpha: Opacity as a float in [0., 1.]
:returns: The triangles' unique identifier used by the backend
"""
return object()

That could be also used in a NonUniformImageData item.

@malte-storm
Copy link
Contributor Author

Thanks for the feedback and the suggestion how to display non-uniform images using the scatter method. I will take an in-depth look after the summer break.

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

Successfully merging this pull request may close these issues.

2 participants