【问题标题】:MATPLOTLIB: How to stack 2 colormaps on same plot in python?MATPLOTLIB:如何在 python 的同一绘图上堆叠 2 个颜色图?
【发布时间】:2021-08-22 14:34:34
【问题描述】:

我想生成如下图:

目前我正在尝试使用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, :, :].T, cmap="Blues", alpha=0.5)
fig.colorbar(pos1, ax=ax, label="\n electron density value [cm^(-3)]")
plt.savefig("Efield_edensity_map.pdf")

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

以上代码的结果是:

你有什么想法吗?

谢谢!

【问题讨论】:

  • 这对我有什么帮助?我对颜色条的位置很满意,图表本身不完整
  • 我的错,我弄错了。我刚刚用一个可能的解决方案回答了你的问题。

标签: python matplotlib graph data-science colormap


【解决方案1】:

设置 pcolor 调用的 alpha 值并不是那么好,因为它将相同的透明度应用于颜色图上的所有颜色。

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

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,6,nx), np.linspace(1,6,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, cax=axs[row_subplot,-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, y_field, field.T,
    alpha=1.0, cmap="Reds", shading='auto')
fig.colorbar(image_field, cax=axs[row_subplot,-2],)
image_density = ax.pcolor(
    x_density, y_density, density.T,
    alpha=0.5, cmap=orig_cmap, shading="auto")
fig.colorbar(image_density, cax=axs[row_subplot,-1],)


# 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, 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=linear_cmap, shading="auto")
fig.colorbar(image_density, cax=axs[row_subplot,-1],)


# option 3 - sigmoid gradient
sigmoid_cmap = derived_cmap.copy()
x = np.linspace(-10,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, 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=sigmoid_cmap, shading="auto")
fig.colorbar(image_density, cax=axs[row_subplot,-1],)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2019-12-01
    • 1970-01-01
    • 2014-03-12
    • 2012-07-01
    • 2020-07-22
    • 2014-04-03
    相关资源
    最近更新 更多