微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MATPLOTLIB:如何在 python 中的同一绘图上堆叠 2 个颜色图?

如何解决MATPLOTLIB:如何在 python 中的同一绘图上堆叠 2 个颜色图?

我想生成如下图:

enter image description here

目前我正在尝试使用 alpha 参数:

import numpy as np
from matplotlib import pyplot as plt

xlocations_edensity = np.loadtxt("edensity_xaxis.txt") 
ylocations_edensity = np.loadtxt("edensity_yaxis.txt") 
xlocsedensity,ylocsedensity = np.meshgrid(xlocations_edensity,ylocations_edensity)

xlocations_Efield = np.loadtxt("Efield_x_axis.txt")
ylocations_Efield = np.loadtxt("Efield_y_axis.txt")
xlocsEfield,ylocsEfield = np.meshgrid(xlocations_Efield,ylocations_Efield)

edensitytensor = np.load("edensitytensor.npy") # shape (76,257,65)
Efieldtensor = np.load("Efieldtensor.npy")

fig,ax = plt.subplots()
ax.set(xlabel="x position [um]",ylabel="y position [um] \n")
pos2 = ax.pcolor(xlocations_Efield,ylocations_Efield,Efieldtensor[40,:,:].T,cmap="Reds",alpha=0.9)
fig.colorbar(pos2,ax=ax,label="\n Efield value [MV/m]")
pos1 = ax.pcolor(xlocations_edensity,ylocations_edensity,edensitytensor[100,cmap="Blues",alpha=0.5)
fig.colorbar(pos1,label="\n electron density value [cm^(-3)]")
plt.savefig("Efield_edensity_map.pdf")

但是改变绘图顺序,我得到了不同的结果。一张颜色图“隐藏”了另一个。 假设我先绘制了 Reds 一个,它出现了,而 Blues 一个隐藏了。 反过来,蓝军先红军,蓝军隐藏红军。

以上代码的结果是:

Code output

你有什么想法我该怎么办?

谢谢!

解决方法

设置 pcolor 调用的 alpha 值不太好,因为它对颜色图上的所有颜色应用相同的透明度。

您可以使用具有不断变化的透明度的自定义颜色图,我尝试使用 alpha 的线性和 sigmoidal 演变,您可以尝试其他方法。我用高斯脉冲创建了虚拟噪声数据来模拟您的示例中的数据。

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap


# generating dummy data
nx,ny = 257,65

x_field,y_field = np.linspace(0,10,nx),np.linspace(0,6,ny)
field = np.random.rand(nx,ny)
# normalizing
field -= np.min(field); field /= np.max(field)

x_density,y_density = np.linspace(1,np.linspace(1,ny)
Y,X = np.meshgrid(y_density,x_density,)
density = np.random.rand(nx,ny) # shape (76,257,65)
gaussian_center = (4.0,4.0)
distance_square = (X - gaussian_center[0])**2 + (Y - gaussian_center[1])**2
density += 5.0*np.exp(-distance_square/4.0)
# normalizing
density -= np.min(density); density /= np.max(density)

# getting the original colormap
orig_cmap = plt.get_cmap('Blues')
cmap_n = orig_cmap.N
derived_cmap = orig_cmap(np.arange(cmap_n))

fig,axs = plt.subplots(
    4,3,gridspec_kw={"width_ratios":[1,0.025,0.025]},figsize=(10,8),constrained_layout=True)

# original
row_subplot = 0
ax = axs[row_subplot,0]
ax.set_ylabel("original")

image_field = ax.pcolor(
    x_field,y_field,field.T,cmap="Reds",shading='auto')
fig.colorbar(image_field,cax=axs[row_subplot,-2],)
image_density = ax.pcolor(
    x_density,y_density,density.T,cmap=orig_cmap,shading="auto")
fig.colorbar(image_density,-1],)

# option 1 - transparent pseudocolor for the above image
row_subplot = 1
ax = axs[row_subplot,0]
ax.set_ylabel("transparent pcolor")

image_field = ax.pcolor(
    x_field,alpha=1.0,alpha=0.5,)


# option 2 - linear gradient colormap
linear_cmap = derived_cmap.copy()
linear_cmap[:,-1] = np.arange(cmap_n)/cmap_n
linear_cmap = ListedColormap(linear_cmap)

row_subplot = 2
ax = axs[row_subplot,0]
ax.set_ylabel("linear gradient")
image_field = ax.pcolor(
    x_field,cmap=linear_cmap,)


# option 3 - sigmoid gradient
sigmoid_cmap = derived_cmap.copy()
x = np.linspace(-10,cmap_n)
sigmoid_cmap[:,-1] = np.exp(x)/(np.exp(x) + 1)
sigmoid_cmap = ListedColormap(sigmoid_cmap)

row_subplot = 3
ax = axs[row_subplot,0]
ax.set_ylabel("sigmoid gradient")

image_field = ax.pcolor(
    x_field,cmap=sigmoid_cmap,)

Plot of stacked pcolors using transparency

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。