2

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

Example from https://stackoverflow.com/a/20146989/6288682

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()

TwoSlopeNorm not producing what I want

... which is clearly not right.

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 ---> https://stackoverflow.com/a/69707735/6288682

I should get this...

enter image description here

... but actually get...

enter image description here

Thanks!

jonnyhtw
  • 61
  • 8
  • 1
    The norm is still working as expected. What has changed, is the display of the colorbar. I don't know whether this change in behavior is intended by matplotlib's maintainers, or is a side effect of some refactoring. Anyway, previously the ticks would be shown in a linear way and the colors got distorted accordingly. Now, the colors are linear and the ticks are distorted. (There also seems to be a difference in showing the highest and lowest values, 2 and -8, when they are just outside the data range.) I was expecting the `spacing='uniform'`parameter would make a difference, but it doesn't. – JohanC Nov 28 '22 at 07:30
  • thanks for your comment @JohanC. it would be interesting to know if the maintainers intended this behaviour. – jonnyhtw Nov 28 '22 at 23:29
  • 1
    You might check the issues at [github](https://github.com/matplotlib/matplotlib/issues), and add a new one if it isn't already mentioned. – JohanC Nov 28 '22 at 23:34
  • thanks @JohanC. i've just asked a question on the matplotlib discourse and will raise a github ticket if nothing comes of that. thanks ---> https://discourse.matplotlib.org/t/twoslopenorm-in-matplotlib-not-working-as-expected/23350 – jonnyhtw Nov 28 '22 at 23:35
  • 4
    It seems already to be in discussion: https://github.com/matplotlib/matplotlib/issues/22197 There `cb.ax.set_yscale('linear')` is suggested as a workarround. – JohanC Nov 28 '22 at 23:39
  • THANKS!!!!! this has fixed it. i've made a comment on the github thread. imho the documentation should mention this. – jonnyhtw Nov 29 '22 at 00:08
  • 1
    Just to comment on this - this was indeed an explicit design change to make non-linear norms all behave consistently. In particular, for `LogNorm` the colors are spaced equally, and the scale is what changes. The changes in MPL 3.6 are meant so that all non linear norms (including `TwoSlopeNorm`) behave the same way. – Jody Klymak Nov 30 '22 at 00:11

0 Answers0