Matplotlib subplots_adjust

In this Python tutorial, we will discuss Matplotlib subplots_adjust in python, which lets us work with refining multiple plots within a single figure and we also cover the following topics:

  • Matplotlib subplots_adjust
  • Matplotlib subplots_adjust tight_layout
  • Matplotlib subplots spacing
  • Matplotlib subplots_adjust height and hspace
  • Matplotlib subplots_adjust figure size
  • Matplotlib subplots_adjust wspace and width
  • Matplotlib subplots_adjust bottom
  • Matplotlib subplots_adjust right
  • Matplotlib subplots_adjust not working

Matplotlib subplots_adjust

The subplots_adjust() is a function in matplotib library, which is used to adjust or refine the subplot structure or design.

The syntax for subplots_adjust() is as follows:

matplotlib.pyplot.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=None,hspace=None)

In the above syntax the following parameters are used which are outlined below:

  • left: specifies the left position of the subplots of the figure. Default size is 0.125.
  • right: specifies the right position of the subplots of the figure. Default size is 0.9.
  • bottom: specifies the bottom position of the subplots of the figure. Default size is 0.1.
  • top: specifies the top position of the subplots of the figure. Default size is 0.9.
  • wspace: specifies the size of width for white space between subplots (called padding), as a fraction of the average Axes width. Default size is 0.2.
  • hspace: specifies the size of height for white space between subplots (called padding), as a fraction of the average Axes height. Default size is 0.2.

Let’s do an example for understanding the concepts:

Code#1

# Importing libraries
import numpy as np
import matplotlib.pyplot as plt

x1 = [2 , 4 , 6 , 8 , 10]
x2 = [5 , 10 , 15 , 20 , 25]

fig = plt.figure()
ax = fig.subplots()

ax.plot(x1,x2)

fig.show()

The above code #1 is just a simple matplotlib subplot code.

Code#2


# Importing libraries
import numpy as np
import matplotlib.pyplot as plt

x1 = [2 , 4 , 6 , 8 , 10]
x2 = [5 , 10 , 15 , 20 , 25]

fig = plt.figure()
ax = fig.subplots()

ax.plot(x1,x2)

fig.subplots_adjust(right = 2)

fig.show()

In the above code #2, we implement the subplots_adjust function with parameter right.

Now, let’s see the difference in both the outputs of the code, so we clearly understand what basically subplots_adjust function does.

matplotlib subpots adjust code #1 output
The output of Code#1 (Simple Subplot code)
matplotlib subplots adjust code#2 output
The output of Code#2 ( Code in which we use subplots_adjust() with parameter right )

Now from the above two codes and their outputs, we clearly see that by using the subplots_adjust(), we adjust the right position of the subplot by 2.

Conclusion! matplotlib.pyplot.subplots_adjust() function reshape the design of the subplot by changing its positions.

Read: Matplotlib best fit line

Matplotlib subplots_adjust tight_layout

The subplots_adjust tight_layout() is a function in matplotib library, which is used to automatically adjust the proper space between the subplots so that it fits into the figure area.

The syntax is as follow:

matplotlib.pyplot.tight_layout(pad=10.8, h_pad=None, w_pad=None, rect=None)

In the above syntax, the following parameters are used which are outlined below:

  • pad: specifies the size of white space ( called Padding ) between edges of subplots. By default, it is 1.08
  • h_pad: specifies the size of the height for Padding between edges of the subplots.
  • w_pad: specifies the size of the width for Padding between edges of the subplots.
  • rect: specifies the size of the figure area into which the subplots fit.

When to use tight_layout():

  • When axis labels to titles go outside the figure area.
  • When we have more than one subplots in a figure area, and we see labels or titles of different subplots overlapping each other.
  • When we have multiple subplots in a figure area, and we see the subplots are of different sizes.
  • When we have multiple subpots in a figure area, and we want to adjust extra padding around the figure and between the subplots.

Let’s do an example for understanding the concept:

Code#1


# Importing libraries 

import numpy as np
import matplotlib.pyplot as plt

x1= [2,4,6]
y1= [3,6,9]

x2= [5,10,15]
y2= [6,12,18]

x3= [2,4,6]
y3= [3,6,9]

