gpt4 book ai didi

python - Matplotlib:如何将 colorbar 切割成 2 个或更多个?

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:05 25 4
gpt4 key购买 nike

我有一个包含 3 组值的数组:

  • 0 到 1 之间的值(绿色渐变)
  • 值等于2(红色)
  • 值等于3(灰色)

感谢这篇文章 ( Define a colormap for each set of values in an array ) 我能够得到这段代码:

from random import random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm

n=11
tab = np.array([[random() for i in range(n)] for j in range(n)])
tab[1,2] = 2.
tab[3,4] = 2.
tab[5,6] = 3.
tab[7,8] = 3.

values1 = np.ma.masked_array(tab, tab > 1.)
values2 = np.ma.masked_array(tab, tab != 2.)
values3 = np.ma.masked_array(tab, tab != 3.)

# 50 values for later use from 0 to 1
greens = cm.Greens(np.linspace(0,1, num=50))
# 25 values for later use from 1 to 1.5
greensfill = cm.Greens(np.ones(25))
# 50 values red for later use from 1.5 to 2.5
red = [(1,0,0,1)]*len(greens)
# 50 values gray for later use from 2.5 to 3.5
gray = [(.5,.5,.5,1)]*len(greens)

colors = np.vstack((greens, greensfill, red, gray))
# in total we now have 175 colors in the colormap
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)

#we now map those 175 colors to the range between 0 and 3.5
im = plt.imshow(tab, cmap = mycmap, interpolation="none", vmin=0, vmax=3.5)
cb = plt.colorbar(im)
cb.set_ticks([0,1,2,3])

plt.show()

结果如下:

enter image description here

我的问题是:ma​​tplotlib 能否通过将现有的颜色图切割成 3 个单独的颜色图或接近的颜色图来获得类似下图的效果(使用 photoshop 完成编辑)?

enter image description here

编辑:

greens = cm.Greens(np.linspace(0,1, num=75))
red = [(1,0,0,1)]*(len(greens)/2)
white = [(1,1,1,1)]*3
black = [(0,0,0,1)]*1
gray = [(.5,.5,.5,1)]*(len(greens)/2)

colors = np.vstack((greens, black, white, black, red, black, white, black, gray))
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)

我修改了代码并得到了以下内容,但它看起来不太好,索引位置错误:

enter image description here

最佳答案

首先:不要更改颜色图!这是指定您的数据的 map ,因此如果您更改它,数据表示将不同于您最初的目标。

您可以做的是创建 3 个不同的颜色条,如下所示。注意区别:我们有一个单一的颜色图,但是有 3 个颜色条,它们都显示了一个颜色图的一部分。

enter image description here

from random import random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib import cm

n=11
tab = np.array([[random() for i in range(n)] for j in range(n)])
tab[1,2] = 2.
tab[3,4] = 2.
tab[5,6] = 3.
tab[7,8] = 3.

values1 = np.ma.masked_array(tab, tab > 1.)
values2 = np.ma.masked_array(tab, tab != 2.)
values3 = np.ma.masked_array(tab, tab != 3.)

# 50 values for later use from 0 to 1
greens = cm.Greens(np.linspace(0,1, num=50))
# 25 values for later use from 1 to 1.5
greensfill = cm.Greens(np.ones(25))
# 50 values red for later use from 1.5 to 2.5
red = [(1,0,0,1)]*len(greens)
# 50 values gray for later use from 2.5 to 3.5
gray = [(.5,.5,.5,1)]*len(greens)

colors = np.vstack((greens, greensfill, red, gray))
# in total we now have 175 colors in the colormap
mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors)

#we now map those 175 colors to the range between 0 and 3.5
im = plt.imshow(tab, cmap = mycmap, interpolation="none", vmin=0, vmax=3.5)

plt.subplots_adjust(top=0.90, bottom=0.1)
cb1ax = plt.gcf().add_axes([0.85, 0.8, 0.035, 0.1])
cb2ax = plt.gcf().add_axes([0.85, 0.67, 0.035, 0.1])
cb3ax = plt.gcf().add_axes([0.85, 0.1, 0.035, 0.54])

plt.gcf().colorbar(im, cax=cb1ax, boundaries=[2.5,3.5], ticks=[3])
plt.gcf().colorbar(im, cax=cb2ax, boundaries=[1.5,2.5], ticks=[2])
plt.gcf().colorbar(im, cax=cb3ax, boundaries=np.linspace(0,1, num=100), ticks=[0,1])

plt.show()


编辑:现在可能会出现颜色条与图像的间距不紧密甚至与图像重叠的情况。这三个条的高度也可能比图像大。为了防止这种情况,我们可以调整条形的大小并调整它们的位置,这样它们看起来总是很漂亮。因此,脚本的下半部分可能会被以下代码替换。

plt.subplots_adjust(top=0.90, bottom=0.1)
plt.gcf().canvas.draw()
ax = plt.gca()

plt.subplots_adjust(top=0.90, bottom=0.1)
cb1ax = plt.gcf().add_axes([0.85, 0.8, 0.035, 0.1])
cb2ax = plt.gcf().add_axes([0.85, 0.67, 0.035, 0.1])
cb3ax = plt.gcf().add_axes([0.85, 0.1, 0.035, 0.54])

plt.gcf().colorbar(im, cax=cb1ax, boundaries=[2.5,3.5], ticks=[3])
plt.gcf().colorbar(im, cax=cb2ax, boundaries=[1.5,2.5], ticks=[2])
plt.gcf().colorbar(im, cax=cb3ax, boundaries=np.linspace(0,1, num=100), ticks=[0,1])

def resize(event=None):
s = 0.03
pos = ax.get_position()
smh = pos.height/8.; loh = pos.height*0.675
w = 0.06*pos.width;
x0 = pos.x1+0.065*pos.width
cb1ax.set_position([x0, pos.y0+loh+smh+2*s, w, smh])
cb2ax.set_position([x0, pos.y0+loh+s, w, smh])
cb3ax.set_position([x0, pos.y0, w, loh])

resize()
plt.connect("resize_event", resize)

plt.show()

关于python - Matplotlib:如何将 colorbar 切割成 2 个或更多个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42650294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com