Imshow plot for 3d data

Hi everyone,

I have an image like array ( [x,y] with entries between 0 and 255) and I would like to plot it with Imshow.
In matplotlib that’s straight forward, but in SweepMe I’m not sure how to format the output of my custom function or how to hand over the values to my plot settings.
Any ideas, suggestions or workarounds when working with more than single data points?

Many thanks
Sebastian

1 Like

Hi Sebastian,

thanks for posting your question!

The Plot widget of SweepMe! comes with different plot styles and one is also “imshow” as used in matplotlib. However, the Plot widget cannot process data from a single 2d-array yet. It can only process multiple 1d arrays or data points be reshaping them to 2d-array.

There are two workarounds that come to my mind.

  1. Write a CustomFunction script that saves an image to the Temp-folder during the message and return the path to this image. Now, you can use the Widget “Image” to select the variable from your CustomFunction module to show the image.
  2. Write a CustomFunction script that creates a custom widget for the Dashboard. The Add-on module CustomFunction comes with some examples how to create an own widget. The widget could contain your matplotlib plot and display the data as you want. At each measurement step, you handover the data to your CustomFunction script to get an updated image.

With the second solution you are quite flexible, but it might be some work. The GUI example that is shipped with CustomFunction might already help a lot here.

If you need help with creating such scripts, we can also offer to help you. It would be also possible to extend the functionality of the standard Plot widget. In that case please contact us via contact@sweep-me.net as well.

Thanks and good luck with the measurements,
Axel

Hi Axel,

thanks for the quick feedback, it was really helpful. The first solution works just fine. The only issue might be a constant “UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.” In the Debug window, but that’s just convenience.

Thank you again,
Sebastian

1 Like

Hi Sebastian,

thanks for the feedback. Indeed there could be random crashes if the GUI is created outside the main GUI thread. However, as the matplotlib window is not embedded into the SweepMe! UI it might work and you just see the warning.

I do not know how you implemented it, but it could also work if you use draw() instead of show() and the save the image with savefig(). This way, there might be no matplotlib related window created and you can silently save the plots, but this is just a guess.

Best, Axel

Hi Axel,

yes I already went with a really minimalistic approach :

    fig, ax = plt.subplots()
    im = ax.imshow(int_array, cmap = ‘gnuplot2’)
    plt.savefig(save_path)
    plt.close()

    return save_path

The warning already shows for the first line without creating an actual matplotlib window. But it is really not an issue since everything works as intended.

Best,
Sebastian

2 Likes