使用“set_under"或“set_over"后重置默认 matplotlib 颜色图值 [英] Reset default matplotlib colormap values after using 'set_under' or 'set_over'

查看:115
本文介绍了使用“set_under"或“set_over"后重置默认 matplotlib 颜色图值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经困扰了我一段时间了.每当我使用 cmap.set_under() cmap.set_over()方法更改界限值的颜色时,它们似乎会将这些更改应用于所有实例我在哪里使用该颜色图.下面是我所指的一个例子.

This is something that has been bugging me for a while. Whenever I use the cmap.set_under() or cmap.set_over() methods to change the the color of out of bound values, they seem to apply these changes to ALL instances where I use that colormap. Below is an example of what I am referring to.

import matplotlib.pyplot as plt 
import numpy as np

rand_data = np.random.randint(50, size=(20,20))

plt.figure()
plt.subplot(211)
cmap1 = plt.cm.rainbow
im1 = plt.pcolormesh(rand_data, cmap=cmap1, vmin=10)
im1.cmap.set_under('w')
plt.colorbar(extend='min')

plt.subplot(212)
cmap2 = plt.cm.rainbow
im2 = plt.pcolormesh(rand_data, cmap=cmap2, vmin=10)
im2.cmap.set_under('k')
plt.colorbar(extend='min')

plt.show()

在这里,我试图创建两个具有相同值的图.在第一个图中,我希望所有低于10的值都为白色.在第二个图中,我希望所有低于10的值都为黑色.结果是这样的:

Here I am trying to create two plots of the same values. In the first plot, I want all values below 10 to be white. In the second plot, I want all values below 10 to be black. The result is this:

似乎我第二次使用 set_under 时,它会为所有使用彩虹色图的现有绘图重置set_under.如果我在第二个图中使用不同的颜色图,我可以设置不同的 set_under 颜色:

It appears that the second time I used set_under it reset the set_under for all existing plots that use the rainbow colormap. If I use a different colormap in the second plot, I able to set a different set_under color:

更奇怪的是,如果在函数或脚本中使用 cmap.set_under() cmap.set_over(),则退出该功能后不会重置此设置.也就是说,如果我注释掉我明确定义 set_under 的行,然后颜色重新运行我的脚本,我会得到与以前相同的结果.

Even more bizarre, if use cmap.set_under() or cmap.set_over() in a function or script, this setting does not reset after exiting that function. That is, if I comment out the lines where I explicitly defined the set_under and colors re-run my script, I get the same result as before.

所以我有几个问题:

  1. 是否可以在不影响任何现有图的色图的情况下为单个图设置颜色图的边界值的颜色?

  1. Is there a way to set the color of out-of-bounds values of a colormap for a single plot without affecting the colormap of any existing plots?

如何将越界值重置为其原始颜色?

How do I reset the out-of-bounds values to their original colors?

对于第二个问题,我知道我可以通过执行以下操作来手动添加原始颜色:

For the second question, I know I can manually add back in the original colors by doing something like this:

N = cmap.N
cmap.set_under(cmap(1))
cmap.set_over(cmap(N-1))

但是,我觉得应该有一种更简单的方法.

But, I feel like there should be an easier way.

推荐答案

您描述的行为是预期的.目前只有一个颜色图,即 plt.cm.rainbow.当你第一次 set_under('w') 和后来的 set_under('k') 时,under 的颜色将是黑色.

The behaviour you describe is expected. There is only a single colormap present, which is plt.cm.rainbow. When you first set_under('w') and later set_under('k'), the color for under will be black.

您要做的实际上是使用同一色彩图的两个不同实例,然后将每个实例更改为下限具有不同的值.
这可以使用 copy,

What you want to do is actually use two different instances of the same colormap and then change each instance to have a different value for the lower bound.
This can easily be done using copy,

cmap1 = copy.copy(plt.cm.rainbow)

现在操作 cmap1 不会改变彩虹色图本身,因此以后可以创建它的另一个副本并应用不同的设置.

Now manipulating cmap1 does not change the rainbow colormap itself, such that it is possible to later create another copy of it and apply different settings.

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

rand_data = np.random.randint(50, size=(20,20))

plt.figure()
plt.subplot(211)
cmap1 = copy.copy(plt.cm.rainbow)
im1 = plt.pcolormesh(rand_data, cmap=cmap1, vmin=10)
im1.cmap.set_under('w')
plt.colorbar(extend='min')

plt.subplot(212)
cmap2 = copy.copy(plt.cm.rainbow)
im2 = plt.pcolormesh(rand_data, cmap=cmap2, vmin=10)
im2.cmap.set_under('k')
plt.colorbar(extend='min')

plt.show()

这篇关于使用“set_under"或“set_over"后重置默认 matplotlib 颜色图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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