python - 如何在 matplotlib 颜色条中创建自定义断点?

标签 python python-2.7 matplotlib colors

我从 matplotlib 自定义 cmap 示例页面借用了一个示例:

https://matplotlib.org/examples/pylab_examples/custom_cmap.html

这会生成具有不同数量阴影轮廓的相同图像,如 bin 数量中指定的那样:n_bins :

https://matplotlib.org/_images/custom_cmap_00.png

但是,我不仅对 bin 的数量感兴趣,而且对颜色值之间的特定断点感兴趣。例如,当 nbins=6在右上角的子图中,我如何指定 bin 的范围,以便在这些自定义区域中填充阴影:

n_bins_ranges = ([-10,-5],[-5,-2],[-2,-0.5],[-0.5,2.5],[2.5,7.5],[7.5,10])

是否也可以指定断点的包容性?例如,我想在 -2 到 0.5 之间的范围内指定它是否是 -2 < x <= -0.5-2 <= x < -0.5 .

编辑以下答案:

使用下面接受的答案,这里是绘制每个步骤的代码,包括最终在中点添加自定义颜色条刻度。请注意,我是新用户,无法发布图片。

设置数据和 6 个色箱:

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

# Make some illustrative fake data:
x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2*np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10

# Create colormap with 6 discrete bins
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]  # R -> G -> B
n_bin = 6
cmap_name = 'my_list'
cm = matplotlib.colors.LinearSegmentedColormap.from_list(
        cmap_name, colors, N=n_bin)

绘制不同的选项:

# Set up 4 subplots
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)

# Plot 6 bin figure
im = axs[0,0].imshow(Z, interpolation='nearest', origin='lower', cmap=cm)
axs[0,0].set_title("Original 6 Bin")
fig.colorbar(im, ax=axs[0,0])

# Change the break points
n_bins_ranges = [-10,-5,-2,-0.5,2.5,7.5,10]
norm = matplotlib.colors.BoundaryNorm(n_bins_ranges, len(n_bins_ranges))
im = axs[0,1].imshow(Z, interpolation='nearest', origin='lower', cmap=cm, norm=norm)
axs[0,1].set_title("Custom Break Points")
fig.colorbar(im, ax=axs[0,1])

# Arrange color labels by data interval (not colors)
im = axs[1,0].imshow(Z, interpolation='nearest', origin='lower', cmap=cm, norm=norm)
axs[1,0].set_title("Linear Color Distribution")
fig.colorbar(im, ax=axs[1,0], spacing="proportional")

# Provide custom labels at color midpoints
# And change inclusive equality by adding arbitrary small value
n_bins_ranges_arr = np.asarray(n_bins_ranges)+1e-9
norm = matplotlib.colors.BoundaryNorm(n_bins_ranges, len(n_bins_ranges))
n_bins_ranges_midpoints = (n_bins_ranges_arr[1:] + n_bins_ranges_arr[:-1])/2.0
im = axs[1,1].imshow(Z, interpolation='nearest', origin='lower', cmap=cm ,norm=norm)
axs[1,1].set_title("Midpoint Labels\n Switched Equal Sign")
cbar=fig.colorbar(im, ax=axs[1,1], spacing="proportional",
        ticks=n_bins_ranges_midpoints.tolist())
cbar.ax.set_yticklabels(['Red', 'Brown', 'Green 1','Green 2','Gray Blue','Blue'])

plt.show()

最佳答案

您可以使用 BoundaryNorm如下:

import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
x = np.arange(0, np.pi, 0.1)
y = np.arange(0, 2*np.pi, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]  # R -> G -> B
n_bin = 6  # Discretizes the interpolation into bins
n_bins_ranges = [-10,-5,-2,-0.5,2.5,7.5,10]
cmap_name = 'my_list'
fig, ax = plt.subplots()

# Create the colormap
cm = matplotlib.colors.LinearSegmentedColormap.from_list(
            cmap_name, colors, N=n_bin)
norm = matplotlib.colors.BoundaryNorm(n_bins_ranges, len(n_bins_ranges))
# Fewer bins will result in "coarser" colomap interpolation
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm, norm=norm)
ax.set_title("N bins: %s" % n_bin)
fig.colorbar(im, ax=ax)

plt.show()

或者,如果你想要比例间距,即颜色之间的距离根据它们的值,

fig.colorbar(im, ax=ax, spacing="proportional")

enter image description here enter image description here

作为boundary norm documentation

If b[i] <= v < b[i+1] then v is mapped to color j; as i varies from 0 to len(boundaries)-2, j goes from 0 to ncolors-1.

所以颜色总是选择为 -2 <= x < -0.5 ,为了在另一边获得等号,您需要提供 类似 n_bins_ranges = np.array([-10,-5,-2,-0.5,2.5,7.5,10])-1e-9 的东西

关于python - 如何在 matplotlib 颜色条中创建自定义断点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46606069/

相关文章:

python - 比较 Pandas 列中的 float

python - Golang 中的并发服务器

Python:分配变量和调用方法的内存效率

python - PyQt4 名称在 eclipse 中显示为未定义,但运行良好

python - 将多行合并为 1 行

python-2.7 - 带分类轴的 3D 图 [Python/Matplotlib]

python - Seaborn 热图 : Move colorbar on top of the plot

python - Pandas 离开加入到位

python - matplotlib 绘图窗口不会出现

python - 如何使用标准化标记透明度创建散点图