移动的颜色条 matplotlib [英] Shifted colorbar matplotlib

查看:44
本文介绍了移动的颜色条 matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为数据集制作填充轮廓.它应该相当简单:

I am trying to make a filled contour for a dataset. It should be fairly straightforward:

plt.contourf(x, y, z, label = 'blah', cm = matplotlib.cm.RdBu)

但是,如果我的数据集关于 0 不对称,我该怎么办?假设我想从蓝色(负值)变为 0(白色),再到红色(正值).如果我的数据集从 -8 变为 3,那么颜色条的白色部分(应该为 0)实际上略显负值.有没有办法改变颜色条?

However, what do I do if my dataset is not symmetric about 0? Let's say I want to go from blue (negative values) to 0 (white), to red (positive values). If my dataset goes from -8 to 3, then the white part of the color bar, which should be at 0, is in fact slightly negative. Is there some way to shift the color bar?

推荐答案

首先,有不止一种方法可以做到这一点.

First off, there's more than one way to do this.

  1. 传递一个 DivergingNorm 的实例 作为 norm kwarg.
  2. 使用 colors kwarg 到 contourf 并手动指定颜色
  3. 使用由 matplotlib.colors.from_levels_and_colors 构建的离散颜色图.
  1. Pass an instance of DivergingNorm as the norm kwarg.
  2. Use the colors kwarg to contourf and manually specify the colors
  3. Use a discrete colormap constructed with matplotlib.colors.from_levels_and_colors.

最简单的方法是第一个选项.它也是允许您使用连续颜色图的唯一选项.

The simplest way is the first option. It is also the only option that allows you to use a continuous colormap.

使用第一个或第三个选项的原因是它们适用于使用颜色图的任何类型的 matplotlib 图(例如 imshowscatter 等).

The reason to use the first or third options is that they will work for any type of matplotlib plot that uses a colormap (e.g. imshow, scatter, etc).

第三个选项从特定颜色构造离散颜色图和归一化对象.它与第二个选项基本相同,但它将 a) 与等高线图以外的其他类型的图一起使用,并且 b) 避免必须手动指定等高线的数量.

The third option constructs a discrete colormap and normalization object from specific colors. It's basically identical to the second option, but it will a) work with other types of plots than contour plots, and b) avoids having to manually specify the number of contours.

作为第一个选项的示例(我将在这里使用 imshow 因为它比 contourf 对随机数据更有意义,但是 contourf> 除了 interpolation 选项之外的用法相同.):

As an example of the first option (I'll use imshow here because it makes more sense than contourf for random data, but contourf would have identical usage other than the interpolation option.):

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

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

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

作为第三个选项的示例(请注意,这提供了离散颜色图而不是连续颜色图):

As an example of the third option (notice that this gives a discrete colormap instead of a continuous colormap):

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

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

num_levels = 20
vmin, vmax = data.min(), data.max()
midpoint = 0
levels = np.linspace(vmin, vmax, num_levels)
midp = np.mean(np.c_[levels[:-1], levels[1:]], axis=1)
vals = np.interp(midp, [vmin, midpoint, vmax], [0, 0.5, 1])
colors = plt.cm.seismic(vals)
cmap, norm = from_levels_and_colors(levels, colors)

fig, ax = plt.subplots()
im = ax.imshow(data, cmap=cmap, norm=norm, interpolation='none')
fig.colorbar(im)
plt.show()

这篇关于移动的颜色条 matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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