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

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.

Also, check: Matplotlib plot a line

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:

matplotlib.pyplot.rcParams["figure.figsize"]

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.

# Import Library

import matplotlib.pyplot as plt

# Increase size of plot in jupyter

plt.rcParams["figure.figsize"] = (8,5.5)

# Define Data

x = [2, 4, 6, 8]
y = [5, 10, 15, 20]

# Plot

plt.plot(x, y, '-.')

# Display

plt.show()
  • 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.
matplotlib increase plot size jupyter
Width=8, Height=5.5

Example #2

The source code below elaborates the process of increasing size in a jupyter notebook using matplotlib.

# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Increase size of plot in jupyter

plt.rcParams["figure.figsize"] = (10,6)

# Define Data

x = np.linspace(0, 10, 1000)
y = np.sin(x)

# Plot

plt.plot(x, y)

# Display

plt.show()

Here we define data coordinates by using linspace() function and sin() function of numpy module.

increase plot size in jupyter using matplotlib
# Increase Size of Plot in Jupyter

Read: What is matplotlib inline

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:

fig, ax = plt.subplots(nrows , ncols , figsize=(width,height))

Example #1

Here is the example where we increase the same size for all the subplots.

# Import necessary libraries

import matplotlib.pyplot as plt
import numpy as np

# Set one size for all subplot

fig, ax = plt.subplots(2, 2, figsize=(10,8))

# Preparing the data to subplots

x = np.linspace(0,10,100)
y1 = x ** 2
y2 = x ** 4
y3 = x ** 6
y4 = x ** 8

# Plot

ax[0, 0].plot(x, y1, color='r')
ax[0, 1].plot(x, y2, color='k', linestyle=':')
ax[1, 0].plot(x, y3, color='y', linestyle='-.')
ax[1, 1].plot(x, y4, color='c',linestyle='--')

# Display

plt.show()
  • 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.
matplotlib increase plot size subplots
Set one size for all subplots

Example #2

Let’s see one more example to set the same size for all the subplots to understand the concept more clearly.

# Import necessary libraries

import matplotlib.pyplot as plt
import numpy as np

# Change the figure size

plt.figure(figsize=[15,14])

# Preparing the data to subplots

