0% found this document useful (0 votes)
66 views1 page

PLT Tips

The document discusses various matplotlib tips and tricks including using transparency in scatter plots, setting text outlines, adjusting colorbars, plotting multiple lines, rasterizing figures, offline rendering, range of continuous colors, combining different axis projections, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views1 page

PLT Tips

The document discusses various matplotlib tips and tricks including using transparency in scatter plots, setting text outlines, adjusting colorbars, plotting multiple lines, rasterizing figures, offline rendering, range of continuous colors, combining different axis projections, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Matplotlib tips & tricks

Transparency Text outline Colorbar adjustment


Scatter plots can be enhanced by using transparency (al- Use text outline to make text more visible. You can adjust colorbar aspect when adding it.
pha) in order to show area with higher density and multiple
import matplotlib.patheffects as fx im = ax.imshow(Z)
scatter plots can be used to delineate a frontier. text = ax.text(0.5, 0.1, ”Label”)
text.set_path_effects([ cb = plt.colorbar(im,
X = np.random.normal(-1,1,500) fx.Stroke(linewidth=3, foreground=’1.0’), fraction=0.046, pad=0.04)
Y = np.random.normal(-1,1,500) fx.Normal()]) cb.set_ticks([])
ax.scatter(X, Y, 50, ”0.0”, lw=2) # optional
ax.scatter(X, Y, 50, ”1.0”, lw=0) # optional
ax.scatter(X, Y, 40, ”C1”, lw=0, alpha=0.1)
Multiline plot Taking advantage of typography
You can use a condensed face such as Roboto Condensed
You can plot several lines at once using None as separator.
Rasterization to save space on tick labels.
X,Y = [], []
for tick in ax.get_xticklabels(which=’both’):
If your figure is made of a lot graphical elements such as a for x in np.linspace(0, 10*np.pi, 100):
tick.set_fontname(”Roboto Condensed”)
huge scatter, you can rasterize them to save memory and X.extend([x, x, None]), Y.extend([0, sin(x), None])
ax.plot(X, Y, ”black”)
keep other elements in vector format.                    
     
X = np.random.normal(-1, 1, 10_000)
Y = np.random.normal(-1, 1, 10_000)
ax.scatter(X, Y, rasterized=True) Getting rid of margins
fig.savefig(”rasterized-figure.pdf”, dpi=600)
Once your figure is finished, you can call tight_layout()
to remove white margins. If there are remaining margins,
Dotted lines you can use the pdfcrop utility (comes with TeX live).
Offline rendering To have rounded dotted lines, use a custom linestyle and
modify dash_capstyle. Hatching
Use the Agg backend to render a figure directly in an array.
ax.plot([0,1], [0,0], ”C1”, You can achieve nice visual effect with thick hatch patterns.
from matplotlib.backends.backend_agg import FigureCanvas linestyle = (0, (0.01, 1)), dash_capstyle=”round”) 59%
canvas = FigureCanvas(Figure())) ax.plot([0,1], [1,1], ”C1”, cmap = plt.get_cmap(”Oranges”)
53%

... # draw som stuff linestyle = (0, (0.01, 2)), dash_capstyle=”round”) plt.rcParams[’hatch.color’] = cmap(0.2)
38%

canvas.draw() plt.rcParams[’hatch.linewidth’] = 8
27%

Z = np.array(canvas.renderer.buffer_rgba()) ax.bar(X, Y, color=cmap(0.6), hatch=”/” )


2018 2019

Read the documentation


Range of continuous colors Combining axes
Matplotlib comes with an extensive documenation explain-
You can use colormap to pick a range of continuous colors. You can use overlaid axes with different projections. ing every detals of each command and is generally accom-
X = np.random.randn(1000, 4) ax1 = fig.add_axes([0,0,1,1],
panied by examples with. Together with the huge online
cmap = plt.get_cmap(”Blues”) label=”cartesian”) gallery, this documenation is a gold-mine.
colors = [cmap(i) for in in [.2,.4,.6,.8]] ax2 = fig.add_axes([0,0,1,1],
label=”polar”,
Matplotlib 3.2 handout for tips & tricks. Copyright (c) 2020 Nicolas P. Rougier. Re-
ax.hist(X, 2, histtype=’bar’, color=colors) projection=”polar”)
leased under a CC-BY 4.0 License. Supported by NumFocus Grant #12345.

You might also like