Scale plot size of Matplotlib Plots in Jupyter Notebooks - python

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. What I need is a plot where all the parameters are scaled accordingly.
P.S.: To display plots in jupyter notebooks I use %matplotlib inline, see screenshot below.
Edit
For completeness, here is a code snippet doing exactly what I needed:
def scale_plot_size(factor=1.5):
import matplotlib as mpl
default_dpi = mpl.rcParamsDefault['figure.dpi']
mpl.rcParams['figure.dpi'] = default_dpi*factor

You don't want to change the figure size. You want to change the dpi (dots per inch).
Also see Relationship between dpi and figure size.
import matplotlib.pyplot as plt
%matplotlib inline
def plot(dpi):
fig, ax=plt.subplots(dpi=dpi)
ax.plot([2,4,1,5], label="Label")
ax.legend()
for i in range(1,4):
plot(i*72)

Related

Python - save a figure with the same size and setting

I plot figures a lot during my python (through Spyder env.) usage. However, when I try to use plt.savefig('figure.png'), the saved figure has a different size from the inline figure plotted on Spyder.
For ex., when I use this command:
plt.savefig('fig1.png')
The saved figure looks like this:
Note that there's something weird with the saved figure, e.g.: the title is cropped, the size is not proportional.
However, the following is the inline figure:
I tried to modify the size through matplotlib.pyplot documentation but couldn't find such setting. Does anyone know how to save the figure with the exact setting as the inline plot?
The inline figure size plotted in Spyder (or any other IDE or editor) depends on how the editor handles showing figures.
If you want to have an exact size as output of your code, use figsize before plotting code. (It uses inches)
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
# Code to produce figure
You can also determine DPI when creating figure or saving.
plt.figure(figsize=(10, 10), dpi=300)
# or
plt.savefig(file_path, dpi=300)

Scale plot size of matplotlib plots in Qubole Notebook

Is there a possibility of increasing the size of the plot plotted using z.showplot() in qubole notebooks.
import matplotlib as plt
plt.figure()
plt.bar(pandas_df_hr_sg[:]['hour'],pandas_df_hr_sg[:]['count'])
plt.title('Hourly wise user visits')
plt.xlabel('Hour')
plt.ylabel('Visit Count')
z.showplot(plt)
Could you try setting the plot size by:
plt.figure(figsize=(8, 8))
This should ideally help in case of Zeppelin notebook in Qubole; let me know how this goes!

Matplotlib: How to prevent Jupyter from squeezing a large graph into a single cell?

Any time I try to plot a large graph in Jupyter with Matplotlib, where I would wish to scroll around the large image in the cell it has been rendered in, the notebook seems to squeeze the plot down to fit within its cell.
Normally, this is an optimal behavior, but how could I turn this off when I need to? How could I make plots display in their correct dimensions, given the ppi of one's screen?
Are you using the magic function %matplotlib inline ?
If so, you may be able to adjust either figure size or resolution to resize accordingly.
Adjust Figure Size (tends to distort text):
import matplotlib.pyplot as plt
%matplotlib inline
fig=plt.figure(figsize=(15, 15))
Global Option:
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = [15, 15]
Adjust Resolution (doesn't distort text):
import matplotlib.pylab as plt
%matplotlib inline
plt.rcParams['figure.dpi'] = 200
Edit
Use the dropdowns: cell > all output > toggle scrolling (see screenshot)

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.
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.
plt.gcf().set_size_inches(16, 8)
After change figsize the figure size do changed when the parameter in a certain range.In my condition,size not growing after size above (24,8).When it's still below the range the size do increase.It's base on your displayer dpi, you can set the dpi in figure but eventually it's rely on your hardware.
The figaspect is set by matplotlib.figure.figaspect
If you save figures to files use savefig,you will see the image size increase also.

Plot size changes on saving plot in matplotlib

I have plotted a graph with 14 subplots in matplotlib. In the window the plot looks like this-
I save this plot using following command-
import matplotlib.pyplot as plt
plt.savefig('img.png')
But the image that is saved looked like this-
Notice that the x axis labels get overlapped because the image is shrinked. The savefig() function has optional argument dpi, but it changes the resolution/quality of saved plot.
I also tried this, but it is used to improve image resolution.
I want the axis labels to be nicely spaced as in the window. Thanks
Ok, So I found the solution myself and posting it here for anyone who might face similar problem. I changed the figure size before saving and following code does the trick-
import matplotlib.pyplot as plt
fig =plt.gcf()
fig.set_size_inches(20, 11,dpi=100)
plt.savefig('img.png')

Categories

Resources