- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
- Related Articles
- How to draw a line outside of an axis in Matplotlib?
- How to draw axis lines inside a plot in Matplotlib?
- How to set "step" on axis X in my figure in Matplotlib Python 2.6.6?
- How to annotate a range of the X-axis in Matplotlib?
- How to change the range of the X-axis and Y-axis in Matplotlib?
- How to customize the X-axis in Matplotlib?
- How to set the margins of a Matplotlib figure?
- How to draw a filled arc in Matplotlib?
- How to set the value of the axis multiplier in matplotlib?
- Draw axis lines or the origin for Matplotlib contour plot.
- How to set the current figure in Matplotlib?
- How to add a second X-axis in Matplotlib?
- How to put the title at the bottom of a figure in Matplotlib?
- How to enforce axis range in Matplotlib?
- How to shift a graph along the X-axis in matplotlib?

Advertisements