matplotlib direct mapping from 2D array to image RGB data - python

I'm looking for away to directly convert my 2D array to the RGB data of matplotlib's matshow() method. What I've acknowledged from the source code is that it uses imshow() method, which sets some hyperparameters and then calls add_image(), in which based on https://github.com/matplotlib/matplotlib/blob/1722bfd6ae4fac707811c8e8dca171138cb5d2a6/lib/matplotlib/axes/_base.py calls append(image). And I'm stuck from this.
So, is there any way to directly map a raw 2D array to image RGB array after matshow() method (with colormap integrated) without calling the plotting?
Edit: In case that my above explanation is hard to understand, I have a 2D matrix (not a grayscale image array). I'm gonna plot it using matshow() with a certain colormap, and vmin & vmax values. I can extract the image pixel values as a 3D array using fig.canvas.show() and np.fromstring() as in here. However my application has very strict time constraint that plotting the data would take too much time (and also very unstable). So instead of plotting (which sequentially call figure(), subplot(), matshow()...) I want to get the 3D image data directly (through some mapping) from my original 2D matrix. I believe it is possible if I understand how pyplot maps the data, but unfortunately I couldn't find the solution in their source code yet.

Related

Layer 2D plots into 3D plot in python

I have a 3D array (created by performing 2D scans at different depths), filled with measurement values. Most values are rather low, while some will be locally higher. I would like to plot these like this in python:
But I have not been able to find a good option for that. Note that the pixel size in XY and Z can be different. Matplotlib would be easiest, but I'm open to other options as well.

How do I plot a contour plot of 3 column data

I have a 3 column irregular data in the format [X Y Z]. I am having difficulty in creating a contourf
plot for the same since it requires one to create a meshgrid and also that data be uniform among the grid.
I need some direction or hint to get started.
I am providing two ways in which you can create a contour/density plot for the data which is in 3-column format and irregular, as you have mentioned.
You can use Mathematica: see the documentation of ListDensityPlot. You can directly provide the data as, ListDensityPlot[{{x1,y1,f1},…,{xk,yk,fk}}], and this will plot the sought density plot.
There is also a simple way to do this in python: You can see the documentation of tricontourf, a module of matplotlib. Its functionality is similar to that of contourf, except that you give 1D arrays rather than the data in mesh grid format.

Given a .jpg of 2D colorplot & colorbar, how can I sample the image to extract numerical data?

I have a .jpg of a 2d colorplot & colorbar, similar to something you might make with matplotlib's pcolormesh().
I'd like to 'reverse engineer' the data from the image. Meaning, I'd like to define some sampling grid on the image, pick the color of that sample, and interpolate it to a point on the color bar to obtain a scalar value of that pixel.
I'm wondering how much of this functionality already exists in some form, and how much I'll need to develop.

Equal bin sizes including border using mayavi imshow?

I am doing a very simple task of plotting a 2d numpy histogram and displaying with with
mayavi.mlab.imshow(my2dhistogram, interpolate=False)
For a 5x5 array the output is the following,
I would like the bins along the border to be the same size as the ones in the center. I understand the logic of what mayavi is doing but for this application I absolutely need the bins to be equal size. This is for a scientific visualization where each bin represents a measurement on a detector surface.
Any suggestions?
I don't know how to do this the right way (it seems like it would be very difficult to get right from what I know about imshow), but I have a conceptual suggestion.
Represent your NxN matrix of items on the surface with an (N+2)x(N+2) matrix and set the border entries to be -1. Then make a customized colormap such that your desired colormap is contained between 0 and 1, with all other entries as (0,0,0,0). I'm not exactly sure how to do that -- iirc mayavi modules don't allow you to setup discontinuous color tables, but you could still hack it in this way. Let me know if the part about the color table is confusing, and I can provide some code to make it work.
Also, is there a reason you need to use mayavi's imshow as opposed to say matplotlib for this essentially 2D problem?

Set color to be used for an array masked values plotted with matplotlib contourf

When I plot an array with masked values, using matplotlib countourf, the masked values appear white. I want them to appear grey.
I tried the set_bad method, but it seems countourf doesn't recognize it (although it recognizes the set_over and set_under methods).
Is there any other method I can use with contourf?
Or will I have to change my code to use imshow, which understands set_bad, instead of countourf?
Have you tried setting first the background, like:
x,y=meshgrid(linspace(0,1),linspace(0,1))
fig=plt.figure()
a=fig.add_subplot(111,axisbg='gray')
z=ma.masked_array(x**2-y**2,mask=y>-x+1)
a.contourf(z)

Categories