<jupyter_start><jupyter_text>PurposeOverlay 2 images in one plot, with the top image having selected regions of transparency.<jupyter_code>from pathlib import Path

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline

print(mpl.__version__)<jupyter_output>3.3.2<jupyter_text>Background image<jupyter_code>n_x, n_y = 500, 400

background = np.zeros((n_y, n_x))
background[:, 50:] = np.linspace(0, 0.5, 450)
temp = np.linspace(0, 0.5, 400)
# print(temp)
for i in range(0, n_x):
    background[:, i] += temp
background[100:150, 200:350] = 0
    
background.shape
fig, ax = plt.subplots()
ax.imshow(background)<jupyter_output><empty_output><jupyter_text>Foreground image overlaid on background image<jupyter_code>foreground = np.zeros((n_y, n_x, 4)) + 0.8
foreground[:, :, 3] = 0
foreground[50:150, 50:150, 3] = 1
foreground[220:280, 350:470, 3] = 1
foreground.shape
fig, ax = plt.subplots()
ax.imshow(background)
ax.imshow(foreground);<jupyter_output><empty_output><jupyter_text>Foreground image only where background image = 0  Simple example - 1 region of zeros Background image<jupyter_code>n_x, n_y = 500, 400

background = np.zeros((n_y, n_x))
background[:, :] = np.linspace(0.2, 0.5, 500)
temp = np.linspace(0.2, 0.5, 400)
# print(temp)
for i in range(0, n_x):
    background[:, i] += temp
background[100:150, 200:250] = 0

print(background.shape)

fig, ax = plt.subplots()
im = ax.imshow(background, origin='lower', cmap='Greys_r') #, vmin=0, vmax=1)
fig.colorbar(im);<jupyter_output>(400, 500)<jupyter_text>Foreground image and plot on top of background<jupyter_code>foreground = np.zeros((n_y, n_x, 4))

temp = np.where(background == 0)
print(len(temp), len(temp[0]), len(temp[1]))
# temp

# indices = [[y, x] for y, x in zip(temp[0], temp[1])]
grey_value = 0.8
foreground[np.ix_(*temp)] = [grey_value, grey_value, grey_value, 1]
# foreground[np.ix_(temp[0], temp[1])] = [grey_value, grey_value, grey_value, 1]

fig, ax = plt.subplots()
ax.imshow(background, origin='lower', cmap="seismic")
im = ax.imshow(foreground, origin='lower')
fig.colorbar(im);<jupyter_output>2 2500 2500<jupyter_text>Another example<jupyter_code>n_x, n_y = 500, 400

background = np.zeros((n_y, n_x))
background[:, :] = np.linspace(0, 0.5, 500)
temp = np.linspace(0, 0.5, 400)
# print(temp)
for i in range(0, n_x):
    background[:, i] += temp
background[100:120, 200:220] = 0
    
fig, ax = plt.subplots()
im = ax.imshow(background, origin='lower', cmap='Greys_r') #, vmin=0, vmax=1)
fig.colorbar(im);
foreground = np.zeros((n_y, n_x, 4))

temp = np.where(background < 1e-1)
print(len(temp), len(temp[0]), len(temp[1]))
# temp

# indices = [[y, x] for y, x in zip(temp[0], temp[1])]
grey_value = 0.8
foreground[np.ix_(*temp)] = [grey_value, grey_value, grey_value, 1]
# foreground[np.ix_(temp[0], temp[1])] = [grey_value, grey_value, grey_value, 1]

fig, ax = plt.subplots()
im = ax.imshow(background, origin='lower', cmap="seismic")
ax.imshow(foreground, origin='lower');
fig.colorbar(im);<jupyter_output>2 4480 4480<jupyter_text>Figure out `np.indices` and radial distance 2D array<jupyter_code>np.indices((4,5))
indices_y, indices_x = np.indices((4,5))
indices_y
indices_x
temp = np.sqrt(indices_x**2 + indices_y**2)
temp
np.max(temp)
temp /= np.max(temp)
temp<jupyter_output><empty_output><jupyter_text>Example - normalized radial distance background image<jupyter_code>n_x, n_y = 500, 400

indices_y, indices_x = np.indices((n_y, n_x))
background = np.sqrt(indices_x**2 + indices_y**2)
background /= np.max(background)
print(background.shape)

background[100:160, 200:300] = 0
    
fig, ax = plt.subplots()
im = ax.imshow(background, origin='lower', cmap='Greys_r') #, vmin=0, vmax=1)
fig.colorbar(im);
foreground = np.zeros((n_y, n_x, 4))

temp = np.where(background < 1e-5)
print(len(temp), len(temp[0]), len(temp[1]))

grey_value = 0.8
foreground[np.ix_(*temp)] = [grey_value, grey_value, grey_value, 1]
# foreground[np.ix_(temp[0], temp[1])] = [grey_value, grey_value, grey_value, 1]

fig, ax = plt.subplots()
im = ax.imshow(background, origin='lower', cmap="seismic")
im_f = ax.imshow(foreground, origin='lower', cmap="Greys_r");
fig.colorbar(im_f);
fig.colorbar(im);
# fig, ax = plt.subplots()
# lim = 250
# temp_max = np.max(background[:lim, :lim])
# print(temp_max)
# im = ax.imshow(background, origin='lower', cmap='Greys_r', vmin=0, vmax=temp_max)
# ax.set_xlim(0, lim)
# ax.set_ylim(0, lim)
# fig.colorbar(im);<jupyter_output><empty_output><jupyter_text>Try method in this matplotlib documentation exampleSee [Blend transparency with color in 2D images](https://matplotlib.org/stable/gallery/images_contours_and_fields/image_transparency_blend.html), section *Blending in transparency*.<jupyter_code>n_x, n_y = 500, 400

indices_y, indices_x = np.indices((n_y, n_x))
background = np.sqrt(indices_x**2 + indices_y**2)
background /= np.max(background)
print(background.shape)

background[100:160, 200:300] = 0
    
fig, ax = plt.subplots()
im = ax.imshow(background, origin='lower', cmap='Greys_r') #, vmin=0, vmax=1)
fig.colorbar(im);
grey_value = 0.3
foreground = np.ones((n_y, n_x)) * grey_value

temp = np.where(background < 1e-5)
print(len(temp), len(temp[0]), len(temp[1]))

alphas = np.zeros(foreground.shape, dtype=np.float)
alphas[np.ix_(*temp)] = 1
# foreground[np.ix_(temp[0], temp[1])] = [grey_value, grey_value, grey_value, 1]

fig, ax = plt.subplots()
im = ax.imshow(background, origin='lower', cmap="seismic")
im_f = ax.imshow(foreground, alpha=alphas, origin='lower', cmap="Greys_r", vmin=0, vmax=1);
# fig.colorbar(im_f);  # If use this, throws an error because using alpha input parameter in imshow
fig.colorbar(im);<jupyter_output>2 6001 6001
{'fraction_non_alphanumeric': 0.17027972027972027, 'fraction_numerical': 0.0493006993006993, 'mean_word_length': 6.278625954198473, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '325464', 'license': 'MIT', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2301, 'n_words': 485, 'path': 'python/matplotlib/images_with_transparency.ipynb', 'repo_name': 'gregnordin/python-notes_to_self', 'size': '5720'}