 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to draw axis in the middle of a figure in Matplotlib?
To draw axis in the middle of a figure, we can take the following steps −
- Create x and sqr data points using numpy. 
- Create a new figure, or activate an existing figure, using figure() method. 
- Add an axis to the figure as a part of a subplot arrangement. 
- Set the postion of left and bottom spines. 
- Set the color of the right and top spines. 
- Plot x and sqr, using plot() method, with label y=x2 and color=red. 
- Place the legend using legend() method. Set the location at upper right corner. 
- To display the figure, use show() method. 
Example
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(-10., 10., 0.2) sqr = np.square(x) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['left'].set_position('center') ax.spines['bottom'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.plot(x, sqr, label="y=x^2", c='red') plt.legend(loc=1) plt.show()  Output

Advertisements
 