如何在自定义颜色图中获取蒙版值的透明度(matplotlib/python) [英] How to obtain transparency for masked values in customised colormap (matplotlib/python)

查看:47
本文介绍了如何在自定义颜色图中获取蒙版值的透明度(matplotlib/python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个有用的类

I have come across this useful class MidpointNormalize that allows you to choose a data value to correspond to the midpoint colour of your colour-scale. Effectively this means you can shift the midpoint colour of your colour bar by properly remapping the interval [0,1] on itself. I need to use this class and – at the same time – mask certain data values. However, when I mask certain values they do not become transparent as desired, instead they appear over-saturated in colour.

So: how can I make the masked data transparent using thie MidpointNormalize class? I post a runnable minimal example

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


class MidpointNormalize(colors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        colors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y))


N = 100
non_masked_data = np.random.rand(N,N)*4.0-2.0
data = np.ma.masked_where(non_masked_data < -1.0, non_masked_data)

fig, ax = plt.subplots()
ax.set_axis_bgcolor('black')

norm_me = MidpointNormalize(midpoint=1.,vmin=data.min(),vmax=data.max())

plot = plt.imshow(data, origin='lower', interpolation='none', cmap="RdBu_r", norm=norm_me)
cb   = fig.colorbar(plot)
plt.show()

解决方案

The MidpointNormalize originates from this answer in which there is an explicit comment "I'm ignoring masked values and all kinds of edge cases to make a simple example".

In this case, you do not want to ignore masked values. Hence you need to propagate the mask through to the output of the __call__.

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

class MidpointNormalize(colors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        colors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        # Note that I'm ignoring clipping and other edge cases here.
        result, is_scalar = self.process_value(value)
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.array(np.interp(value, x, y), mask=result.mask, copy=False)

N = 100
non_masked_data = np.sort(np.random.rand(N,N)*4.0-2.0)
data = np.ma.masked_where(non_masked_data < -1.0, non_masked_data)

fig, ax = plt.subplots()
ax.set_facecolor('black')

norm_me = MidpointNormalize(midpoint=1.,vmin=data.min(),vmax=data.max())
plot = plt.imshow(data, origin='lower', interpolation='none', cmap="RdBu_r", norm=norm_me)
cb   = fig.colorbar(plot)
plt.show()

I added another note in the class, to warn that I'm ignoring clipping and other edge cases here. ;-)

The result is the following where masked values are transparent (hence the black background is shown).

这篇关于如何在自定义颜色图中获取蒙版值的透明度(matplotlib/python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