问题描述
我有多条曲线在一个参数上不同,我想将它们绘制在一个图中.为了区分它们,我想使用matplotlib的颜色条之一.为此,我根据所述参数生成颜色列表.此外,我想添加一个颜色条来解释所使用的颜色.我可以轻松完成所有这些.现在的问题是,我只想使用可用颜色图的一部分,因为它太亮了,因此在某个阈值以上几乎看不到.但是,当我现在只在一个子范围中选择颜色时,我没有找到一种方法来调整显示的颜色栏的范围.
I have multiple curves that differ in one parameter and which I want to plot in one figure. To distinguish them, I want to use one of matplotlib's colorbars. To do so I produce a list of colors depending on said parameter. Additionally, I want to add a colorbar to explain the colors that are used. I can easily do all of that. The problem is now, that I want to use only a part of the available colormap, as it gets too bright and thus barely visible above some threshold. But when I now choose the colors only in a subrange, I did not find a way to adjust the range of the displayed colorbar.
这是我想要实现的(几乎)最小的示例:
Here is a (nearly) minimal example of what I want to achieve:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 1,
height_ratios=[1, 4]
)
ax = [plt.subplot(g) for g in gs]
parameterToColorBy = np.linspace(5, 10, 6, dtype=float)
maxColor = 0.85
colors = [plt.get_cmap("inferno")(i)
for i in np.linspace(0, maxColor, parameterToColorBy.shape[0])]
norm = mpl.colors.Normalize(parameterToColorBy[0],
parameterToColorBy[0]+
(parameterToColorBy[-1]-parameterToColorBy[0])/
maxColor)
cb = mpl.colorbar.ColorbarBase(ax[0],
cmap="inferno",
norm=norm,
ticks=parameterToColorBy,
orientation='horizontal')
ax[0].xaxis.set_ticks_position('top')
for p, c in zip(parameterToColorBy, colors):
ax[1].plot(np.arange(2)/p, c=c)
plt.show()
结果如下:
我现在希望颜色条停在 10.但如果我只是通过添加行 ax[0].set_xlim(0, maxColor)来调整子图的
xlim
code>,颜色部分调整正确,但是周围的框乱了:
I now want the colorbar to stop at 10. But if I just adjust the xlim
of the subplot by adding the line ax[0].set_xlim(0, maxColor)
, the colored part is adjusted correctly, but the surrounding box is messed up:
或者,我找到了一个颜色条 set_clim
的函数.但这只会改变规范化,似乎并没有像我想要的那样工作.添加 cb.set_clim(parameterToColorBy [0],parameterToColorBy [-1])
会导致颜色更改但轴不变:
Alternatively, I found a function for colorbars set_clim
. But this only changes the normalization and does not seem to work as I want. Adding cb.set_clim(parameterToColorBy[0], parameterToColorBy[-1])
results in an changed colors but unchanged axis:
我似乎需要的是一种适当的方法来调整显示的颜色条的限制,或者是一种创建自己的颜色条作为可用颜色条的子集的方法.有没有办法实现其中之一?
What I seem to need is either an appropriate way to adjust the limits of the displayed colorbar, or a way to create an own colorbar as a subset of an available colorbar. Is there any way to achieve one of these things?
推荐答案
您可以使用我在下面的代码中编写的 truncate_colormap
函数来截断颜色图.它将根据现有的颜色图创建一个新的 matplotlib.colors.LinearSegmentedColormap
.
You can truncate the colormap by using the truncate_colormap
function I have written in the code below. It creates a new matplotlib.colors.LinearSegmentedColormap
from an existing colormap.
请注意,您不需要通过 maxColor
缩放 Normalise
实例,并且在创建 colors 列表和
colorbar
.
Note that you then don't need to scale the Normalise
instance by maxColor
, and you need to use this new colormap instance when creating your colors
list and the colorbar
.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.colors as mcolors
gs = gridspec.GridSpec(2, 1,
height_ratios=[1, 4]
)
ax = [plt.subplot(g) for g in gs]
parameterToColorBy = np.linspace(5, 10, 6, dtype=float)
def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=-1):
if n == -1:
n = cmap.N
new_cmap = mcolors.LinearSegmentedColormap.from_list(
'trunc({name},{a:.2f},{b:.2f})'.format(name=cmap.name, a=minval, b=maxval),
cmap(np.linspace(minval, maxval, n)))
return new_cmap
minColor = 0.00
maxColor = 0.85
inferno_t = truncate_colormap(plt.get_cmap("inferno"), minColor, maxColor)
colors = [inferno_t(i)
for i in np.linspace(0, 1, parameterToColorBy.shape[0])]
norm = mpl.colors.Normalize(parameterToColorBy[0],
parameterToColorBy[-1])
cb = mpl.colorbar.ColorbarBase(ax[0],
cmap=inferno_t,
norm=norm,
ticks=parameterToColorBy,
orientation='horizontal')
ax[0].xaxis.set_ticks_position('top')
for p, c in zip(parameterToColorBy, colors):
ax[1].plot(np.arange(2)/p, c=c)
plt.show()
这篇关于如何在 matplotlib 中仅使用和绘制颜色条的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!