Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Last active July 30, 2024 19:32
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

Thank you, this looks really pretty!
Any ideas on how to make it support linestyles? I'm trying with dashed and it won't look good since the glow lines change lw, so dashed glow is misplaced relative to the main line. I tried to solve it myself, but I'm not very smart. This looks like it might help though, in case you'll want to look into it.

@nicoguaro
Copy link
Author

@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

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