将颜色条的标签从递增值更改为递减值 [英] Change the labels of a colorbar from increasing to decreasing values
问题描述
我想将颜色条的标签从递增值更改为递减值.当我尝试通过 vmin
和 vmax
执行此操作时,我收到错误消息:
最小值必须小于或等于最大值
因此,例如,我希望颜色条从左侧的 20 处开始,到右侧的 15 处.
到目前为止,这是我的颜色条代码,但在此示例中,值从 15 变为 20,我想颠倒该顺序:
cmap1 = mpl.cm.YlOrBr_rnorm1 = mpl.colors.Normalize(15,20)cb1 = mpl.colorbar.ColorbarBase(colorbar1, cmap=cmap1, norm=norm1,orientation='horizontal')cb1.set_label('幅度')
下面显示的颜色条可能与您的完全不一样,因为它们只是示例功能的颜色条作为概念证明.
在下面我假设你有一个与此类似的颜色条,右侧的值增加:
方法一:反转x轴
反转颜色条的整个 x 轴
如果您想反转 x 轴,即 x 轴上的值向右下降,使颜色条镜像",您可以使用 ,虽然您也可以使用许多其他定位器.
from matplotlib.ticker import MultipleLocatorcb1.locator = MultipleLocator(1) # 仅显示每个 1 的倍数的刻度cb1.update_ticks()cb1.ax.invert_xaxis()
方法 2:使用自定义刻度标签
颠倒刻度标签的顺序,保持颜色条的方向
如果您想要颜色条本身的方向,并且只反转刻度标签出现的顺序,您可以使用 set_ticks
和 set_ticklabels
方法.这更像是一种蛮力".比以前的解决方案更接近.
cb1.set_ticks(np.arange(15, 21))cb1.set_ticklabels(np.arange(20, 14, -1))
这给出了如下所示的颜色条.请注意,颜色保持不变,只有刻度位置和刻度标签发生了变化.
I'd like to change the labels for the colorbar from increasing to decreasing values. When I try to do this via vmin
and vmax
I get the error message:
minvalue must be less than or equal to maxvalue
So, for example I'd like the colorbar to start at 20 on the left and go up to 15 on the right.
This is my code for the colorbar so far, but in this example the values go from 15 to 20 and I'd like to reverse that order:
cmap1 = mpl.cm.YlOrBr_r
norm1 = mpl.colors.Normalize(15,20)
cb1 = mpl.colorbar.ColorbarBase(colorbar1, cmap=cmap1, norm=norm1, orientation='horizontal')
cb1.set_label('magnitude')
The colorbars displayed below are probably not exactly like yours, as they are just example colorbars to function as a proof of concept.
In the following I assume you have a colorbar similar to this, with increasing values to the right:
Method 1: Inverting the x-axis
Inverts the whole x-axis of the colorbar
If you want to invert the x-axis, meaning that the values on the x-axis are descending to the right, making the colorbar "mirrored", you can make use of the ColorbarBase
's ax
attribute:
cb1 = mpl.colorbar.ColorbarBase(colorbar1,
cmap=cmap1,
norm=norm1,
orientation='horizontal')
cb1.ax.invert_xaxis()
This gives.the output below.
It is also possible to change the number of ticklabels by setting the colorbars locator
. Here the MultipleLocator
is used, although you can use many other locators as well.
from matplotlib.ticker import MultipleLocator
cb1.locator = MultipleLocator(1) # Show ticks only for each multiple of 1
cb1.update_ticks()
cb1.ax.invert_xaxis()
Method 2: Using custom ticklabels
Reverses the order of the ticklabels, keeping the orientation of the colorbar
If you want the orientation of the colorbar itself as it is, and only reverse the order in which the ticklabels appear, you can use the set_ticks
and set_ticklabels
methods. This is more of a "brute force" approach than the previous solution.
cb1.set_ticks(np.arange(15, 21))
cb1.set_ticklabels(np.arange(20, 14, -1))
This gives the colorbar seen below. Note that the colors are kept intact, only the tick locations and ticklabels have changed.
这篇关于将颜色条的标签从递增值更改为递减值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!