Save Matplotlib Plots as PDF in Python

matplotlib save as pdf

When working with data visualization in Python, Matplotlib is my go-to library. Over the years, I’ve found that saving plots as PDF files is an essential step, especially when preparing reports or presentations for clients, colleagues, or stakeholders. PDFs maintain vector quality, ensuring your charts look crisp regardless of zoom or print size, something that’s … Read more >>

How to Change the Title Font Size in Matplotlib

Matplotlib title font size examples: three subplots with titles at size 10, x-large and bold 16

To change the title font size in Matplotlib, pass fontsize to the title function: plt.title(“My title”, fontsize=20), or ax.set_title(“My title”, fontsize=20) when you work with axes. The size can be a number of points or a name like “large”. This guide covers the title font size for single plots and subplots, bold titles, the figure … Read more >>

Matplotlib savefig() Saves a Blank Image? 7 Causes and Fixes

Matplotlib plt.savefig saves a blank image: save before plt.show() or use fig.savefig()

plt.savefig() saves a blank image when the figure it saves is no longer the one you drew on. The most common reason is calling it after plt.show(): when you close the plot window, Matplotlib closes the figure, and plt.savefig() then saves a brand-new empty one. The fix is to save first, or to keep the … Read more >>

Save Matplotlib Plots as PNG Images in Python

save png python

When I was working with data visualization tools. Matplotlib is one of my go-to libraries when it comes to creating insightful charts and graphs. A common task I often encounter is saving these visualizations as images, especially in PNG format, which is widely used for reports, presentations, and web applications. If you’ve ever wondered how … Read more >>

Add Labels to Matplotlib Bar Charts

matplotlib bar label

While working on a project for my clients, I’ve found that data visualization is essential for communicating insights effectively. Bar charts, in particular, are a go-to choice for comparing categories. But one thing I always make sure to include is value labels on bars. They make charts instantly clearer and more professional, especially when presenting … Read more >>

How to Add Text to a Plot in Matplotlib (Text Box, Annotate)

Add text to a plot in Matplotlib: a histogram with a statistics text box in the top right corner

To add text to a plot in Matplotlib, call plt.text(x, y, “text”) or ax.text(x, y, “text”), where x and y are the position in data coordinates. Add bbox=dict(…) to put the text in a box, transform=ax.transAxes to place it relative to the axes, fig.text() to write outside the plot, and annotate() to point at a … Read more >>

How to Plot Error Bars in Matplotlib (plt.errorbar)

Error bars in Matplotlib: a bar chart of three groups with standard error of the mean error bars and caps

To plot error bars in Matplotlib, use plt.errorbar(x, y, yerr=errors); add xerr for horizontal error bars and capsize for the little caps. For a bar chart, pass yerr to plt.bar(). This guide covers plt.errorbar step by step: styling, error bars without a connecting line, asymmetric error bars, calculating the error (standard deviation or standard error) … Read more >>

How to Remove Ticks and Tick Labels in Matplotlib

Remove ticks and tick labels in Matplotlib: four subplots comparing default ticks, no ticks, labels removed and tick marks removed

To remove tick labels in Matplotlib but keep the tick marks, use ax.tick_params(labelbottom=False, labelleft=False). To remove the ticks and their labels together, use ax.set_xticks([]) and ax.set_yticks([]) (or plt.xticks([]) with pyplot), and to hide everything including the frame, use ax.axis(“off”). This guide compares all of these on real plots, then shows how to remove tick labels … Read more >>

How to Change the Background Color in Matplotlib

Change the background color in Matplotlib: a line chart with a light blue figure background and a cream axes background

To change the background color in Matplotlib, set the facecolor: ax.set_facecolor(“lightyellow”) colours the plot area inside the axes, and fig.set_facecolor(“lightblue”) (or plt.figure(facecolor=…)) colours the figure around it. To change the default background color for every plot, set plt.rcParams[“axes.facecolor”] and plt.rcParams[“figure.facecolor”]. This guide covers all of these, plus dark mode with dark_background, transparent PNGs, subplots, the … Read more >>

Matplotlib Scatter Marker: Shape, Size and Color

Matplotlib scatter marker: a reference chart showing every marker style and its code

Three arguments control how a Matplotlib scatter marker looks, and each one does a single job: s is the one that surprises people. It is an area, so s=400 is twice as wide as s=100, not four times. Every chart below is a real Matplotlib window from Python 3.12.5, Matplotlib 3.11.2, NumPy 2.5.3. Every Matplotlib … Read more >>