在轮廓图的颜色栏上设置限制 [英] Setting the limits on a colorbar of a contour plot

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

问题描述

我看到了很多例子,这些例子根本不适用于我的情况.我想做的是为颜色栏设置一个简单的最小值和最大值.为图像cmap设置范围很容易,但这不会将相同的范围应用于颜色条的最小值和最大值.下面的代码可能会解释:

I have seen so many examples that just don't apply to my case. What I would like to do is set a simple minimum and maximum value for a colorbar. Setting a range for an image cmap is easy but this does not apply the same range to the minimum and maximum values of the colorbar. The code below may explain:

triang = Triangulation(x,y)
plt.tricontourf(triang, z, vmax=1., vmin=0.)
plt.colorbar()

尽管cmap范围现在固定在0到1之间,但颜色条仍固定为数据z的极限.

The colorbar is still fixed to the limits of the data z, although the cmap range is now fixed between 0 and 1.

推荐答案

I propose you incorporate you plot in a fig and get inspiration from this sample using the colorbar

data = np.tile(np.arange(4), 2)
fig = plt.figure()
ax = fig.add_subplot(121)
cax = fig.add_subplot(122)
cmap = colors.ListedColormap(['b','g','y','r'])
bounds=[0,1,2,3,4]
norm = colors.BoundaryNorm(bounds, cmap.N)
im=ax.imshow(data[None], aspect='auto',cmap=cmap, norm=norm)
cbar = fig.colorbar(im, cax=cax, cmap=cmap, norm=norm, boundaries=bounds, 
     ticks=[0.5,1.5,2.5,3.5],)
plt.show()

您看到可以为颜色栏和刻度中的颜色设置 bounds .

you see that you can set bounds for the colors in colorbar and ticks.

这并不是您想要达到的严格要求,但是无花果的提示可能会有所帮助.

it is not rigourously what you want to achieve, but the hint to fig could help.

这另外一个使用 ticks 以及定义颜色条的比例.

This other one uses ticks as well to define the scale of colorbar.

import numpy as np
import matplotlib.pyplot as plt

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
               [0., 1.0, 2.0],
               [-0.1, 1.0, 2.0]])

v = np.linspace(-.1, 2.0, 15, endpoint=True)
plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
x = plt.colorbar(ticks=v)
print x
plt.show()

这篇关于在轮廓图的颜色栏上设置限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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