banner



How To Change The Figure Size In Matplotlib

Matplotlib is a handy visualization library in python that offers a number of plotting options and customizations. It may happen that at times you'd want to change the size of the plots as per your needs. In this tutorial, we'll look at how to change the size of figures plotted with matplotlib.

How to change the size of figures drawn with matplotlib?

To have the plots of a specific size in matplotlib, you can pass the figure size to the figsize parameter of matplotlib pyplot's figure() function or specify the default figure size via rcParams. The following is the syntax:

          import matplotlib.pyplot as plt plt.figure(figsize=(width,height))        

Here, we pass the desired dimensions of the plot as a (width,height) tuple to figsize. Note that the width and height should be in inches. So if you want your plot to be 8 inches wide and 6 inches high, pass (8,6) to figsize. The default size of a plot in matplotlib is (6.4,4.8)

Examples

Let's look at some examples of changing the figure size in maplotlib:

Change the figure size using figure()

First, let's plot a simple line chart with maplotlib without explicitly setting the figure size. Here's a line chart showing the employee count growth at a company over the last few years:

          import matplotlib.pyplot as plt  # number of employees of ABC emp_count = [3, 20, 50, 200, 350, 400] year = [2014, 2015, 2016, 2017, 2018, 2019]  # plot a line chart plt.plot(year, emp_count) plt.xlabel("Year") plt.ylabel("Count") plt.title("Employee count at ABC") plt.show()        

Output:

A line chart of default figsize

This is the default figure size we get on plotting with matplotlib. Now let's make this plot bigger.

          # change the figure size plt.figure(figsize=(8,6))  # plot a line chart plt.plot(year, emp_count) plt.xlabel("Year") plt.ylabel("Count") plt.title("Employee count at ABC") plt.show()        

Output:

A bigger line chart plotted with matplotlib by setting figsize to (8,6)

For the above plot, we first set the figsize to (8,6) and then proceeded to plot the line chart. You can see that this figure is indeed larger than the previous one. Let's see what happens if we attempt to plot the same figure without setting the figsize again.

          # plot a line chart plt.plot(year, emp_count) plt.xlabel("Year") plt.ylabel("Count") plt.title("Employee count at ABC") plt.show()        

Output:

Line chart with default figure size

Here we get the plot with the default figure size, smaller than the one we got in the above example where we specified the size. This means that you'll have to specify the figsize each time you want a plot (with a size other than default) using this method.

Set a default figsize in matplotlib using rcParams

Let's say your working in a Jupyter Notebook and want to set a default figsize without having to specify it each time you plot. For this, use rcParams which is used for handling default Matplotlib values.

          plt.rcParams['figure.figsize'] = [10, 8]  # plot a line chart plt.plot(year, emp_count) plt.xlabel("Year") plt.ylabel("Count") plt.title("Employee count at ABC") plt.show()        

Output:

Line chart of size (10,8) set with rcParams

The size configuration set with rcParams remains consistent for other plots as well. For instance, if you plot a bar chart of the same values without setting the figure size:

          # plot a bar chart plt.bar(year, emp_count) plt.xlabel("Year") plt.ylabel("Employees") plt.title("Employee count at ABC") plt.show()        

Output:

Bar chart of the same size (10, 8)

The resulting bar chart retains the figure size settings specified with rcParams in the previous example.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version 3.2.2


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

How To Change The Figure Size In Matplotlib

Source: https://datascienceparichay.com/article/change-figure-size-matplotlib/

Posted by: randallbrated58.blogspot.com

0 Response to "How To Change The Figure Size In Matplotlib"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel