在一个颜色栏上绘制两个范围 [英] Plotting two ranges on one colorbar

查看:33
本文介绍了在一个颜色栏上绘制两个范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使一个颜色条具有两个范围?

Is it possible to get one colorbar to have two ranges?

基本上,我有两个图(分别创建并粘贴到空白图像中).

Basically I have two plots (created separatly and pasted into a blank image).

它们使用相同的色图(热图),但是由于每个绘图中数据的性质,颜色在不同的范围内.基本上在颜色栏的左侧,我希望范围为0-1,右侧为0.75 -1.

They use the same colormap (hot), but the color is over a different range due to the nature of the data in each plot. Basically on the left side of the colorbar I would like the range to be 0-1 and the right to be 0.75 -1.

可以使用一个图或一个完整的图形来创建颜色条.我想绘制一个颜色条.

The colorbar could be created with one plot, or a completely figure. I would like to plot a colorbar.

我目前知道如何将刻度线从右移到左侧或在颜色栏方向上移动.但是,我似乎无法在两边都打勾.

I currently know how to move the ticks from right to left or the colorbar orientation. However, I can't seem to put the ticks on both sides.

这是我用来在颜色栏左侧而不是默认右侧绘制刻度线的代码,但是我似乎无法将其放在两侧.

This is the code that I use to plot the ticks on the left of the colorbar rather than the default right, but I can't seem to put them on both sides.

    cb = plt.colorbar(sm)
    cb.ax.yaxis.set_ticks_position('left')

推荐答案

基本上,您可以使用 twinx 来创建另一个Y轴,但是当我尝试将其放置在副颈斧上时,长宽比设置引起一些问题,所以我使用 set_position()更改两个轴的宽度,这是代码:

Basically, you can use twinx to create another Y-axis, but when I try it to the colobar axe, the aspect setting cause some problem, so I use set_position() to change the width of the two axes, here is the code:

import pylab as pl
import numpy as np
a = np.random.rand(10, 10)
pl.imshow(a)
cb = pl.colorbar(pad=0.1)

l, b, w, h = cb.ax.get_position().bounds
cb.ax.set_aspect("auto")
w = 0.05
ax2 =pl.twinx(ax=cb.ax)
cb.ax.set_position([l, b, w, h])
ax2.set_position([l, b, w, h])
cb.ax.set_ylim(0, 1)
ax2.set_ylim(-10, 10)

输出:

这篇关于在一个颜色栏上绘制两个范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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