5 Best Ways to Adjust Plot Width Settings in IPython Notebook

Rate this post

πŸ’‘ Problem Formulation: When working with visualizations in IPython Notebooks, a common challenge is adjusting the plot width so that the visualizations fit well within the confines of the notebook’s cell. This article will delve into methods for altering the plot width to enhance visual comprehension and improve the aesthetic appeal of the plots. For instance, a user might want their Matplotlib plot to span a specific width that aligns with their presentation or report layout.

Method 1: Using Matplotlib’s rcParams

To adjust the plot width in an IPython notebook, one effective method is manipulating Matplotlib’s rcParams. This configuration dictionary contains default styles for all Matplotlib plots, which means setting the figure size globally. By changing the figure size parameter, you can easily control the width of your plots.

Here’s an example:

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [10, 5]
plt.plot([1, 2, 3], [4, 5, 6])

The output is a plot that spans a width of 10 inches and a height of 5 inches.

This code snippet changes the default settings of figure size for Matplotlib plots by setting the figure.figsize parameter. The value is a list where the first element specifies the width and the second element the height. Subsequent plots will adhere to these dimension settings unless overridden.

Method 2: Adjusting Plot Size per Plot with figsize

If you need to change the plot width for just one plot rather than globally, you can do so when creating the figure object. Using the figsize attribute allows for precision on a per-plot basis, without affecting other visualizations within your IPython notebook.

Here’s an example:

fig, ax = plt.subplots(figsize=(12, 6))
ax.plot([1, 2, 3], [4, 5, 6])

The output is a plot that spans a width of 12 inches and a height of 6 inches.

This approach creates a new figure with a specific size using the figsize argument within subplots(). The resulting plot dimensions only apply to this particular instance, preserving the original settings for other plots.

Method 3: Use %matplotlib Magic Command in IPython

IPython notebooks support magic commands, which are prefixed by a percentage sign (%). The %matplotlib magic command, followed by the “inline” backend option, can be used in combination with other settings to adjust plot dimensions conveniently.

Here’s an example:

%matplotlib inline
%config InlineBackend.figure_format = 'svg'
%config InlineBackend.rc = {'figure.figsize': (8, 4)}

The output is a scalable vector graphic (SVG) image of the plot with an 8-inch width and 4-inch height.

The %config command is used to set inline backend configuration options, including the plot size. SVG format ensures that the plot remains sharp at any size.

Method 4: Using output_notebook() from Bokeh

Bokeh is an interactive visualization library that provides elegant, concise construction of versatile graphics. With Bokeh, adjusting the plot width is as straightforward as setting it directly on the figure object. The output_notebook() function integrates Bokeh plots within IPython notebooks.

Here’s an example:

from bokeh.plotting import figure, output_notebook, show


p = figure(plot_width=700, plot_height=400)
p.line([1, 2, 3], [4, 6, 5])


You’ll get an interactive Bokeh plot embedded in the IPython notebook with the specified dimensions.

This code creates a Bokeh plot with a specified plot_width and plot_height passed as arguments to the figure() function. The result is an interactive and resizable plot.

Bonus One-Liner Method 5: Using IPython’s Display Function

A quick way to set plot dimensions is by using IPython’s display tools to embed images with custom width using HTML. This method is particularly useful when your plots are static images.

Here’s an example:

from IPython.display import Image, display

display(Image(filename='example.png', width=500))

This will output the image ‘example.png’ with a specific width of 500 pixels.

The Image class from IPython’s display module enables IPython Notebook to display images with adjustable width and height parameters directly without affecting the plot’s source settings.


  • Method 1: Matplotlib’s rcParams. Affects all subsequent plots. Quick global setting change. May not be suitable for granular control.
  • Method 2: figsize per plot. Provides individual control. Settings are isolated to single instances. Requires more code per plot.
  • Method 3: IPython magic commands. Convenient and notebook-oriented. Adjustments can be specific to the notebook session. Can be less intuitive for newcomers.
  • Method 4: Bokeh’s output_notebook(). Ideal for interactive plots. Comes with high-level granularity. Requires familiarity with Bokeh library.
  • Method 5: IPython’s Display Function. Quick and simple for static images. Limited to image-based plots. Does not affect plot generation settings.