5

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...

Community
  • 1
  • 1
Nigu
  • 2,025
  • 2
  • 22
  • 31
  • 2
    Can you include (toy)data in your example that reproduces the problem? – thomas Dec 07 '15 at 10:46
  • 1
    Could you edit your code to include tick_locator? A fully functioning example is far more likely to encourage people to try to solve your problem… – urschrei Dec 07 '15 at 17:56
  • Using `im0 = ax0.imshow(im2, aspect='auto',extent=[-0.15,0.15,0,4])` seems to give nice result for me: this is not what you are looking for? – stellasia Dec 07 '15 at 19:17
  • @stellasia Yes. Yes it is. Silly me. How can I give you the bounty? – Nigu Dec 07 '15 at 19:47
  • @urschrei. Sorry, while stripping the code down, I also deleted that line by accident. That variable was already loaded in my Canopy session, so it didn't complain when I checked it. Will edit it now... – Nigu Dec 07 '15 at 19:48
  • 1
    @Nigu, great! I have transformed my comment into an answer, feel free to accept it if you are happy with this solution. Thanks! – stellasia Dec 07 '15 at 20:08

1 Answers1

1

With:

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

which produces the following result:

enter image description here

stellasia
  • 5,372
  • 4
  • 23
  • 43