- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 switch axes in Matplotlib?
To switch axes in matplotlib, we can create a figure and add two subplots using subplots() method. Plot curves, extract x and y data, and set these data in a second plotted curve.
Steps
Create x and y data points using numpy.
Create a figure and add a set of two subplots.
Set the title of the plot on both the axes.
Plot x and y data points using plot() method.
Extract the x and y data points using get_xdata and get_ydata.
To switch the axes of the plot, set x_data and y_data of the axis 1 curve to axis 2 curve.
Adjust the padding between and around the subplots.
To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 50) y = np.sin(x) f, axes = plt.subplots(2) axes[0].set_title("First plot on axis") curve, = axes[0].plot(x, y, c='r') newx = curve.get_xdata() newy = curve.get_ydata() axes[1].set_title("Switch of first plot") curve2, = axes[1].plot(x, y, c='r') curve2.set_xdata(newy) curve2.set_ydata(newx) plt.show()
Output
- Related Articles
- How to make axes transparent in Matplotlib?
- How to change axes background color in Matplotlib?
- How to hide axes and gridlines in Matplotlib?
- How to rotate a simple matplotlib Axes?
- How to plot a point on 3D axes in Matplotlib?
- How to get a matplotlib Axes instance to plot to?
- Change x axes scale in matplotlib
- How can I hide the axes in Matplotlib 3D?
- How to plot sine curve on polar axes using Matplotlib?
- How to combine several matplotlib axes subplots into one figure?
- How to make simple double head arrows on the axes in Matplotlib?
- Multiple axes in Matplotlib with different scales
- Plot 3D bars without axes in Matplotlib
- Rotating axes label text in 3D Matplotlib
- How to plot single data with two Y-axes (two units) in Matplotlib?

Advertisements