A zero-dependency plotting utility that implements vectorized gradient rendering for Matplotlib Figure and Axes objects. Unlike other styling libraries that require heavy dependencies (like Pillow or Qt), mpl-gradients leverages NumPy broadcasting to generate gradient arrays directly within the Matplotlib canvas.
This approach ensures compatibility with all Matplotlib backends while maintaining a minimal footprint. Key features include support for variable alpha channels (transparency), arbitrary gradient angles, and multi-color transitions, all optimized for rendering speed.
pip install mpl-gradients
## Features
* **Vertical Gradients:** Fade from Top to Bottom.
* **Horizontal Gradients:** Fade from Left to Right.
* **Diagonal Gradients:** Fade from Corner to Corner.
* **Alpha Blending:** Correctly handles transparency.
## Installation
You can install directly from GitHub:
```bash
pip install mpl-gradientsimport matplotlib.pyplot as plt
from mpl_gradients import LinearGradient
fig, ax = plt.subplots()
ax.bar([0, 1, 2], [10, 20, 15])
# Create a gradient (Top-Left Navy -> Bottom-Right Lime)
gradient = LinearGradient("navy", "lime", direction="diagonal")
# Apply to bars
for bar in ax.containers[0]:
bar.set_agg_filter(gradient)
plt.show()Python 3.9+
Matplotlib
Numpy
You can now create gradients that fade to transparent!
By default, gradients preserve the original alpha of the plot (preserve_alpha=True).
To create transparent gradients (e.g., Red -> Transparent), set preserve_alpha=False.
from mpl_gradients import LinearGradient
# Create a gradient that fades from Red to Transparent to Green
gradient = LinearGradient.from_colors(
["red", "#ffffff00", "green"],
preserve_alpha=False
)
# Apply it
ax.fill_between(x, y, color="blue").set_agg_filter(gradient)