Home / matplotlib scatter colorbar / Matplotlib tips & tricks

Matplotlib tips & tricks - matplotlib scatter colorbar

Matplotlib tips & tricks-matplotlib scatter colorbar

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 a colorbar's size when adding it.
pha) in order to show area with higher density. Multiple scat-
ter plots can be used to delineate a frontier. import matplotlib.patheffects as fx im = ax.imshow(Z)
text = ax.text(0.5, 0.1, "Label")
X = np.random.normal(-1, 1, 500)
text.set_path_effects([ cb = plt.colorbar(im,
Y = np.random.normal(-1, 1, 500)
fx.Stroke(linewidth=3, foreground='1.0'), fraction=0.046, pad=0.04)
ax.scatter(X, Y, 50, "0.0", lw=2) # optional
fx.Normal()]) cb.set_ticks([])
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
Rasterization You can plot several lines at once using None as separator. You can use a condensed font such as Roboto Condensed
to save space on tick labels.
If your figure has many graphical elements, such as a huge X,Y = [], [] for tick in ax.get_xticklabels(which='both'):
scatter, you can rasterize them to save memory and keep
for x in np.linspace(0, 10*np.pi, 100): tick.set_fontname("Roboto Condensed")
X.extend([x, x, None]), Y.extend([0, sin(x), None])
other elements in vector format. ax.plot(X, Y, "black")

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)
Offline rendering
Once your figure is finished, you can call tight_layout()
Dotted lines
to remove white margins. If there are remaining margins,
you can use the pdfcrop utility (comes with TeX live).
To have rounded dotted lines, use a custom linestyle and
Use the Agg backend to render a figure directly in an array. modify dash_capstyle. Hatching
from matplotlib.backends.backend_agg import FigureCanvas ax.plot([0,1], [0,0], "C1", You can achieve a nice visual effect with thick hatch pat-
canvas = FigureCanvas(Figure())) linestyle = (0, (0.01, 1)), dash_capstyle="round") terns. 59%
... # draw some stuff ax.plot([0,1], [1,1], "C1", 53%
canvas.draw() linestyle = (0, (0.01, 2)), dash_capstyle="round") cmap = plt.get_cmap("Oranges") 38%
Z = np.array(canvas.renderer.buffer_rgba()) plt.rcParams['hatch.color'] = cmap(0.2) 27%
plt.rcParams['hatch.linewidth'] = 8
ax.bar(X, Y, color=cmap(0.6), hatch="" )
2018 2019
Range of continuous colors
Combining axes Read the documentation
You can use colormap to pick from a range of continuous
colors. You can use overlaid axes with different projections. Matplotlib comes with an extensive documentation explain-
X = np.random.randn(1000, 4) ax1 = fig.add_axes([0,0,1,1],
ing the details of each command and is generally accom-
cmap = plt.get_cmap("Oranges") label="cartesian") panied by examples. Together with the huge online gallery,
colors = cmap([0.2, 0.4, 0.6, 0.8]) ax2 = fig.add_axes([0,0,1,1], this documentation is a gold-mine.
label="polar", Matplotlib 3.5.0 handout for tips & tricks. Copyright (c) 2021 Matplotlib Development
ax.hist(X, 2, histtype='bar', color=colors) projection="polar") Team. Released under a CC-BY 4.0 International License. Supported by NumFOCUS.

How do you plot a scatter plot in MATLAB?scatter (x,y) creates a scatter plot with circular markers at the locations specified by the vectors x and y. To plot one set of coordinates, specify x and y as vectors of equal length. To plot multiple sets of coordinates on the same set of axes, specify at least one of x or y as a matrix. scatter (x,y,sz) specifies the circle sizes.