在matplotlib中设置颜色条范围 [英] Set Colorbar Range in matplotlib

查看:689
本文介绍了在matplotlib中设置颜色条范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  import matplotlib.pyplot as plt 

cdict = {
'red':((0.0,0.25,.25),(0.02,.59,.59),(1.,1.,1.)),
'green':(( (0.0,0.0,0.0),(0.02,0.45,.45),(1.97,97)),
'blue':((0.0,1.0,1.0),(0.02,0.0)。 75,.75),(1.,0.45,0.45))


cm = m.colors.LinearSegmentedColormap('my_colormap',cdict,1024)

plt.clf()
plt.pcolor(X,Y,v,cmap = cm)
plt.loglog()
plt.xlabel('X Axis')
plt.ylabel('Y Axis')

plt.colorbar()
plt.show()

因此,这会使用指定的色彩图在x轴和y轴上生成值v的图形。 X和Y轴是完美的,但颜色映射在v的最小值和最大值之间扩展。我想强制颜色映射的范围在0到1之间。



I想到使用:

  plt.axis(...)

设置坐标轴的范围,但这只需要X和Y的最小值和最大值的参数,而不是颜色图。



编辑

为了清楚起见,假设我有一个图表,其值范围为(0 ... 0.3) (0.2 ... 0.8)。

在这两个图中,我都希望颜色条的范围为(0 ... 1) 。在这两张图中,我希望使用上面所有的cdict全部范围来使这个范围的颜色相同(因此在这两个图中的0.25将是相同的颜色)。在第一张图中,0.3到1.0之间的所有颜色将不会在图中显示,但会在侧面的色彩键中显示。另一方面,0到0.2之间的所有颜色,以及0.8到1之间的所有颜色将不会在图中显示,但会在颜色栏的侧面显示。

解决方案使用 vmin vmax 强制颜色的范围。以下是一个例子:



  import matplotlib as m 
import matplotlib.pyplot as plt
import numpy as np

cdict = {
'red':((0.0,0.25 (0.0,0.0,0.0),(0.02,.59,.59),(1.,1,1。)),
'green' (0.17,0.45),(1.97,97)),
'blue':((0.0,1.0,1.0),(0.02,.75,.75),(1.,0.45,0.45 ))
}

cm = m.colors.LinearSegmentedColormap('my_colormap',cdict,1024)

x = np.arange(0,10,.1 )
y = np.arange(0,10,.1)
X,Y = np.meshgrid(x,y)

data = 2 *(np.sin( X)+ np.sin(3 * Y))

def do_plot(n,f,title):
#plt.clf()
plt.subplot(1, 3,n)
plt.pcolor(X,Y,f(data),cmap = cm,vmin = -4,vmax = 4)
plt.title(title)
plt。 colorbar()

plt.figure()
do_plot(1,lambda x:x,all)
do_plot(2,lambda x:np.clip(x,-4,0),< 0)
do_plot (3,lambda x:np.clip(x,0,4),> 0)
plt.show()


I have the following code:

import matplotlib.pyplot as plt

cdict = {
  'red'  :  ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
  'green':  ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),
  'blue' :  ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))
}

cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)

plt.clf()
plt.pcolor(X, Y, v, cmap=cm)
plt.loglog()
plt.xlabel('X Axis')
plt.ylabel('Y Axis')

plt.colorbar()
plt.show()

So this produces a graph of the values 'v' on the axes X vs Y, using the specified colormap. The X and Y axes are perfect, but the colormap spreads between the min and max of v. I would like to force the colormap to range between 0 and 1.

I thought of using:

plt.axis(...)

To set the ranges of the axes, but this only takes arguments for the min and max of X and Y, not the colormap.

Edit:

For clarity, let's say I have one graph whose values range (0 ... 0.3), and another graph whose values (0.2 ... 0.8).

In both graphs, I will want the range of the colorbar to be (0 ... 1). In both graphs, I want this range of colour to be identical using the full range of cdict above (so 0.25 in both graphs will be the same colour). In the first graph, all colours between 0.3 and 1.0 won't feature in the graph, but will in the colourbar key at the side. In the other, all colours between 0 and 0.2, and between 0.8 and 1 will not feature in the graph, but will in the colourbar at the side.

解决方案

Using vmin and vmax forces the range for the colors. Here's an example:

import matplotlib as m
import matplotlib.pyplot as plt
import numpy as np

cdict = {
  'red'  :  ( (0.0, 0.25, .25), (0.02, .59, .59), (1., 1., 1.)),
  'green':  ( (0.0, 0.0, 0.0), (0.02, .45, .45), (1., .97, .97)),
  'blue' :  ( (0.0, 1.0, 1.0), (0.02, .75, .75), (1., 0.45, 0.45))
}

cm = m.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)

x = np.arange(0, 10, .1)
y = np.arange(0, 10, .1)
X, Y = np.meshgrid(x,y)

data = 2*( np.sin(X) + np.sin(3*Y) )

def do_plot(n, f, title):
    #plt.clf()
    plt.subplot(1, 3, n)
    plt.pcolor(X, Y, f(data), cmap=cm, vmin=-4, vmax=4)
    plt.title(title)
    plt.colorbar()

plt.figure()
do_plot(1, lambda x:x, "all")
do_plot(2, lambda x:np.clip(x, -4, 0), "<0")
do_plot(3, lambda x:np.clip(x, 0, 4), ">0")
plt.show()

这篇关于在matplotlib中设置颜色条范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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