How to turn off interpolation in pcolormesh()? - python

I've noticed pcolormesh by default interpolates/smoothes the raw data. How do I turn this off?

Actually it's not invalid. pcolor and pcolormesh are certainly interpolating!
Use pcolor to plot a 4x4 matrix and you will get a 3x3 output.
pcolor is not plotting each matrix entry, but each "output value" is an interpolation of the four surrounding values of the original matrix.
And there is no way to turn this off.

Related

Density Plot From 3D Array In Python

For some reason I cannot find any examples online for what I'm trying to do.
I have an array where each point is [x,y,f(x,y)]. The x,y values cover the 2d space evenly and without repeating.
I want to create a 2D density plot where each point is colored according to its z value.
If I plot this data in Mathematica as a 3D surface and look at it from above, it looks like this: https://imgur.com/a/m4Rt0ui
I would like to make this, except 2D and in python.

In python, is it possible to plot colour map by giving coordinates and function value?

On the web, the only solution I find to plot the values of a two-dimensional function is to have a file with the matrix A(nxm) of the function's values and use matplotlib imshow. This will produce a map, whose extent (dimension along axes) has to be known and explicitly indicated.
Now, my concern arises from the fact that I need to plot two maps on the same axis, but one is slanted with respect to the other. So to say, I have a large main rectangular map and a smaller one that is oblique and superimposed.
In the end, the question is: is it possible to plot 2d maps starting from a file that is written as x, y, f(x,y) rather than just using f(x,y) so the plotting tool knows exactly where to draw?
I luckily found what I was looking for in pcolormesh that can draw a map given the coordinates of each point and its "colour"-value.

matplotlib direct mapping from 2D array to image RGB data

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.

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)

pcolor in matplotlib

I am using pcolor in matplotlib. I use a three dimensional array as the input. I want to arrange the third array to be displayed in a log scale. Is there an option to do this? If I take a logarithm of the third array before plotting it, the sub one values become negative and pcolor no longer works.

Categories