x4= [5,10,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

ax[0, 0].set_title("Graph 1 ")
ax[0, 1].set_title("Graph 2")
ax[1, 0].set_title("Graph 3")
ax[1, 1].set_title("Graph 4")

plt.show()

The above code#1 is just a simple matplotlib subplot code in which we have multiple subplots in a figure area.

Code#2

# Importing Libraries
import numpy as np
import matplotlib.pyplot as plt

x1= [2,4,6]
y1= [3,6,9]

x2= [5,10,15]
y2= [6,12,18]

x3= [2,4,6]
y3= [3,6,9]

x4= [5,10,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

ax[0, 0].set_title("Graph 1 ")
ax[0, 1].set_title("Graph 2")
ax[1, 0].set_title("Graph 3")
ax[1, 1].set_title("Graph 4")

fig.tight_layout()
plt.show()

In the above Code#2, we have to implement the tight_layout function.

Now, let’s see the difference in both the outputs of the code, so we clearly understand what basically tight_layout function does.

matplotlib subplot output code#1
The output of Code#1 (Simple Subplot code)

In the above output, we have seen that labels of different axes overlapping each other.

matplotlib subplot output code#2
The output of Code#2 ( Code in which we use tight_layout function )

In the above output, we have seen that by using the tight_layout function we adjust the spacing between subplots in a figure area to avoid overlapping.

Conclusion! matplotlib.pyplot.tight_layout() function adjusts the subplots so it fits into the figure area perfectly.

Read: Matplotlib subplot tutorial

Matplotlib subplots spacing

Why do we need Spacing!

When we deal with subplots we have to plot multiple subplots in the figure area. Sometimes we see that axes for multiple subplots in a figure area start overlapping or axes values are too confusing and overcrowded.

So to solve these problems we need to set the spacing between subplots.

READ:  Matplotlib is currently using agg a non-gui backend

Ways To Solve Spacing:

  • Using tight_layout() function
  • Using subplots_adjust() function
  • Using subplot_tool() function

Using tight_layout() function

tight_layout() function adjusts the spacing between subplots automatically.

The syntax for the above:

matplotlib.pyplot.tight_layout()

Let’s understand with the help of an example:

# Importing Libraries
import numpy as np
import matplotlib.pyplot as plt

x1= [2,4,6]
y1= [3,6,9]

x2= [5,10,15]
y2= [6,12,18]

x3= [2,4,6]
y3= [3,6,9]

x4= [5,10,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

fig.tight_layout()
plt.show()

tight layout output
Using tight_layout()

From the above example, we conclude that by using the tight_layout() function the spacing between subplots is proper.

Read Matplotlib plot_date

Using subplots_adjust() function

subplots_adjust() function changes the space between subplots. By using parameters left, right, top, bottom, wspace, hspace, we can adjust the subplot’s position.

The syntax for the above:

matplotlib.pyplot.subplots_adjust(left=None, bottom= None, right=None, top=None, wspace=None, hspace=None)

Let’s understand with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [2,4,6]
y1= [3,6,9]

x2= [5,10,15]
y2= [6,12,18]

x3= [2,4,6]
y3= [3,6,9]

x4= [5,10,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

plt.subplots_adjust(left=0.5, bottom=0.5, right=1, top=1, wspace=0.9,hspace=0.9)
plt.show()
subplots adjust output
subplots_adjust()

From the above example, we conclude that by using the subplots_adjust() function the spacing between subplots is proper.

Using subplot_tool() function

subplot_tool() method provides a slider to adjust subplot spacing. It is an interactive method in which users drag the bar to do adjustments as per their needs.

The syntax for the above:

matplotlib.pyplot.subplot_tool()

Let’s understand with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [2,4,6]
y1= [3,6,9]

x2= [5,10,15]
y2= [6,12,18]

x3= [2,4,6]
y3= [3,6,9]

x4= [5,10,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

plt.subplot_tool()
plt.show()
subplot toll output
subplot_tool()

From the above example, we conclude that by using the subplot_tool() function we can adjust spacing by using the slider.

Read: Matplotlib plot bar chart

Matplotlib subplot_adjust height or hspace

When we deal with subplots we have to plot multiple subplots in the figure area. Sometimes the problem of overlapping starts occurring so to avoid that we need to adjust the height.

Ways to Adjust Height:

  • Using tight_layout() function
  • Using subplots_adjust() function
  • Using subplot_tool() function

Using tight_layout() function

tight_layout() function adjusts the height of white space ( Called Padding ) between subplots automatically.

Syntax as below:

matplotlib.pyplot.tight_layout()

Let’s understand it with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [1,2,3]
y1= [5,6,9]

x2= [5,10,20]
y2= [6,12,15]

x3= [2,5,6]
y3= [3,6,9]

x4= [7,8,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

fig.tight_layout()
plt.show()
adjust height tight layout
tight_layout()

From the above example, we conclude that by using the tight_layout() function the height space between subplots is exact as needed.

Using subplots_adjust() function

subplots_adjust() function changes the space between subplots. By using parameter hspace we can adjust the height of the Padding between subplots.

The syntax for this is as below:

matplotlib.pyplot.subplots_adjust(hspace=None) 

Let’s understand this with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [1,2,3]
y1= [5,6,9]

x2= [5,10,20]
y2= [6,12,15]

x3= [2,5,6]
y3= [3,6,9]

x4= [7,8,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

fig.subplots_adjust(hspace=0.9)
plt.show()
using subplots_adjust() to adjust height
subplots_adjust()

From the above example, we conclude that by using the subplots_adjust() function we can tune the height of the padding.

Read Add text to plot matplotlib in Python

Using subplot_tool() function

subplot_tool() method provides a slider to adjust subplot height. It is an interactive tool in which users darg the height bar to do adjustments as per their needs.

The syntax for the above:

matplotlib.pyplot.subplot_tool()

Let’s understand it with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [1,2,3]
y1= [5,6,9]

x2= [5,10,20]
y2= [6,12,15]

x3= [2,5,6]
y3= [3,6,9]

x4= [7,8,15]
y4= [6,12,18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

plt.subplot_tool()
plt.show()
adjust height outadjust height using subplot_toolput
subplot_tools()

In the above example, we can drag the slider to adjust the height of the subplots in a figure area.

Read: What is matplotlib inline

Matplotlib subplots_adjust figure size

As we have already study, that by using function subplots() we can create multiple graphs within the same figure area. But the exception occurs the subplots method only creates subplots of the same size or adjusts the size automatically.

Now the problem comes! What to do if we want to create subplots of different sizes within the area.

Hurrah! We have a solution for this problem. So let’s learn it.

For changing the size of subplots within the same figure area according to your choice using the method add_gridspec(). It is a powerful solution if we want to change the size of multiple subplots but it takes time for creating multiple subplots of the same size.

  • In this method, we have defined the location in which we want to create a plot and also specify each subplot individually.
  • We have to specify the number of rows and columns of the grid as the input parameter of the add_gridspec() method.
  • After the initialization of grid size, we can adjust the size according to your relevance by varying the input coordinates in the add_subplot() function.
  • The coordinates of the axes which we enter as parameters in the sub_plot() method specify the initial and final position of the plot (i.e. rows and columns).

To specify the initial and final position of the plot you should have some knowledge of indexing and slicing.

Tip! Don’t use add_gridspec() for creating subplots of the same size.

So, Let’s understand it with the help of an example:

# Importing Libraries.

import matplotlib.pyplot as plt

# .figure() method is used to plot a fig and parameter constrained_layout=True is used to plot figure nicely distributed.

fig = plt.figure(constrained_layout=True)

# Creating the subplots with .add_gridspec().

ax = fig.add_gridspec(3, 3)

# .add_subplot() shows initial and final position of the plot.

ax1 = fig.add_subplot(ax[0, 0:1])   # Plot is located in the first row and has a width of 1/3 of the entire gride.

ax1.set_title('[0, 0:1]')

ax1 = fig.add_subplot(ax[0:, -1])   # Plot is located in one column wide but has a height of entire grid.

ax1.set_title('[0:, -1]')

ax1 = fig.add_subplot(ax[2:, 0:2])  # Plot is located in last three rows and first two columns.

ax1.set_title('[2:, 0:2]')

# Display the Plot
plt.show()

Here is the output of the above code.

figure size output
add_gridspec() method output

In the above example, we have concluded that by using the add_gridspec() method we can change the size of multiple subplots in a figure area according to your preference.

READ:  Attributeerror: Module 'tensorflow' has no attribute 'truncated_normal_initializer'

Conclusion! Steps to change the figure size:

  • Creat the subplots with add_gridspec() method and specify their distribution.
  • Use the add_subplot() method to specify the initial and final position of the subplots.
  • Use slicing and indexing to specify the location of the subplots within the region.

Read: Python plot multiple lines using Matplotlib

Matplotlib subplot_adjust width or wspace

When we deal with subplots we have to plot multiple subplots in the figure area. Sometimes the problem of overlapping starts occurring so to avoid that we need to adjust the width.

Ways to Adjust wspace:

  • Using tight_layout() function
  • Using subplots_adjust() function
  • Using subplot_tool() function

Using tight_layout() function

tight_layout() function adjusts the width of padding between the subplots automatically as per the requirement. This means that the width of the white space between the subplots with a figure area is tuned.

Syntax as below:

matplotlib.pyplot.tight_layout()

Let’s understand it with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

fig.tight_layout()
plt.show()
tight layout width
tight_layout()

From the above example, we concluded that by using the tight_layout() method the wspace or width of the padding between the subplots is exact without any overlapping.

Using subplots_adjust() function

subplots_adjust() method changes the space between subplots. By using parameter wspace we can adjust the width of white space between the subplots (called Padding). Sometimes we refer wspace also as the width of the subplots.

The syntax for this is as below:

matplotlib.pyplot.subplots_adjust(wspace=None)

Let’s understand this with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

fig.subplots_adjust(wspace=10)
plt.show()
subplots adjust width
subplots_adjust(wspace=None)

From the above example, we conclude that by using the subplots_adjust() method we can tune the width of the padding between subplots within a region.

Using subplot_tool() function

subplot_tool() function provides a slider to adjust subplot width or wspace. It is an interactive way to adjust the width of the subplots. Users can drag the wspace bar to adjust the width of the subplots.

The syntax for the above:

matplotlib.pyplot.subplot_tool()

Let’s understand it with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

plt.subplot_tool()
plt.show()
subplot tool
Subplot_tool()

In the above example, we have to click on the slider to adjust the subplot param wspace or we say the width of subplots.

Read: Matplotlib plot a line

Matplotlib subplot_adjust bottom

When we deal with subplots we have to plot multiple graphs in the figure area. Sometimes the problem of overlapping starts occurring. We encounter a problem that tick mark labels or an x-axis label flaps or overrun on tick marks and axis labels of another subplot in an area. To overcome this problem we have to adjust the bottom margin.

Ways to Adjust bottom:

  • Using tight_layout() method
  • Using subplots_adjust() method
  • Using subplot_tool() method

Using tight_layout() method

tight_layout() function adjusts the bottom margin between the subplots automatically. We can also say that it automatically creates a room for tick marks labels or an x-axis label.

Syntax as below:

matplotlib.pyplot.tight_layout()

Let’s understand the concept with an example:

# Import Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [2,4,6]
y1= [3,6,9]

x2= [5,10.9,15]
y2= [6,12,18]

x3= [2,4.5,6]
y3= [3,6,9]

x4= [5,10]
y4= [6,12]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4, y4)

ax[0, 0].set_title("Graph 1 ")
ax[0, 1].set_title("Graph 2")
ax[1, 0].set_title("Graph 3")
ax[1, 1].set_title("Graph 4")

fig.tight_layout()

plt.show()
adjust bottom uUsing tight_layout() method
tight_layout()

From the above example, we concluded that by using the tight_layout() function the bottom margin increases its size automatically to avoid overlapping.

Using subplots_adjust() method

subplots_adjust() method changes the space between subplots. By using parameter bottom we can tune the bottom margin of the subplots. By default, the value of the bottom is 0.1. We have to increase the value to increase the size of the bottom margin.

Syntax as below:

matplotlib.pyplot.subplots_adjust(bottom=None)

Let’s understand this with the help of an example:

# Import Libraries

import numpy as np
import matplotlib.pyplot as plt
   
  
x = [2,4,6,8,10]
y = [5,10,15,20,25]
   
fig = plt.figure()
ax = fig.subplots()
  
ax.plot(x, y)
  
fig.subplots_adjust(bottom = 0.2)
  
  
fig.show() 
subplot adjust
subplots_adjust()

From the above example, we conclude that by using the subplots_adjust() method we can increase or decrease the size of the bottom margin in a figure area.

READ:  Python Scipy Eigenvalues [7 Useful Examples]

Using subplot_tool() method

subplot_tool() function provides an interactive way to adjust the bottom margin of the subplots. This method provides a slider to adjust the subplot bottom margin. Users can drag the bottom bar to tune the margin space of the subplots.

The syntax for the above:

matplotlib.pyplot.suplot_tool()

Let’s understand it with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

plt.subplot_tool()
plt.show()
using subplot tool
subplot_tool()

In the above example, we have to click on the slider to adjust the subplot param bottom.

Read: How to install matplotlib python

Matplotlib subplot_adjust Right

When we deal with subplots we have to plot multiple subplots in the figure area. Sometimes the problem of overlapping starts occurring so to avoid that we need to adjust right.

To perfectly fit subplots in a region we have to increase the right margin so that figure can adjust without flapping and to make the room available for legends we have to decrease the size of the right margin.

Ways to Adjust right:

  • Using tight_layout() function
  • Using subplots_adjust() function
  • Using subplot_tool() function

Using tight_layout() function

tight_layout() function adjusts the size of the right margin between the subplots automatically. Without any user interface the design of this method perfect figure area containing multiple subplots.

The syntax for this is given below:

matplotlib.pyplot.tight_layout()

Let’s understand it with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

fig.tight_layout()
plt.show()
tight layout
tight_layout()

From the above example, we concluded that by using the tight_layout() method the right margin between subplots gets perfect.

Using subplots_adjust() function

subplots_adjust() method changes the space between subplots. By using parameter right we can adjust the margin size from the right side. By default, the value of the right parameter is 0.9. We have to increase it or decrease it as per our needs.

The syntax for this is as below:

matplotlib.pyplot.subplots_adjust(right=None)

Let’s understand this concept with the help of an example:

# Import Libraries

import numpy as np
import matplotlib.pyplot as plt
   
  
x = [2,4,6,8,10]
y = [5,10,15,6.3,25]
   
fig = plt.figure()
ax = fig.subplots()
  
ax.plot(x, y)
  
fig.subplots_adjust(right = 0.9)
  
  
fig.show() 
subplots adjust
subplots_adjust()

From the above-coded example, we concluded that by using the subplots_adjust() method we can tune the right margin of subplots.

Using subplot_tool() function

subplot_tool() function provides a slider to adjust the subplot right margin. It is an interactive way to refine the right side margin of the subplots. Users have to drag the right bar to tune the right margin.

The syntax for the above:

matplotlib.pyplot.subplot_tool()

Let’s understand it with the help of an example:

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [7, 8, 15]
y4= [6, 12, 18]

fig, ax = plt.subplots(2, 2)

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4,y4)

plt.subplot_tool()
plt.show()
subplot tool
subplot_tool()

In the above example, we have to click on the slider to adjust the subplot right param to adjust the margin.

Read: modulenotfounderror: no module named ‘matplotlib’

Matplotlib subplot_adjust not working

Not working in the conditions in which we are not able to perform your task. We can also say that the condition in which errors or some obstacles occur in coding and you are not getting the desired output.

Not working conditions occur due to runtime error or maybe syntax error.

Let’s discuss some common, not working conditions:

Error#1

Matplotlib subplot_adjust not working

If you encounter such kind of error:

  • Check whether the libaraires are imported or not.
  • Check whether you mention ‘plt’ as nickname of any imported library or not

Error#2

Matplotlib subplot_adjust not working

If you encounter such kind of error:

Most programmers do this mistake. It’s a very common syntax error.

  • Note that attribute name is subplots_adjust not subplot_adjust. Take care of such errors.

Error#3

Matplotlib subplot_adjust not working

If you encounter such kind of error you will not able to get the slider to adjust the subplot param.

  • Remember subplot_tool is attribute of plt. So rectify this object name.

Error#4

Matplotlib subplot_adjust not working

If you encounter such kind of an error:

  • The default value of top is 0.9 and bottom is 0.1. To remove this error note that bottom should be less than or equal to top value.

You may also like to read the following articles.

In this Python tutorial, we have discussed the “Matplotlib subplot_adjust” and we have also covered some examples related to it. There are the following topics that we have discussed in this tutorial.

  • Matplotlib subplots_adjust
  • Matplotlib subplots_adjust tight_layout
  • Matplotlib subplots_adjust spacing
  • Matplotlib subplots_adjust height and hspace
  • Matplotlib subplots_adjust figure size
  • Matplotlib subplots_adjust width and wspace
  • Matplotlib subplots_adjust bottom
  • Matplotlib subplots_adjust right
  • Matplotlib subplots_adjust not working