0

I would like to plot a circle of a given outer radius which would have an empty hole of a given inner radius. Then the resulting ring would have a fade-out gradient fill, however starting not from the center of the circle, but from the border of the inner circle. In Photoshop it's called "glow", from what I know. Is something like this possible?
here's an image showing what I mean

  • Not exactly your geometry, but I think this is answered at: https://stackoverflow.com/questions/29321835/is-it-possible-to-get-color-gradients-under-curve-in-matplotlib – Jody Klymak Nov 17 '21 at 11:50
  • @JodyKlymak thank you, I have seen this example, but I couldn't figure out the way to convert it to my case :/ – Bartłomiej Mróz Nov 17 '21 at 12:21

1 Answers1

1

You could create an image from a function that is zero inside the circle and goes from 1 to 0 on the outside.

Using a colormap that goes from fully transparent white to opaque red would not only interpolate the color but also the transparency.

Here is an example, placing some text to demonstrate the effect of the transparency.

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

inner_radius = 1
outer_radius = 3
center_x = 6
center_y = 4
halo_color = 'gold'
# center_color = 'none' # for an empty center
center_color = '#ff334466'  ## redish with 25% alpha
xmin = center_x - outer_radius
xmax = center_x + outer_radius
ymin = center_y - outer_radius
ymax = center_y + outer_radius
x, y = np.meshgrid(np.linspace(xmin, xmax, 500), np.linspace(ymin, ymax, 500))
r = np.sqrt((x - center_x) ** 2 + (y - center_y) ** 2)
z = np.where(r < inner_radius, np.nan, np.clip(outer_radius - r, 0, np.inf))
cmap = LinearSegmentedColormap.from_list('', ['#FFFFFF00', halo_color])
cmap.set_bad(center_color)
plt.text(center_x, center_y, "Test", size=50, color='b')
plt.imshow(z, cmap=cmap, extent=[xmin, xmax, ymin, ymax], origin='lower', zorder=3)
plt.axis('equal')
plt.show()

plt.imshow with open circle with glow

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • This looks great! I am trying out your code, but I want to control the center point, as well as innter and outer radius. It seems to be some proportion of the "extent" parameter from plt.imshow(), am I correct? – Bartłomiej Mróz Nov 17 '21 at 12:06
  • I updated the answer with a variable center and radii – JohanC Nov 17 '21 at 12:24
  • This is exactly what I had in mind! Thank you very much. One last question - if I would want to have the center and the "halo" colored in any way, could it be easily done? I guess I could draw another circle on top of this one with solid / transparent color, right? – Bartłomiej Mróz Nov 17 '21 at 12:54
  • I updated the code using a center color (transparent red in the example). – JohanC Nov 17 '21 at 14:07