x = np.linspace(0, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

# Plot the subplots

# Plot 1

plt.subplot(2, 2, 1)
plt.plot(x, y1, color='g')

# Plot 2

plt.subplot(2, 2, 2)
plt.plot(x, y2, color='k')

# Display


plt.show()
  • 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.
matplotlib increase subplot size
Matplotlib increase plot size 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:

fig, ax = plt.subplots(nrows, ncols, gridspec_kw=
                       {'width_ratios': [3, 1],
                        'height_ratios’: [3, 3]})

Example:

The source code below elaborates the process of increasing the size of the individual subplots.

# Import Library

import matplotlib.pyplot as plt

# Define subplots

fig, ax = plt.subplots(1, 2, 
                      gridspec_kw={'width_ratios': [8,15]})

# Define data

x = [1, 2, 3, 4, 5]
y1 = [7, 13, 24, 26, 32]
y2 = [2, 4, 6, 8, 10]

# Create subplots

ax[0].plot(x, y1, color='red', linestyle=':')
ax[1].plot(x, y2, color='blue', linestyle='--')

# Adjust padding

plt.tight_layout()

# Display

plt.show()
  • 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.
increase size of subplots using matplotlib
Matplotlib increase plot size subplots

Example #2

The process of increasing the size of specific subplots is explained in the source code below.

# Import Library

import matplotlib.pyplot as plt

# Define subplots

fig, ax = plt.subplots(1, 2, gridspec_kw={'width_ratios': 
                       [10,4]})

# Define data

x = np.arange(0, 30, 0.2)
y1 = np.cos(x)
y2 = np.sin(x)

# Create subplots

ax[0].plot(x, y1, color='red', linestyle=':')
ax[1].plot(x, y2, color='blue', linestyle='--')

# Adjust padding

plt.tight_layout()

# Display


plt.show()

To plot a data, we define data coordinates, by using arange(), cos() and sin() functions of numpy.

matplotlib increase plot size for subplots
gridspec_kw={‘width_ratios’: [10,4]}

Read: Matplotlib plot bar chart

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:

matplotlib.figure.Figure.set_size_inches(w, h)

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 Libraries

import matplotlib.pyplot as plt 

# Create figure
        
fig = plt.figure() 

# Figure size

fig.set_size_inches(6.5, 6)

# Define Data Coordinates
      
x = [10, 20, 30, 40, 50]
y = [25, 25, 25, 25, 25]

# Plot

plt.plot(x, y, '--')
plt.plot(y, x)
    
# Display

plt.show() 
  • 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.
matplotlib pyplot set_size_inches
Matplotlib pyplot set_size_inches

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.

# Import Libraries

import matplotlib.pyplot as plt 
import numpy as np 

# Create figure
        
fig = plt.figure() 

# Figure size

fig.set_size_inches(12,10)

# Define Data Coordinates
      
x = np.arange(0,4*np.pi,0.1)
y = np.sin(4*x)

# Plot

plt.plot(x, y, '--')

# Display

plt.show() 

Here we define x and y data coordinates by using arange(), pi, and sin() function of numpy.

matplotlib pyplot with set_size_inches
fig.set_size_inches(12,10)

Read: Matplotlib subplots_adjust

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:

figsize=(width, height)

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 Library

import pandas as pd
  
# Creat pandas dataframe

df = pd.DataFrame({'Owner': [50, 15, 8, 20, 12]},
                  index=['Dog', 'Cat', 'Rabbit',  
                         'Parrot','Fish'])
 
# Plot

df.plot.pie(y='Owner', figsize=(8,8))

# Display

plt.show()
  • 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.
pandas matplotlib increase plot size
Pandas Matplotlib Increase Plot Size

Example #2

We’ll make a line chart with a pandas dataframe and increase the plot size in this example.

# Import libraries

import pandas as pd
import matplotlib.pyplot as plt

# Data

data = {'Year': [2021, 2020, 2019, 2018, 2017, 2016, 2015, 
                2014],
        'Birth_Rate': [17.377, 22.807, 26.170, 18.020, 34.532,    
                       18.636, 37.718, 19.252]
}

# Create DataFrame

df = pd.DataFrame(data,columns=['Year','Birth_Rate'])

# Line Plot

df.plot(x ='Year', y='Birth_Rate', kind = 'line', 
        figsize=(8,6))

# Display

plt.show()
  • 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 increase plot size in pandas
figsize=()

Read: Matplotlib set y axis range

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:

matplotlib.pyplot.figure(figsize=(w,h), dpi)

Let’s see examples related to this:

Example #1

In this example, we plot a line chart and change its size and resolution.

# Import Libraries

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

# Set fig size and dpi

figure(figsize=(8,6), dpi=250)

# Define Data

x = range(0,12)

# Plot

plt.plot(x)

# Display

plt.show()
  • 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.
matplotlib change figure size and dpi
Matplotlib change figure size and dpi

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.

# Import Libraries

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import numpy as np

# Set fig size and dpi

figure(figsize=(6,4), dpi=150)

# Define Data

x = np.random.randint(450,size=(80))

# Plot

plt.plot(x)

# Display

plt.show()
  • 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.
change figure size and dpi using matplotlib
figure(figsize=(6,4), dpi=150)

Read: Matplotlib update plot in loop

Matplotlib plot change point size

Here we’ll learn how to change marker size in matplotlib with different examples.

The following is the syntax:

matplotlib.pyplot.scatter(x , y , s)

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 Library

import matplotlib.pyplot as plt

# Define Data

A = [6, 7, 2, 5, 4, 8]
B = [12, 14, 17, 20, 22, 27]

# Scatter Plot and set size

plt.scatter(A, B, s=80)

# Display

plt.show()
  • 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.
matplotlib plot change point size
Set Single Size For All Points

Example #2

Here we’ll see an example where we set different sizes for each point

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [6, 7, 2, 5, 4, 8]
y = [12, 14, 17, 20, 22, 27]

# Define Size

sizes = [20, 55, 85, 150, 250, 9]

# Scatter Plot and set size

plt.scatter(x, y, s=sizes)

# Display

plt.show()
  • 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.
matplotlib plot change point size for each point
Set Different Size For Each Point

Example #3

Here we’ll see an example where we define the point size for each point in the plot.

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [2, 4, 6, 8, 10, 12, 14, 16]
y = [4, 8, 12, 16, 20, 24, 28, 32]

# Define Size

sizes = [3**n for n in range(len(x))]

# Scatter Plot and set size

plt.scatter(x, y, s=sizes, color='red')

# Display

plt.show()
  • 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 plot change point size using function
Matplotlib Plot Change Point Size

Read: Matplotlib Pie Chart Tutorial

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 Libraries

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import numpy as np

px = 1/plt.rcParams["figure.dpi"]

# Set fig size in Pixel

figure(figsize=(600 * px , 450 * px))

# Define Data

x = np.random.randint(800,size=(120))

# Plot

plt.plot(x)

# Display


plt.show()
  • 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.
matplotllib set plot size in pixels
Matplotllib set plot size in pixels

Example #2

Here we change the plot size in pixels by using the dpi argument.

# Import Libraries

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

# Set fig size and dpi

figure(dpi=100)

# Define Data Coordinates

water_quantity = [17, 27, 14, 22, 16, 6]

# Add labels

water_uses = ['Shower', 'Toilet', 'Leaks', 'Clothes Wsher', 
              'Faucet', 'Other']

# Colors

colors =['salmon','palegreen','skyblue','plum','pink','silver']

# Plot

plt.pie(water_quantity, colors=colors)

# Add legend

plt.legend(labels=water_uses, fontsize=18, loc='upper center', 
           bbox_to_anchor=(0.5, -0.04), ncol=2)

# Display

plt.show() 
  • 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 set plot size in pixels(dpi)
matplotlib set plot size in pixels(dpi)

Read: Matplotlib scatter plot color

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 Library

import matplotlib.pyplot as plt

# Default plot size

plt.rcParams["figure.figsize"] = (7,7)

# Plot


plt.plot([[5, 6], [9, 10], [1, 2], [1, 2]])

# Show


plt.show()
  • Import matplotlib.pyplot library.
  • To set default plot size, use plt.rcParams[“figure.figsize”].
  • To plot a chart, use the plot() function.
matplotlib change default plot size
Matplotlib change default plot size

Read: Matplotlib Plot NumPy Array

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 Library

import matplotlib.pyplot as plt

# Change size

plt.figure(figsize=(9,7))

# Define Data

pets = ['Rabbit', 'Dog', 'Cat', 'Goldfish', 'Parrot']
no_of_people = [4, 8, 11, 6, 5]

# Plot bar chart

plt.bar(pets, no_of_people)

# Display

plt.show()
  • 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.
matplotlib change bar plot size
Matplotlib change bar plot size

Example #2

In this example, we’ll change the size of the bar chart by using rcParams.

# Import Library

import matplotlib.pyplot as plt

# Change size

plt.rcParams['figure.figsize']=(8,6.5)

# Define Data

school_suppiles = ['Pencil', 'Scale', 'Pen', 'Sharpner',  
                   'Eraser']
no_of_suppiles = [8, 3, 2, 5, 4]

# Plot bar chart

plt.bar(school_suppiles, no_of_suppiles)

# Display

plt.show()

Here we use the default method, to change the size of bar plots i.e. by using plt.rcParams[‘figure.figsize’].

matplotlib change bar plot size
plt.rcParams[‘figure.figsize’]

Read: Matplotlib set_xticklabels

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 Library

import matplotlib.pyplot as plt

# Set size

plt.figure(figsize=(8,5))
  
# Define Dataset

x1 = [45, 58, 29, 83, 95, 20, 
      98, 27]
  
y1 = [31, 75, 8, 29, 79, 55, 
      43, 72]
  
x2 = [23, 41, 25, 64, 3, 15,
      39, 66]
  
y2 = [26, 34, 38, 
      20, 56, 2, 47, 15]


# Plot Scatter Graph

plt.scatter(x1, y1, c ="cyan", 
            marker ="s", 
            edgecolor ="black", 
            s = 50)
  
plt.scatter(x2, y2, c ="yellow",
            linewidths = 2,
            marker ="^", 
            edgecolor ="red", 
            s = 200)

# Display


plt.show()
  • 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.
matplotlib change scatter plot size
Matplotlib change scatter plot size

Example #2

In this example, we use the set_size_inches method change the size of the scatter plot.

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Create figure
        
fig = plt.figure() 

# Figure size

fig.set_size_inches(6,8)

# Define Data

x = np.linspace(0, 10, 100)
y = np.sin(x)

# Plot

plt.scatter(x, y)

# Display


plt.show()
  • 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 size of scatter plot
fig.set_size_inches(6,8)

Read: Matplotlib tight_layout – Helpful tutorial

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.

# Import Library

import numpy as np 
import matplotlib.pyplot as plt

# Set width

fig = plt.figure()
fig.set_figwidth(8)

# Data Coordinates

x = np.arange(2, 8) 
y = np.array([5, 8, 6, 20, 18, 30])

# Plot

plt.plot(x, y, linestyle='--') 

# Display

plt.show()
  • 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.
matplotlib change figure size fig ax
Matplotlib change figure size fig ax

Example #2

Here we set the height of the plot, by using the set_figheight method.

# Import Library

import numpy as np 
import matplotlib.pyplot as plt

# Set height

fig = plt.figure()
fig.set_figheight(8)

# Data Coordinates

x = np.arange(2, 8) 
y = np.array([5, 8, 6, 20, 18, 30])

# Plot

plt.plot(x, y, linestyle='--') 

# Display

plt.show()
  • 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 change figure size
Matplotlib change figure size

Read: Matplotlib x-axis label

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 Library

import numpy as np 
import matplotlib.pyplot as plt

# Set Size

cm = 1/2.54 
plt.figure(figsize=(15*cm, 11*cm))

# Data Coordinates

x = np.arange(2, 8) 
y = x * 2 + 6

# Plot

plt.plot(x, y) 

# Display

plt.show()
  • 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.
matplotlib set plot size in centimeter
Matplotlib set plot size in centimeter

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