Matplotlib Legend Outside the Plot: fig.legend and bbox_to_anchor

Matplotlib legend outside the plot: a real chart window with the legend placed to the right using constrained layout

Putting a Matplotlib legend outside the plot takes two things: a figure made with layout="constrained", and fig.legend(loc="outside right upper"). The layout engine then shrinks the plot to make room, so nothing gets cut off: Most tutorials still teach the older bbox_to_anchor trick, which works but tends to crop the legend. I’ll show both and measure … Read more >>

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 >>