1

I want to generate a plot like the below: enter image description here

At the moment I am trying to play around with the alpha parameter:

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")

But changing the order of plotting, I get different results. One color map ''hides'' the other. Say I plot the Reds one first, it appears and the Blues one is hidden. The other way around, Blues first and Reds first, the Blues hides the Reds.

The result of the above code is: Code output

Do you have anything in mind what shall I do?

Thank you!

2
  • How would that help me? I am happy with the positions of the colorbars, the graph itself is incomplete Commented Jun 4, 2021 at 18:03
  • 1
    My bad, I got it wrong. I just answered your questions with a possible solution. Commented Jun 4, 2021 at 22:23

1 Answer 1

2

Setting the alpha value of the pcolor call is not that good because it applies the same transparency to all the colors on the colormap.

You could use a custom colormap with an evolving transparency, I present my try with linear and sigmoidal evolutions of alpha, you could try others. I created dummy noisy data with a Gaussian pulse to simulate the data as in your example.

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],)

Plot of stacked pcolors using transparency

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.