Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter notebook- how to scale plot to fill entire width of page? (while preserving aspect ratio)

I have a Jupyter/IPython notebook with a graph.

I am using the following code to display the graph, while keeping the aspect ratio square so the graph does not get distorted.

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_aspect('equal')
ax.plot(x,y)

This works, but I want to scale the graph so its width takes up the entire width of the notebook page. How can I do this?

like image 383
888 Avatar asked May 03 '17 00:05

888


People also ask

Does Jupyter Notebook change the size of the images?

However, the size of the images has changed with a version of Jupyter or matplotlib. The images have become smaller. You can see this by opening and running an example notebook. Matplotlib displays the images smaller than in the example.

Is it possible to scale the plot size of Matplotlib plots in Jupyter notebooks?

Bookmark this question. Show activity on this post. Is there a possibility to scale the plot size of matplotlib plots in jupyter notebooks? You could increase the plot size by changing the default values of figure.figsize, but this does not affect parameters like fontsize, linewidth, markersize etc.

How do I control the layout of a page with Jupyter book?

There are a few ways to control the layout of a page with Jupyter Book. Many of these ideas take inspiration from the Edward Tufte layout CSS guide. Let’s begin with a sample plot. You can click the toggle button to the right to see the code that generated it.

Can Matplotlib be used with Jupyter?

Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. – matplotlib Within a Jupyter notebook, the data can be visualized with matplotlib. However, the size of the images has changed with a version of Jupyter or matplotlib.


1 Answers

You need to set proper (width,height) of figsize:

%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
import sys

# Plot a random line that fill whole width of the cell in jupyter notebook
# need ranges of x, y to get proper (width, height) of figure

x = np.random.randn(5)   # prep data to plot
y = np.random.randn(5)
xmin,xmax = min(x),max(x)
ymin,ymax = min(y),max(y)

yox = None
if (xmax-xmin)!=0:
    yox = (ymax-ymin)/(xmax-xmin)

# set number that should spans cell's width
pwidth = 20    # inches

if yox>1.0:
    # tall figure
    width, height = pwidth, pwidth*yox
elif yox==1.0:
    width, height = pwidth, pwidth
elif yox<1.0:
    # wide figure
    width, height = pwidth*yox, pwidth
    if width<pwidth:
        height = height/width*pwidth
        width = pwidth
else:
    sys.exit

fig = plt.figure(figsize=(width, height))  # specify (width,height) in inches
ax = fig.add_subplot(1,1,1)
ax.set_aspect('equal') # preserve aspect ratio
ax.plot( x, y )        # should fill width of notenook cell

plt.show()
like image 55
swatchai Avatar answered Sep 19 '22 07:09

swatchai