Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Last active September 28, 2024 18:55
Show Gist options
  • Save nicoguaro/2658ae7d701dffcb811e3ff428e63a4c to your computer and use it in GitHub Desktop.
Save nicoguaro/2658ae7d701dffcb811e3ff428e63a4c to your computer and use it in GitHub Desktop.
Plots with neon-glow in Matplotlib.
# -*- coding: utf-8 -*-
"""
Attempt of neon-glowing plots.
@author: Nicolás Guarín-Zapata
@date: September 2020
"""
import numpy as np
import matplotlib.pyplot as plt
def neon_plot(x, y, ax=None):
if ax is None:
ax = plt.gca()
line, = ax.plot(x, y, lw=1, zorder=6)
for cont in range(6, 1, -1):
ax.plot(x, y, lw=cont, color=line.get_color(), zorder=5,
alpha=0.05)
return ax
# Styling
repo = "https://raw.githubusercontent.com/nicoguaro/matplotlib_styles/master"
style = repo + "/styles/neon.mplstyle"
plt.style.use(style)
# Plotting
x = np.linspace(0, 4, 100)
y = np.sin(np.pi*x + 1e-6)/(np.pi*x + 1e-6)
plt.figure(figsize=(6, 4))
for cont in range(5):
neon_plot(x, y/(cont + 1))
plt.xlabel("One axis")
plt.ylabel("The other axis")
plt.grid(zorder=3, alpha=0.2)
plt.savefig("neon_example.png", dpi=300, bbox_inches="tight")
plt.show()
@mrmeloman
Copy link

@mrmeloman, you can change the linestyle

def neon_plot(x, y, ax=None):
    if ax is None:
        ax = plt.gca()
    line, = ax.plot(x, y, lw=1, zorder=6, ls="dashed")
    for cont in range(6, 1, -1):
        ax.plot(x, y, lw=cont, color=line.get_color(), zorder=5,
                alpha=0.05, ls="dashed")
    return ax

neon_example

Look closely at the glow, it's not consistent with the line. Can be more or less visible on different sizes

@nicoguaro
Copy link
Author

@mrmeloman, I know. I would suggest to use a solid line for the glow, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment