Matplotx
:bar_chart: More styles and useful extensions for Matplotlib
Install / Use
/learn @nschloe/MatplotxREADME
This package includes some useful or beautiful extensions to Matplotlib. Most of those features could be in Matplotlib itself, but I haven't had time to PR yet. If you're eager, let me know and I'll support the effort.
Install with
pip install matplotx[all]
and use in Python with
import matplotx
See below for what matplotx can do.
Clean line plots (dufte)
<a href="tests/dufte_comparison.py"> <table width="100%"> <tr> <td width="33%"><img src="https://nschloe.github.io/matplotx/ex1-mpl.svg"/></td> <td width="33%"><img src="https://nschloe.github.io/matplotx/ex1-dufte.svg"/></td> <td width="33%"><img src="https://nschloe.github.io/matplotx/ex1-dufte-dracula.svg"/></td> </tr> <tr> <td>matplotlib</td> <td> <code>matplotx.styles.dufte</code>, <code>matplotx.ylabel_top</code>, <code>matplotx.line_labels</code> </td> <td> <code>matplotx.styles.duftify(matplotx.styles.dracula)</code> </td> </tr> </table> </a>The middle plot is created with
import matplotlib.pyplot as plt
import matplotx
import numpy as np
# create data
rng = np.random.default_rng(0)
offsets = [1.0, 1.50, 1.60]
labels = ["no balancing", "CRV-27", "CRV-27*"]
x0 = np.linspace(0.0, 3.0, 100)
y = [offset * x0 / (x0 + 1) + 0.1 * rng.random(len(x0)) for offset in offsets]
# plot
with plt.style.context(matplotx.styles.dufte):
for yy, label in zip(y, labels):
plt.plot(x0, yy, label=label)
plt.xlabel("distance [m]")
matplotx.ylabel_top("voltage [V]") # move ylabel to the top, rotate
matplotx.line_labels() # line labels to the right
plt.show()
The three matplotx ingredients are:
matplotx.styles.dufte: A minimalistic stylematplotx.ylabel_top: Rotate and move the the y-labelmatplotx.line_labels: Show line labels to the right, with the line color
You can also "duftify" any other style (see below) with
<!--pytest.mark.skip-->matplotx.styles.duftify(matplotx.styles.dracula)
Further reading and other styles:
- <img src="https://nschloe.github.io/matplotx/data-ink.webp" width="50%"/>
Clean bar plots
<a href="tests/dufte_comparison.py"> <table width="100%"> <tr> <td width="33%"><img src="https://nschloe.github.io/matplotx/bars-mpl.svg"/></td> <td width="33%"><img src="https://nschloe.github.io/matplotx/bars-dufte1.svg"/></td> <td width="33%"><img src="https://nschloe.github.io/matplotx/bars-dufte2.svg"/></td> </tr> <tr> <td>matplotlib</td> <td>dufte</td> <td>dufte with <code>matplotx.show_bar_values()</code></td> </tr> </table> </a>The right plot is created with
import matplotlib.pyplot as plt
import matplotx
labels = ["Australia", "Brazil", "China", "Germany", "Mexico", "United\nStates"]
vals = [21.65, 24.5, 6.95, 8.40, 21.00, 8.55]
xpos = range(len(vals))
with plt.style.context(matplotx.styles.dufte_bar):
plt.bar(xpos, vals)
plt.xticks(xpos, labels)
matplotx.show_bar_values("{:.2f}")
plt.title("average temperature [°C]")
plt.show()
The two matplotx ingredients are:
matplotx.styles.dufte_bar: A minimalistic style for bar plotsmatplotx.show_bar_values: Show bar values directly at the bars
Extra styles
matplotx contains numerous extra color schemes, e.g., Dracula, Nord, gruvbox, and Solarized, the revised Tableau colors.
<!--pytest.mark.skip-->import matplotlib.pyplot as plt
import matplotx
# use everywhere:
plt.style.use(matplotx.styles.dracula)
# use with context:
with plt.style.context(matplotx.styles.dracula):
pass
| <img src="https://nschloe.github.io/matplotx/aura-dark-soft.svg" width="100%"> | | :----------------------------------------------------------------------------------: | | <img src="https://nschloe.github.io/matplotx/dracula.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/gruvbox-dark.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/pitaya-smoothie-dark.svg" width="100%"> |
<details> <summary>Many more styles here...</summary>| <img src="https://nschloe.github.io/matplotx/aura-dark.svg" width="100%"> | | :-----------------------------------------------------------------------------------: | | <img src="https://nschloe.github.io/matplotx/ayu-dark.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/ayu-light.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/ayu-mirage.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/challenger-deep.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/github-dark.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/github-dimmed.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/github-light.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/gruvbox-light.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/nord.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/one-dark.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/pacoty.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/pitaya-smoothie-light.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/solarized-dark.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/solarized-light.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/tableau-10.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/tableau-20.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/tokyo-night-day.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/tokyo-night-night.svg" width="100%"> | | <img src="https://nschloe.github.io/matplotx/tokyo-night-storm.svg" width="100%"> |
</details>Other styles:
- John Garrett, Science Plots
- Dominik Haitz, Cyberpunk style
- Dominik Haitz, Matplotlib stylesheets
- Carlos da Costa, The Grand Budapest Hotel
- Danny Antaki, vaporwave aesthetics
- QuantumBlack Labs, QuantumBlack
Smooth contours
| <img src="https://nschloe.github.io/matplotx/mpl-contourf.svg" width="100%"> | <img src="https://nschloe.github.io/matplotx/matplotx-contours.svg" width="100%"> |
| :--------------------------------------------------------------------------: | :-------------------------------------------------------------------------------: |
| plt.contourf | matplotx.contours() |
Sometimes, the sharp edges of contour[f] plots don't accurately represent the
smoothness of the function in question. Smooth contours, contours(), serves as a drop-in replacement.
import matplotlib.pyplot as plt
import matplotx
def rosenbrock(x):
return (1.0 - x[0]) ** 2 + 100.0 * (x[1] - x[0] ** 2) ** 2
im = matplotx.contours(
rosenbrock,
(-3.0, 3.0, 200),
(-1.0, 3.0, 200),
log_scaling=True,
cmap="viridis",
outline="white",
)
plt.gca().set_aspect("equal")
plt.colorbar(im)
plt.show()
Contour plots for functions with discontinuities
| <img src="https://nschloe.github.io/matplotx/contour-mpl.svg" width="100%"> | <img src="https://nsc
