将颜色条的标签从增加值更改为减少值 [英] Change the labels of a colorbar from increasing to decreasing values

查看:80
本文介绍了将颜色条的标签从增加值更改为减少值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将颜色条的标签从增加值更改为减少值.当我尝试通过vminvmax执行此操作时,收到错误消息:

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

例如,我希望颜色栏从左侧的20开始,到右侧的15.

So, for example I'd like the colorbar to start at 20 on the left and go up to 15 on the right.

到目前为止,这是我用于颜色栏的代码,但在此示例中,值从15到20,我想颠倒该顺序:

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')

推荐答案

下面显示的颜色条可能与您的颜色条不完全,因为它们只是功能性的 example 作为概念证明.

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:

如果您想反转 x轴,这意味着x轴上的值向右下降,使颜色栏为"mirrored",则可以使用 ColorbarBase ax属性:

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()

这给出了下面的输出.

也可以通过设置颜色条locator来更改刻度标签的数量.尽管您可以使用许多<还有href ="http://matplotlib.org/api/ticker_api.html#tick-locating" rel ="nofollow noreferrer">其他定位符.

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() 

如果您想要颜色条本身的方向,并且仅颠倒刻度线标签的显示顺序,则可以使用set_ticksset_ticklabels方法.这更多地是一种暴力".比以前的解决方案更有效.

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屋!

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