mplcyberpunk Documentation

repository·main·Indexed 23 days ago

https://github.com/dhaitz/mplcyberpunk

An add-on for Matplotlib version 0.7.6 that produces 'Cyberpunk' style plots. It provides a "cyberpunk" stylesheet for dark backgrounds and neon aesthetics, along with functions to add glow effects to lines and scatter plots, underglow and gradient fills for lines, and gradient effects for bar charts.

Tokens
1.4K
Snippets
4
Records
13
Agent score
34%

What's inside mplcyberpunk

  1. Apply the cyberpunk stylesheet

    main

    Once mplcyberpunk is imported, you can apply the dark background and cyberpunk theme to your matplotlib plots using the standard plt.style.use method with the "cyberpunk" identifier.

    import matplotlib.pyplot as plt
    import mplcyberpunk
    
    plt.style.use("cyberpunk")
  2. Add glow effects to line plots

    main

    To add the signature line glow and 'underglow' effects to a plot, call mplcyberpunk.add_glow_effects() after defining your lines but before calling plt.show(). Note that this effect is currently only implemented for lines.

    import matplotlib.pyplot as plt
    import mplcyberpunk
    
    plt.style.use("cyberpunk")
    
    plt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')
    plt.plot([4, 5, 5, 7, 9, 8, 6], marker='o')
    
    mplcyberpunk.add_glow_effects()
    
    plt.show()
  3. Add gradient effects to bar charts

    main

    To add a gradient effect to bar charts, pass the bar container object (returned by plt.bar) to mplcyberpunk.add_bar_gradient().

    import matplotlib.pyplot as plt
    import mplcyberpunk
    
    plt.style.use('cyberpunk')
    
    categories = ['A', 'B', 'C', 'D', 'E']
    values = [25, 67, 19, 45, 10]
    colors = ["C0", "C1", "C2", "C3", "C4"]
    
    bars = plt.bar(categories, values, color=colors, zorder=2)
    
    mplcyberpunk.add_bar_gradient(bars=bars)
    
    plt.show()
  4. Customize line glow and underglow

    main

    You can control the glow effects with more granularity by calling the individual functions:

    • mplcyberpunk.make_lines_glow(ax=None, lines=None): Adds the glow effect to lines. You can pass a specific matplotlib.axes.Axes object or a Line2D object (or a list of them) to target specific lines.
    • mplcyberpunk.add_underglow(ax=None): Adds only the underglow effect.
  5. Configure gradient glow and fills

    main

    You can enable gradient fills for the underglow effect using the following methods:

    • Combined: Use mplcyberpunk.add_glow_effects(gradient_fill=True) to add both line glow and gradient underglow.
    • Independent: Use mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5, gradient_start=...) to add only the gradient fill. The alpha_gradientglow parameter controls transparency, and gradient_start allows you to adjust the starting value of the gradient.
  6. Add glow to scatter plots with `make_scatter_glow()`

    main

    Add a glow effect to the dots in a scatter plot. Each dot is redrawn multiple times with increasing size to create the glow.

    Parameters:

    • ax: The Matplotlib axes. Defaults to plt.gca().
    • n_glow_lines: Number of glow layers. Defaults to 10.
    • diff_dotwidth: The multiplier for dot size in each successive layer. Defaults to 1.2.
    • alpha: The base alpha value for the glow layers. Defaults to 0.3.
  7. Add gradient fills under lines with `add_gradient_fill()`

    main

    Add a sophisticated gradient fill under or above lines in an axis. This provides a smoother aesthetic than standard underglow.

    Parameters:

    • ax: The Matplotlib axes. Defaults to plt.gca().
    • alpha_gradientglow: If a float, the gradient goes from 0 to this value. If a tuple[float, float], the gradient goes from the first value to the second. Defaults to 1.0.
    • gradient_start: Determines where the gradient is minimal. Options:
      • 'min': Fills below the curve (minimum of curve).
      • 'max': Fills above the curve (maximum of curve).
      • 'bottom': Fills from the bottom of the figure.
      • 'top': Fills from the top of the figure.
      • 'zero': Fills both above and below the curve.
    • N_sampling_points: Number of sampling points for the gradient. Higher values look better but cost more performance. Defaults to 50.
  8. Add glow effects to lines with `add_glow_effects()`

    main

    Apply a cyberpunk glow effect to lines in a Matplotlib axis. This function adds a glow effect to the lines themselves and either an 'underglow' (faintly coloring the area below the line) or a gradient fill under the line.

    By default, it uses add_underglow(). To use a gradient instead, set gradient_fill=True.

  9. Customize line glow with `make_lines_glow()`

    main

    Manually add a glow effect to specific Line2D objects or all lines in an axis. Each line is redrawn multiple times with increasing width and low alpha to simulate a glow.

    Parameters:

    • ax: The Matplotlib axes. Defaults to plt.gca().
    • n_glow_lines: Number of glow layers to create. Defaults to 10.
    • diff_linewidth: The increment in width for each successive glow layer. Defaults to 1.05.
    • alpha_line: The base alpha value for the glow layers. Defaults to 0.3.
    • lines: A single Line2D object or a list of Line2D objects to apply the effect to.