Rohit Gavval
Rohit Gavval

Reputation: 247

How to convert a grid-like matplotlib plot to a smooth gradient like surface?

I am trying to plot a 2D numpy array using imshow function from matplotlib.pyplot (it is the plot of a U-matrix generated using the codebook of a self organizing map).

The plot is being generated by the following line:

matplotlib.pyplot.imshow(umat, cmap=plt.cm.get_cmap('RdYlBu_r'), alpha=1)

Here, umat is the 2D numpy array that I am trying to plot. Here is the plot that is generated:

It shows each node (point) as a distinct square.

I would like to generate something like this

,

showing a smooth surface, however I am unable to find a way to do it. Could someone show how it could be done?

Upvotes: 2

Views: 1796

Answers (1)

CodeZero
CodeZero

Reputation: 1689

If you want to use imshow, you can try different interpolation methods, see https://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html

Example: ax.imshow(grid, interpolation=interp_method, cmap='viridis') where interp_method is one of

methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']

Upvotes: 3

Related Questions