Home > OS >  vcenter location of matplotlib pcolormesh with TwoSlopeNorm
vcenter location of matplotlib pcolormesh with TwoSlopeNorm

Time:09-06

If I make a pcolormesh plot with a diverging colormap and TwoSlopeNorm to keep 0 in the middle of the colormap (because vmax > abs(vmin)), the location of vcenter changes between matplotlib version 3.4.3 to 3.5.2. An example code is:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors

fld = np.random.random((10,10)) - 0.2

norm = colors.TwoSlopeNorm(vmin=-0.2, vcenter=0, vmax=0.8)

plt.pcolormesh(fld, norm=norm, cmap='bwr')
plt.colorbar()
plt.show()

With matplotlib 3.4.3, the ticks remain equally spaced between vmin and vmax (matplotlib 3.4.3. However, with matplotlib 3.5.2 vcenter is forced to be the middle of the colorbar (matplotlib 3.5.2), so that the tick spacing between vmin and vcenter is different than the spacing between vcenter and vmax. What can I do with matplotlib 3.5.2 to keep the tick spacing constant, instead of forcing vcenter to be the middle of the colorbar. Essentially, how to get the result of matplotlib 3.5.2 to look as matplotlib 3.4.2

CodePudding user response:

You are using a non-linear Norm, so the colorbar has a non-linear scale (consistent with, for instance a LogNorm). The old behaviour can be retrieved by setting the colorbar axes to be linear:

...
cb = plt.colorbar()
cb.ax.set_yscale('linear')
...
  • Related