Skip to content
Related Articles

Related Articles

How to put the origin in the center of the figure with Matplotlib ?
  • Last Updated : 26 Dec, 2020

In this article, we are going to discuss how to put the origin in the center of the figure using the matplotlib module. To put the origin at the center of the figure we use the spines module from the matplotlib module. Basically, spines are the lines connecting the axis tick marks and noting the boundaries of the data area. Under this module, we use set_position() method which sets the position of the spine which helps to set the origin in the centre.

However, we can put the origin in the center of the figure without using set_position() method. The below graph is made without using set_position() method.

Example 1:

Python3




# import required modules
import numpy as np
import matplotlib.pyplot as plt
  
# assign coordinates
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)
  
# depict illustration
plt.xlim(-np.pi, np.pi)
plt.plot(x, y)
plt.grid(True)
plt.show()

Output:



The below graph is made with using set_position() method which helped to bring origin to the centre.

Example 2:

Python3




# import required modules
import numpy as np
import matplotlib.pyplot as plt
  
# assign coordinates
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)
  
# use set_position
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
  
# depict illustration
plt.xlim(-np.pi, np.pi)
plt.plot(x, y)
plt.grid(True)
plt.show()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :