How to Make a Square Plot With Equal Axes in Matplotlib?

29 Oct, 2021
In this article, we are going to discuss how to illustrate a square plot with equal axis using matplotlib module.  We can depict a Square plot using matplotlib.axes.Axes.set_aspect() and matplotlib.pyplot.axis() methods.
Syntax: matplotlib.axes.Axes.set_aspect()
Parameters: 
aspect : This parameter accepts the following value {‘auto’, ‘equal’} or num.
adjustable : This defines which parameter will be adjusted to meet the required aspect.
anchor : This parameter is used to define where the Axes will be drawn if there is extra space due to aspect constraints.
share: This parameter is used to apply the settings to all shared Axes.
Example 1:
We can generate a square plot using matplotlib.axes.Axes.set_aspect() method. We will assign equal as an aspect argument and box as adjustable argument.
Python3
# import required module# import required modulesimport numpy as npimport matplotlib.pyplot as plt # adjust coordinatesx = y = [i for i in range(0, 6)] # depict illustrationfig = plt.figure()ax = fig.add_subplot()plt.plot(x, y) # square plotax.set_aspect('equal', adjustable='box')plt.show()
Output:
Example 2:
The above example possibly yields a square plot when ranges for the two tomahawks are set to be the equivalent. To produce a square plot in the overall case, we need to physically set the viewpoint proportion utilizing the accompanying order:  
axes.set_aspect(1./axes.get_data_ratio())
Python3
# import required modulesimport numpy as npimport matplotlib.pyplot as plt # adjust coordinatesx = y = [i for i in range(0, 6)] # depict illustrationfig = plt.figure()ax = fig.add_subplot()plt.plot(x, y) # square plotax.set_aspect(1.0/ax.get_data_ratio(), adjustable='box')plt.show()
Output:
Syntax: matplotlib.pyplot.axis()
Parameters: 
xmin, xmax, ymin, ymax:These parameters can be used to set the axis limits on the graph.
emit:Its a bool value used to notify observers of the axis limit change.
Example 1:
In this example, we pass square as an argument to matplotlib.pyplot.axis(), it illustrates a square plot.
Python3
# import required module# import required modulesimport numpy as npimport matplotlib.pyplot as plt # adjust coordinatesx=y=[i for i in range(0,6)] # plot coordinatesplt.plot(x,y) # square plotplt.axis('square') # depict illustrationplt.show()
 
 
Output:
 
 
Example 2:
 
Here is another example to illustrate a square plot using axis() method.
 
Python3
# importing moduleimport matplotlib.pyplot as plt # assigning x and y coordinatesx = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]y = [] for i in range(len(x)):    y.append(max(0, x[i])) # depicting the visualizationplt.plot(x, y, color='green')plt.xlabel('X')plt.ylabel('Y') # square plotplt.axis('square') # displaying the titleplt.title('ReLU Function')
Output:
sumitgumber28
Picked
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
Introduction To PYTHON
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list