Explain Codes LogoExplain Codes Logo

How to Make Inline Plots in Jupyter Notebook Larger?

python
matplotlib
plot-sizing
jupyter-notebook
Alex KataevbyAlex Kataev·Dec 16, 2024
TLDR

Increase the size of inline plots in Jupyter with just one line. Set matplotlib's figure.figsize:

plt.rcParams['figure.figsize'] = [12, 8] # Plot size is now 12x8 "virtual" inches

Write this at the beginning of your Jupyter notebook to ensure all your plots are consistently larger by default.

Size matters: understanding width and height in Matplotlib

While matplotlib defines plot size in inches, remember these are "virtual" inches – not the real-world measurement.

To adjust the size of each individual plot, use the figsize parameter:

fig, ax = plt.subplots(figsize=(14, 10)) # The plot's size is now 14x10 "virtual" inches

In the above line, (14, 10) represents the width and height of your plot in the digital realm.

High resolution: improving sharpness with dpi

In matplotlib, dpi stands for dots per inch. A higher dpi means crisper images that "survive" zooming in or presenting intricate details:

plt.figure(figsize=(8, 6), dpi=120) # Avengers may assemble, but so do our pixels

Adjusting dpi is handy when your visualizations show detailed heatmaps or scatter plots where pixel density impacts clarity.

Interactivity: using notebook and widget backends

When static sizes don't cut it, consider interactive backends. %matplotlib notebook for Jupyter Notebook or %matplotlib widget for Jupyter Lab provide interactive, resizable plots.

%matplotlib notebook # Use Zoom and do not get sued by DC Comics

Remember to restart your kernel when toggling between these backends to avoid any Matrix-like glitches.

Customizing your plot's palette and style

Maintaining a visual style across your plots is fairly easy. Customizing facecolor and edgecolor parameters in plt.rcParams will elevate your plots from the standard blacks and whites.

plt.rcParams['figure.facecolor'] = 'lightgrey' # Sophisticated background tone plt.rcParams['axes.edgecolor'] = 'blue' # Edges that pop colorfully

These adjustments ensure your plots are noticeably your own.

Plot sizing for R users

For R language enthusiasts, adjusting plot sizes in a Jupyter notebook running the R kernel is as easy as the options() function:

options(repr.plot.width=12, repr.plot.height=8) # Plot size is now R-sized!

Balancing size and notebook performance

Larger plots mean higher rendering time, which may slow down your notebook. Find the sweet spot between high detail and quick load times to prevent viewer frustration.

The comfort zone: ensuring readability

Scaling up should improve data interpretation while considering the need for minimal scrolling. Your plots should live in harmony with your text, code, and anything else populating your notebook.