Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change figsize in matplotlib

It seems that the figsize option only changes the ratio of the height to width. Atleast this is the case when using jupyter notebooks. Here is an example:

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

plt.figure(figsize=(16,8))
plt.plot(np.arange(1,10),np.arange(1,10))
plt.show()
plt.figure(figsize=(24,6))
plt.plot(np.arange(1,10),np.arange(1,10))
plt.show()

I was hoping that figsize intended inches, not a relative ratio. How would you go about enforcing that in python/ jupyter notebooks.

like image 607
sachinruk Avatar asked Aug 23 '26 13:08

sachinruk


2 Answers

If you use a large figsize, say figsize=(50, 5) you will notice that the lines, the labels, everything is incredibly thin and small with respect to a plot with normal size.

This happens because you are using widths that are not compatible with the width of the output cell and the notebook just scales down the figure to make it fit in the output cell.

To have the behavior you asked for, you need a horizontal scrolling capability in the output cell. I don't know of a `nbextension` that can enable horizontal scrolling in output cells.

After a bit of experimenting, it looks like using the nbagg backend

%matplotlib nbagg

gives you a scrollable output cell, and an interactive one as well, inside the notebook and possibly it is what you want.


Addendum

I've found this issue on IPython's github, with a request for horizontal scrolling in output cell — as you can see it's dated 2012 and there is no followup of sort.

like image 128
gboffi Avatar answered Aug 25 '26 04:08

gboffi


plt.gcf().set_size_inches(16, 8)
like image 29
Kapil Sharma Avatar answered Aug 25 '26 02:08

Kapil Sharma