Building an irregular RGB image in Python

I want to build multiple RGB images on one axis set using matplotlib. At first I tried using imshow for this, but it doesn't seem to be processing two images on the same set of axes with different extents (when I draw the second, it disappears first). I think the answer here is to use pcolormesh, as in How to build an irregular spaced RGB image using python and a base map?

However, this does not work for me - the color coming from the displayed one (i.e. the data array that I pass to pcolormesh and the specified cmap) overrides the color of the face that I specify. The edges of the mesh have the correct color, but not the edges.

Does anyone know how to directly set the complexion? Ideally, I would use pcolormesh, which takes an n * m * 3 array as an alternative to n * m, just like imshow ...

Minimal example of how I would like to work: import numpy as np import matplotlib.pyplot as plt

#make some sample data x, y = np.meshgrid(np.linspace(0,255,1),np.linspace(0,255,1)) r, g = np.meshgrid(np.linspace(0,255,100),np.linspace(0,255,120)) b=255-r #this is now an RGB array, 100x100x3 that I want to display rgb = np.array([r,g,b]).T m = plt.pcolormesh(x, y, rgb/255.0, linewidth=0) plt.show() 

The problem is that the call to plt.pcolormesh fails,

 numRows, numCols = C.shape ValueError: too many values to unpack 

I guess this is because it needs a 2D array, not a three-dimensional array with a last size of 3 long.

+2
source share
2 answers

The solution was very simple: the only thing I needed to do was not the question that I linked to remove the array from the mesh object. A minimal example if it is useful to others:

 import numpy as np import matplotlib.pyplot as plt #make some sample data r, g = np.meshgrid(np.linspace(0,255,100),np.linspace(0,255,100)) b=255-r #this is now an RGB array, 100x100x3 that I want to display rgb = np.array([r,g,b]).T color_tuple = rgb.transpose((1,0,2)).reshape((rgb.shape[0]*rgb.shape[1],rgb.shape[2]))/255.0 m = plt.pcolormesh(r, color=color_tuple, linewidth=0) m.set_array(None) plt.show() 

I assume that the color_tuple string may not be obvious: essentially we need to turn the array (n, m, 3) into an array (n * m, 3). I think transposing is necessary so that everything matches correctly. It is also worth noting that the colors we transmit must be a floating point, between 0 and 1.

+4
source

How to combine all your images into one big numpy array, which you then show with imshow?

-2
source

Source: https://habr.com/ru/post/1263648/


All Articles