Matplotlib Figure Size – How to Change Plot Size in Python with plt.figsize()
Ihechikara Vincent Abba
When creating plots using Matplotlib, you get a default figure size of 6.4 for the width and 4.8 for the height (in inches).
In this article, you’ll learn how to change the plot size using the following:
- The figsize() attribute.
- The set_figwidth() method.
- The set_figheight() method.
- The rcParams parameter.
Let’s get started!
How to Change Plot Size in Matplotlib with plt.figsize()
As stated in the previous section, the default parameters (in inches) for Matplotlib plots are 6.4 wide and 4.8 high. Here’s a code example:
In the code above, we first imported matplotlib . We then created two lists — x and y — with values to be plotted.
Using plt.plot() , we plotted list x on the x-axis and list y on the y-axis: plt.plot(x,y) .
Lastly, the plt.show() displays the plot. Here’s what the plot would look like with the default figure size parameters:
matplotlib plot with default figure size parameters
We can change the size of the plot above using the figsize() attribute of the figure() function.
The figsize() attribute takes in two parameters — one for the width and the other for the height.
Here’s what the syntax looks like:
Here’s a code example:
We’ve added one new line of code: plt.figure(figsize=(10,6)) . This will modify/change the width and height of the plot.
Here’s what the plot would look like:
matplotlib plot with modified figure size
How to Change Plot Width in Matplotlib with set_figwidth()
You can use the set_figwidth() method to change the width of a plot.
We’ll pass in the value the width should be changed to as a parameter to the method.
This method will not change the default or preset value of the plot’s height.
Here’s a code example:
Using the set_figwidth() method, we set the width of the plot to 10. Here’s what the plot would look like:
matplotlib plot with modified width
How to Change Plot Height in Matplotlib with set_figheight()
You can use the set_figheight() method to change the height of a plot.
This method will not change the default or preset value of the plot’s width.
Here’s a code example:
Using the set_figheight() in the example above, we set the plot’s height to 2. Here’s what the plot would look like:
matplotlib plot with modified height
How to Change Default Plot Size in Matplotlib with rcParams
You can override the default plot size in Matplotlib using the rcParams parameter.
This is useful when you want all your plots to follow a particular size. This means you don’t have to change the size of every plot you create.
Here’s an example with two plots:
Using the figure.figsize parameter, we set the default width and height to 4: plt.rcParams[‘figure.figsize’] = [4, 4] . These parameters will change the default width and height of the two plots.
Here are the plots:
matplotlib plot with modified default size
matplotlib plot with modified default size
Summary
In this article, we talked about the different ways you can change the size of a plot in Matplotlib.
We saw code examples and visual representation of the plots. This helped us understand how each method can be used to change the size of a plot.
We discussed the following methods used in changing the plot size in Matplotlib:
Change Figure Size in Matplotlib
Matplotlib is one of the most widely used data visualization libraries in Python. Much of Matplotlib's popularity comes from its customization options — you can tweak just about any element from its hierarchy of objects.
In this tutorial, we'll take a look at how to change a figure size in Matplotlib.
Creating a Plot
Let's first create a simple plot in a figure:
Here, we've plotted a sine function, starting at 0 and ending at 10 with a step of 0.1 . Running this code yields:
The Figure object, if not explicitly created, is created by default and contains all the elements we can and cannot see. Changing the size of the Figure will in turn change the size of the observable elements too.
Let's take a look at how we can change the figure size.
Change Figure Size in Matplotlib
Set the figsize Argument
First off, the easiest way to change the size of a figure is to use the figsize argument. You can use this argument either in Pyplot's initialization or on an existing Figure object.
Let's first modify it during initialization:
Here, we've accessed the Figure instance that was created by default and passed the figsize argument. Note that the size is defined in inches, not pixels. This will result in a figure that's 3in by 3in in size:
Matplotlib/PyPlot don't currently support metric sizes, though, it's easy to write a helper function to convert between the two:
And then adjust the size of the plot like this:
This would create a plot with the size of 15cm by 10cm:
Alternatively, if you're creating a Figure object for your plot, you can assign the size at that time:
Here, we've explicitly assigned the return value of the figure() function to a Figure object. Then, we can add axes to this figure to create multiple subplots and plot on them.
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
We've used the add_subplot() function, which accepts a series of numerical values. The first number specifies how many rows you want to add to the figure, the second number specifies how many columns you want to add, and the third number specifies the number of the plot that you want to add.
This means that if you in passed in 111 into the add_subplots() function, one new subplot would be added to the figure. Meanwhile, if you used the numbers 221 , the resulting plot would have four axes with two columns and two rows — and the subplot you're forming is in the 1st position.
This code results in:
Set the Height and Width of a Figure in Matplotlib
Instead of the figsize argument, we can also set the height and width of a figure. These can be done either via the set() function with the figheight and figwidth argument, or via the set_figheight() and set_figwidth() functions.
The former allows you to write one line for multiple arguments while the latter provides you with code that's more readable.
Let's go with the second option:
This code results in:
Finally, you can also use the set_size_inches() function as well:
And this performs the same as setting the figsize argument or using the two functions:
Conclusion
In this tutorial, we've gone over several ways to change the size of a figure in Matplotlib.
If you're interested in Data Visualization and don't know where to start, make sure to check out our book on Data Visualization in Python.
If you're interested in Data Visualization and don't know where to start, make sure to check out our bundle of books on Data Visualization in Python:
Data Visualization in Python
Limited time discount: 2-for-1, save 50%!
✅ 30-day no-question money-back guarantee
✅ Beginner to Advanced
✅ Updated regularly for free (latest update in April 2021)
✅ Updated with bonus resources and guides
Data Visualization in Python with Matplotlib and Pandas is a book designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and allow them to build a strong foundation for advanced work with theses libraries — from simple plots to animated 3D plots with interactive buttons.
It serves as an in-depth, guide that'll teach you everything you need to know about Pandas and Matplotlib, including how to construct plot types that aren't built into the library itself.
Data Visualization in Python, a book for beginner to intermediate Python developers, guides you through simple data manipulation with Pandas, cover core plotting libraries like Matplotlib and Seaborn, and show you how to take advantage of declarative and experimental libraries like Altair. More specifically, over the span of 11 chapters this book covers 9 Python libraries: Pandas, Matplotlib, Seaborn, Bokeh, Altair, Plotly, GGPlot, GeoPandas, and VisPy.
It serves as a unique, practical guide to Data Visualization, in a plethora of tools you might use in your career.
Matplotlib increase plot size
In this Python Matplotlib tutorial, we’ll discuss the Matplotlib increase plot size in python. Here we will cover different examples related to the increase of the plot size using matplotlib. Moreover, we’ll also cover the following topics:
- Matplotlib increase plot size
- Matplotlib increase plot size jupyter
- Matplotlib increase plot size subplots
- Matplotlib pyplot set_size_inches
- Pandas matplotlib increase plot size
- Matplotlib change figure size and dpi
- Matplotlib plot change point size
- Matplotlib set plot size in pixels
- Matplotlib change default plot size
- Matplotlib change bar plot size
- Matplotlib change scatter plot size
- Matplotlib set plot window size
- Matplotlib change figure size fig ax
- Matplotlib set plot size in centimeter
Table of Contents
Matplotlib increase plot size
Plots are a great method to graphically depict data and summarise it in a visually attractive way. However, if not plotted properly, it appears to be complex.
In matplotlib, we have several libraries for the representation of data. While creating plots in matplotlib, it is important for us to correct their size, so that we properly visualize all the features of the plot.
Matplotlib increase plot size jupyter
In this section, we’ll learn to increase the size of the plot using matplotlib in a jupyter notebook.
The syntax is given below:
The above syntax is used to increase the width and height of the plot in inches. By default, the width is 6.4 and the height is 4.8.
Let’s see examples:
Example #1
Here we’ll see an example to increase the size of the plot in the jupyter notebook.
- Firstly, import the matplotlib.pyplot library
- Next, to increase the size of the plot in the jupyter notebook use plt.rcParams[“figure.figsize”] method and set width and height of the plot.
- Then, define the data coordinates used for plotting.
- To plot a graph, use the plot() function and also set the style of the line to dot-dash.
- To display the plot, on the user’s screen use the show() function.
Example #2
The source code below elaborates the process of increasing size in a jupyter notebook using matplotlib.
Here we define data coordinates by using linspace() function and sin() function of numpy module.
Matplotlib increase plot size subplots
Here we’ll learn to increase the plot size of subplots using matplotlib. There are two ways to increase the size of subplots.
- Set one size for all subplots
- Set individual sizes for subplots
Set one size for all subplots
Here we’ll see examples where we set one size for all the subplots.
The following is the syntax:
Example #1
Here is the example where we increase the same size for all the subplots.
- Import necessary libraries, such as matplotlib.pyplot, and numpy.
- Then create subplots with 2 rows and 2 columns, using the subplots() function.
- To set one size for all subplots, use the figsize() function with width and height parameters.
- Define data coordinates used for plotting.
- To plot the subplots, use the plot() function with axes.
- To set different linestyles of the plotted lines, use linestyle parameter.
- To visualize the subplots, use the show() function.
Example #2
Let’s see one more example to set the same size for all the subplots to understand the concept more clearly.
- To define data coordinates, we use linspace(), sin() and cos() functions of numpy.
- Here we increase the size of all subplots, using the figure() method and we pass the figsize() as a parameter and set the width and height of the subplots.
Set individual sizes for subplots
Here we’ll see different examples where we set individual sizes for subplots using matplotlib.
The following is the syntax:
Example:
The source code below elaborates the process of increasing the size of the individual subplots.
- Import matplotlib.pyplot library.
- Then create subplots with 1 row and 2 columns , using the subplots() function.
- To set the individual sizes for subplots, use gridspec_kw() method. The gridpec_kw is a dictionary with keywords that can be used to change the size of each grid.
- Next, define data coordinates to plot data.
- To plot a line chart, use plot() function.
- To auto adjust padding, use tight_layout() function.
- To visualize the subplots, use show() function.
Example #2
The process of increasing the size of specific subplots is explained in the source code below.
To plot a data, we define data coordinates, by using arange(), cos() and sin() functions of numpy.
Matplotlib pyplot set_size_inches
In matplotlib, to set the figure size in inches, use the set_size_inches() method of the figure module.
The following is the syntax:
Here w represents the width and h represents the height.
Let’s see different examples:
Example #1
Here is an example to increase the plot size by using the set_size_inches method.
- Import matplotlib.pyplot library.
- Next, create a new figure by using the figure() function.
- To set the figure size in inches, use the set_size_inches() function and pass the width and the height as a parameter, and set their value to 6.5 and 6 respectively.
- Then, define the x and y data coordinates used for plotting.
- To plot the line chart, use the plot() function.
Example #2
Here we create a line chart using x and y coordinates (define by arange and sin function). We set the width of the plot to 12 and the height to 10 inches by using the set_size_inches function.
Here we define x and y data coordinates by using arange(), pi, and sin() function of numpy.
Pandas matplotlib increase plot size
In Pandas, the figsize parameter of the plot() method of the matplotlib module can be used to change the size of a plot bypassing required dimensions as a tuple. It is used to calculate the size of a figure object.
The following is the syntax:
Width and height must be in inches.
Let’s see different examples:
Example #1
In this example, we’ll create a pie chart using pandas dataframe and increase the size of the plot.
- Import the pandas module.
- After this, create a pandas dataframe with an index.
- To plot a pie chart, use df.plot.pie() function.
- To increase the size of the plot, pass the figsize() parameter along with dimensions.
Example #2
We’ll make a line chart with a pandas dataframe and increase the plot size in this example.
- Create a dataframe in pandas using the DataFrame() method.
- Next, plot the DataFrame using df.plot() function.
- Here we set the kind parameter to the line in order to plot the line chart.
- To increase the size of the plot, pass the figsize() parameter along with dimensions.
Matplotlib change figure size and dpi
Here we’ll learn to change the figure size and the resolution of the figure using matplotlib module.
The following is the syntax:
Let’s see examples related to this:
Example #1
In this example, we plot a line chart and change its size and resolution.
- First use matplotlib.pyplot.figure to create a new figure or activate one that already exists.
- The method takes a figsize argument, which is used to specify the figure’s width and height (in inches).
- Then, enter a dpi value that corresponds to the figure’s resolution in dots-per-inch.
Example #2
Here we’ll see one more example related to the change in size and resolution of the figure to understand the concept more clearly.
- To define data coordinates, here we use random.randint() method of numpy.
- Here we set the width, height, and dpi of the figure to 6, 4, and 150 respectively.
Matplotlib plot change point size
Here we’ll learn how to change marker size in matplotlib with different examples.
The following is the syntax:
Here x and y specify the data coordinates and s specify the size of the marker.
Example #1
Here we’ll see an example where we set a single size for all the points.
- Import matplotlib.pyplot library.
- Next, define A and B data coordinates.
- To plot a scatter graph, use the scatter() function.
- To set the same size to all points, use s as an argument and set the size.
- To visualize the pot, use the show() function.
Example #2
Here we’ll see an example where we set different sizes for each point
- Import matplotlib.pyplot library.
- Next, define data coordinates.
- Then, define different sizes for each point.
- To plot a scatter graph, use the scatter() function and set the size pass s parameter.
- To display the plot, use the show() function.
Example #3
Here we’ll see an example where we define the point size for each point in the plot.
- Import matplotlib.pyplot library for data visualization.
- Define data coordinates.
- Create a function to define the point sizes to use for each point in the plot.
- To plot a scatter graph, use the scatter() function.
Matplotlib set plot size in pixels
Here we’ll see examples where we convert inches to pixels and plot the graph using matplotlib module.
Example #1
In the below example we’ll change the plot size by using the pixel conversion method.
- Import necessary libraries such as matplotlib.pyplot, figure, and numpy.
- Then use, default pixels values, rcParams[‘figure.dpi’] to set values to px.
- Now, set figsize in pixels.
- Then, define the data coordinates for plotting.
- To plot the line chart, use the plot() function.
- To display the plot, use the show() function.
Example #2
Here we change the plot size in pixels by using the dpi argument.
- Here we use, the dpi parameter to set the plot in pixels with the figure() function.
- Next, we define data coordinates, labels, and colors for pie chart creation,
- To plot a pie chart, we use the pie() function.
- To add a legend to a plot, we use the legend() function.
Matplotlib change default plot size
In Matplotlib, the dictionary object rcParams contains the properties. The figure size could be used as the key figure’s value in figsize in rcParams, which represents the size of a figure.
To change the size by default, we use plt.rcParams. It is acceptable when we placed it before or after plt.plot. Any figure made using the same scripts will be assigned the same figure size.
Let’s see examples:
Example #1
Here we’ll set the default, size of the figure to 7 by 7.
- Import matplotlib.pyplot library.
- To set default plot size, use plt.rcParams[“figure.figsize”].
- To plot a chart, use the plot() function.
Matplotlib change bar plot size
Here we’ll learn the different ways to change the size of the bar plot using the matplotlib module with the help of examples.
Example #1
Here we’ll use the figsize() method to change the bar plot size.
- Import matplotlib.pyplot library.
- To change the figure size, use figsize argument and set the width and the height of the plot.
- Next, we define the data coordinates.
- To plot a bar chart, use the bar() function.
- To display the chart, use the show() function.
Example #2
In this example, we’ll change the size of the bar chart by using rcParams.
Here we use the default method, to change the size of bar plots i.e. by using plt.rcParams[‘figure.figsize’].
Matplotlib change scatter plot size
Here we’ll learn the different ways to change the size of the scatter plot using the matplotlib module with the help of examples.
Example #1
Here we’ll learn to change the size of scatter plot by using figsize.
- Import matplotlib.pyplot library.
- To set figure size, use the figsize parameter and set the width and height of the figure.
- Define datasets to plot to scatter graph.
- To plot a scatter plot, use the scatter method.
- To add extra features to the plot, pass color, size, marker, edgecolor, marker as a parameter.
Example #2
In this example, we use the set_size_inches method change the size of the scatter plot.
- Import necessary libraries such as matplotlib.pyplot and numpy.
- Then create a new figure by using the figure() method.
- To set the size of the scatter plot, use the set_size_inches() method and pass width and height.
- To define data coordinates, use linspace() and sin() methods.
- To plot a scatter graph, use the scatter() function.
Matplotlib change figure size fig ax
We can easily change the height and the width of the plot by using the set_figheight and the set_figwidth method of the matplotlib module.
Let’s see examples related to this concept:
Example #1
Here we set the width of the plot, by using the set_figwidth method.
- First import necessary libraries, such as numpy and matplotlib.pyplot.
- Next, use the set_figwidth() method to change the width of the plot.
- To define data coordinates, use arange() and array() methods of numpy.
- To plot a line graph with a dashed line style, use the plot() function with linestyle parameter.
Example #2
Here we set the height of the plot, by using the set_figheight method.
- First import necessary libraries, such as numpy and matplotlib.pyplot.
- Next, use the set_figheight() method to change the height of the plot.
- To define data coordinates, use arange() and array() methods of numpy.
Matplotlib set plot size in centimeter
In matplotlib by default, the figure size is in inches. Here we’ll learn to set plot size in centimeter with the help of an example.
Example:
- Import numpy library.
- Next, import the matplotlib library.
- Set figure size by conversion inches to centimeters.
- Define data coordinates x and y.
- To plot a line chart, we use the plot() function.
You may also like to read the following Matplotlib tutorials.
In this Python tutorial, we have discussed the “Matplotlib increase plot size” and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.
- Matplotlib increase plot size
- Matplotlib increase plot size jupyter
- Matplotlib increase plot size subplots
- Matplotlib pyplot set_size_inches
- Pandas matplotlib increase plot size
- Matplotlib change figure size and dpi
- Matplotlib plot change point size
- Matplotlib set plot size in pixels
- Matplotlib change default plot size
- Matplotlib change bar plot size
- Matplotlib change scatter plot size
- Matplotlib set plot window size
- Matplotlib change figure size fig ax
- Matplotlib set plot size in centimeter
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.
How do I change the size of figures drawn with Matplotlib?
How do I change the size of figure drawn with Matplotlib?
14 Answers 14
figure tells you the call signature:
figure(figsize=(1,1)) would create an inch-by-inch image, which would be 80-by-80 pixels unless you also give a different dpi argument.
If you’ve already got the figure created, you can use figure.set_size_inches to adjust the figure size:
To propagate the size change to an existing GUI window, add forward=True :
Additionally as Erik Shilts mentioned in the comments you can also use figure.set_dpi to "[s]et the resolution of the figure in dots-per-inch"
Using plt.rcParams
There is also this workaround in case you want to change the size without using the figure environment. So in case you are using plt.plot() for example, you can set a tuple with width and height.
This is very useful when you plot inline (e.g., with IPython Notebook). As asmaier noticed, it is preferable to not put this statement in the same cell of the imports statements.
To reset the global figure size back to default for subsequent plots:
Conversion to cm
The figsize tuple accepts inches, so if you want to set it in centimetres you have to divide them by 2.54. Have a look at this question.
Deprecation note:
As per the official Matplotlib guide, usage of the pylab module is no longer recommended. Please consider using the matplotlib.pyplot module instead, as described by this other answer.
The following seems to work:
This makes the figure’s width 5 inches, and its height 10 inches.
The Figure class then uses this as the default value for one of its arguments.
In case you’re looking for a way to change the figure size in Pandas, you could do:
where df is a Pandas dataframe. Or, to use an existing figure or axes:
If you want to change the default settings, you could do the following:
For more details, check out the docs: pd.DataFrame.plot .
The first link in Google for ‘matplotlib figure size’ is AdjustingImageSize (Google cache of the page).
Here’s a test script from the above page. It creates test[1-3].png files of different sizes of the same image:
The module comments and the actual output differ.
This answer allows easily to combine all three images in one image file to see the difference in sizes.
As of Matplotlib 2.0.0, changes to your canvas will be visible immediately, as the forward keyword defaults to True .
If you want to just change the width or height instead of both, you can use
fig.set_figwidth(val) or fig.set_figheight(val)
These will also immediately update your canvas, but only in Matplotlib 2.2.0 and newer.
For Older Versions
You need to specify forward=True explicitly in order to live-update your canvas in versions older than what is specified above. Note that the set_figwidth and set_figheight functions don’t support the forward parameter in versions older than Matplotlib 1.5.0.
Adjust the figsize= parameter in matplotlib.pyplot.figure , which is similar to this answer, but uses the standard plt import alias, and doesn’t directly import figure from the pyplot namespace.
Without fig = plt.figure(figsize=(10, 10))
With fig = plt.figure(figsize=(10, 10))
This works well for me:
Comparison of different approaches to set exact image sizes in pixels
This answer will focus on:
- savefig : how to save to a file, not just show on screen
- setting the size in pixels
Here is a quick comparison of some of the approaches I’ve tried with images showing what the give.
Summary of current status: things are messy, and I am not sure if it is a fundamental limitation, or if the use case just didn’t get enough attention from developers. I couldn’t easily find an upstream discussion about this.
Baseline example without trying to set the image dimensions
Just to have a comparison point:
base.py
My best approach so far: plt.savefig(dpi=h/fig.get_size_inches()[1] height-only control
I think this is what I’ll go with most of the time, as it is simple and scales:
get_size.py
I tend to set just the height because I’m usually most concerned about how much vertical space the image is going to take up in the middle of my text.
plt.savefig(bbox_inches=’tight’ changes image size
I always feel that there is too much white space around images, and tended to add bbox_inches=’tight’ from: Removing white space around a saved image
However, that works by cropping the image, and you won’t get the desired sizes with it.
Instead, this other approach proposed in the same question seems to work well:
which gives the exact desired height for height equals 431:
Fixed height, set_aspect , automatically sized width and small margins
Ermmm, set_aspect messes things up again and prevents plt.tight_layout from actually removing the margins. this is an important use case that I don’t have a great solution for yet.
plt.savefig(dpi=h/fig.get_size_inches()[1] + width control
If you really need a specific width in addition to height, this seems to work OK:
width.py
and for a small width:
So it does seem that fonts are scaling correctly, we just get some trouble for very small widths with labels getting cut off, e.g. the 100 on the top left.
From this, we also see that tight_layout removes a lot of the empty space at the top of the image, so I just generally always use it.
Fixed magic base height, dpi on fig.set_size_inches and plt.savefig(dpi= scaling
I believe that this is equivalent to the approach mentioned at: https://stackoverflow.com/a/13714720/895245
magic.py
And to see if it scales nicely:
So we see that this approach also does work well. The only problem I have with it is that you have to set that magic_height parameter or equivalent.
Fixed DPI + set_size_inches
This approach gave a slightly wrong pixel size, and it makes it is hard to scale everything seamlessly.
set_size_inches.py
So the height is slightly off, and the image:
The pixel sizes are also correct if I make it 3 times larger:
We understand from this however that for this approach to scale nicely, you need to make every DPI-dependant setting proportional to the size in inches.
In the previous example, we only made the "Hello" text proportional, and it did retain its height between 60 and 80 as we’d expect. But everything for which we didn’t do that, looks tiny, including:
- line width of axes
- tick labels
- point markers
SVG
I could not find how to set it for SVG images, my approaches only worked for PNG, e.g.:
get_size_svg.py
And the generated output contains:
And identify says:
And if I open it in Chromium 86 the browser debug tools mouse image hover confirm that height as 460.79.
But of course, since SVG is a vector format, everything should in theory scale, so you can just convert to any fixed sized format without loss of resolution, e.g.:
gives the exact height:
I use Inkscape instead of ImageMagick’s convert here because you need to mess with -density as well to get sharp SVG resizes with ImageMagick:
And setting <img height="" on the HTML should also just work for the browser.