I need to visualize some data. It’s basic 2D grid, where each cell have float value. I know how to e.g. assign color to value and paint grid in OpenCV. But the point here is that there are so many values so it’s nearly impossible to do that. I am looking for some method, where I could use gradient. For example value -5.0 will be represented by blue, 0 – black, and +5.0 as red. Is there any way to do that in Python?
Here is sample data I am talking about
A B C D
A -1.045 2.0 3.5 -4.890
B -5.678 3.2 2.89 5.78
Matplotlib has the
imshowmethod for plotting arrays:This is what it looks like:
The details for the color bar setup were taken from a matplotlib example: colorbar_only.py. It explains that the number of
boundariesneed to be one larger then then number of colors.EDIT
You should note, that
imshowaccepts theoriginkeyword, which sets the where the first point is assigned. The default is ‘upper left’, which is why in my posted plot the y axis has 0 in the upper left and 99 (not shown) in the lower left. The alternative is to setorigin="lower", so that first point is plotted in the lower left corner.EDIT 2
If you want a gradient and not a discrete color map, make a color map by linearly interpolating through a series of colors:
This produces:

EDIT 3
To add a grid, as shown in this example, use the
gridmethod. Setting the grid color to ‘white’ works well with the colors used by the colormap (ie the default black does not show up well).Including this before the

savefigcall produces this plot (made using 11×11 grid for clarity):There are many options for
grid, which are described in the matplotlib documentation. One you might be interested in islinewidth.