Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - two different colormaps with different ranges

I'm trying to combine two figures that were separate before. One is a 3 panel figure (ax1,ax2,ax3) (all generated with imshow), where I was using one single colormap on the side. Now, I want to add another figure (ax0) that I load from a png file, using imread and get_sample_data (from matplotlib.cbook).

The problem is that this new figure takes the same colormap as the one from the 3 panels, thus yielding one simple uniform color in the newly added panel. The 3 other panels still have the right color.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

g1 = gridspec.GridSpec(4, 1, height_ratios=[4,1,1,1])
g1.update(wspace=0.05, hspace=0.2) # set the spacing between axes.

f, ((ax0), (ax1), (ax2), (ax3)) = plt.subplots(4, 1, sharex='col', sharey='row')

ax0 = subplot(g1[0])
ax1 = subplot(g1[1])
ax2 = subplot(g1[2])
ax3 = subplot(g1[3])

from matplotlib.cbook import get_sample_data
im2 = plt.imread(get_sample_data('PathToFig/Figure.png'))
ax0.imshow(im2, vmax=1)

ziAA = np.random.rand(4, 4, 4)
ziAB = np.random.rand(4, 4, 4)
ziBA = np.random.rand(4, 4, 4)

ax1.imshow(ziAA,origin="lower",vmin=0,vmax=0.03,aspect="auto",extent=[-0.15,0.15,0,4])
ax2.imshow(ziAB,origin="lower",vmin=0,vmax=0.03,aspect="auto",extent=[-0.15,0.15,0,4])
im = ax3.imshow(ziBA,origin="lower",vmin=0,vmax=0.03,aspect="auto",extent=[-0.15,0.15,0,4])

from matplotlib import ticker
tick_locator = ticker.MaxNLocator(nbins=5)

f.subplots_adjust(right=0.85)

cbar_ax = f.add_axes([1.0, 0.15, 0.01, 0.7])
cbar = f.colorbar(im, cax=cbar_ax)

cbar.locator = tick_locator
cbar.update_ticks()
cbar.solids.set_rasterized(True)
cbar.solids.set_edgecolor("face")

ziAB, ziBA and ziAA were generated in a previous griddata interpolation call.

I tried specifying two different colormaps inside each imshow call, I tried changing the values of vmax. But to no avail...

If I put ax0 after ax1-3, then, it's ax0 who gets the right colors, and not ax1-3.

I've looked at the other Similar Questions (Two different color colormaps in the same imshow matplotlib) that talk about creating masked arrays or my own colormap, but because of the png file origin for ax0, I don't really see how I should proceed.

EDIT :

Toy data:

ziAA = np.random.rand(4, 4, 4)
ziAB = np.random.rand(4, 4, 4)
ziBA = np.random.rand(4, 4, 4)

Toy png figure:

Toy image

With this figure, the uniform color turns out to be white. My actual figure gives uniform red. This suggests that by properly tuning vmin, vmax and maybe other control parameters, one might get the png figure to display properly. However, I'd be interested in something that would work for any png figure...

like image 488
Nigu Avatar asked Nov 10 '22 00:11

Nigu


1 Answers

With:

im0 = ax0.imshow(im2, aspect='auto',extent=[-0.15,0.15,0,4])

which produces the following result:

enter image description here

like image 140
stellasia Avatar answered Nov 15 '22 06:11

stellasia