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()
@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