Why setting fixed colorbar failed in this case?

77 views Asked by At

I am trying to make a bunch of polar view plots using the same colorbar. However, the colorbars differ after setting the plotting limits. In the code snippet below, I randomly created 5 maps but plotted in a fixed range, but the output figures are still different in colorbar.

from numpy import linspace, pi, ndarray, random
import matplotlib
matplotlib.use('Agg')
from matplotlib.pyplot import figure

lon = linspace(start=0, stop=2*pi, num=100)
colat = linspace(start=0, stop=9, num=10)
emission = ndarray(shape=(10, 100, 5), dtype=float)
for t in range(5):
    emission[:, :, t] = random.rand(10, 100)

fig = figure(num='emission', figsize=(15, 15))
em_pos = [0.05, 0.1, 0.8, 0.8]
emc_pos = [0.9, 0.1, 0.05, 0.8]
for t in range(5):
    fig.clear()

    ax = fig.add_subplot(121, polar=True, position=em_pos)
    axcont = ax.contourf(lon, colat, emission[:, :, t], vmin=0, vmax=2)

    axc = fig.add_subplot(122, position=emc_pos)
    fig.colorbar(mappable=axcont, cax=axc)

    fig.savefig(fname='emission{0:d}.png'.format(t), format='png')
1

There are 1 answers

0
Haonan Wu On

The problem seems to be solved. It is not a problem of colorbar, but a problem of contourf. When I replaced

ax.contourf(lon, colat, emission[:, :, t], vmin=0, vmax=2)

with

ax.pcolormesh(lon, colat, emission[:, :, t], vmin=0, vmax=2)

Then the colorbar shows the proper range. Indeed it is not a full solution, pcolormesh differs in some aspects from contourf, but it meets my needs.