TwoSlopeNorm in Matplotlib not working as expected

I am trying to create a plot with a diverging colour map which is not symmetric around zero.

In this example, the DivergingNorm function is used and produces what I want…

I am using a later version of Matplotlib however (3.5.1) and when I use the suggested code in the link above, I get the following image…

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((10,10))
data = 10 * (data - 0.8)

fig, ax = plt.subplots()
im = ax.imshow(data, norm=matplotlib.colors.TwoSlopeNorm(0), cmap=plt.cm.seismic, interpolation='none')
fig.colorbar(im)
plt.show()

image

Does anyone know how I can reproduce this behaviour from DivergingNorm from older Matplotlib verions? I can’t find a solution to this anywhere even though the older behaviour of ‘DivergingNorm’ is exactly what I want.

I get the same wrong behaviour using this example —> python - Seaborn plot with colorbar, centered around 0 - Stack Overflow

I should get this…

image

… but actually get…

image

Thanks!

FIX FOUND

as noted here —> [Bug]: TwoSlopeNorm behaves like CenteredNorm · Issue #22197 · matplotlib/matplotlib · GitHub

the solution is do this…

cb.ax.set_yscale('linear')

… in my case…

cb.ax.set_xscale('linear')

:slight_smile:

props to the kind person on stack overflow for pointing this out! —> https://stackoverflow.com/questions/74595690/twoslopenorm-in-matplotlib-not-working-as-expected?noredirect=1#comment131692930_74595690