Matplotlib - Reverse Axes (2024)

Matplotlib - Reverse Axes (1)

  • Matplotlib Basics
  • Matplotlib - Home
  • Matplotlib - Introduction
  • Matplotlib - Vs Seaborn
  • Matplotlib - Environment Setup
  • Matplotlib - Anaconda distribution
  • Matplotlib - Jupyter Notebook
  • Matplotlib - Pyplot API
  • Matplotlib - Simple Plot
  • Matplotlib - Saving Figures
  • Matplotlib - Markers
  • Matplotlib - Figures
  • Matplotlib - Styles
  • Matplotlib - Legends
  • Matplotlib - Colors
  • Matplotlib - Colormaps
  • Matplotlib - Colormap Normalization
  • Matplotlib - Choosing Colormaps
  • Matplotlib - Colorbars
  • Matplotlib - Text
  • Matplotlib - Text properties
  • Matplotlib - Subplot Titles
  • Matplotlib - Images
  • Matplotlib - Image Masking
  • Matplotlib - Annotations
  • Matplotlib - Arrows
  • Matplotlib - Fonts
  • Matplotlib - What are Fonts?
  • Setting Font Properties Globally
  • Matplotlib - Font Indexing
  • Matplotlib - Font Properties
  • Matplotlib - Scales
  • Matplotlib - Linear and Logarthmic Scales
  • Matplotlib - Symmetrical Logarithmic and Logit Scales
  • Matplotlib - LaTeX
  • Matplotlib - What is LaTeX?
  • Matplotlib - LaTeX for Mathematical Expressions
  • Matplotlib - LaTeX Text Formatting in Annotations
  • Matplotlib - PostScript
  • Enabling LaTex Rendering in Annotations
  • Matplotlib - Mathematical Expressions
  • Matplotlib - Animations
  • Matplotlib - Artists
  • Matplotlib - Styling with Cycler
  • Matplotlib - Paths
  • Matplotlib - Path Effects
  • Matplotlib - Transforms
  • Matplotlib - Ticks and Tick Labels
  • Matplotlib - Radian Ticks
  • Matplotlib - Dateticks
  • Matplotlib - Tick Formatters
  • Matplotlib - Tick Locators
  • Matplotlib - Basic Units
  • Matplotlib - Autoscaling
  • Matplotlib - Reverse Axes
  • Matplotlib - Logarithmic Axes
  • Matplotlib - Symlog
  • Matplotlib - Unit Handling
  • Matplotlib - Ellipse with Units
  • Matplotlib - Spines
  • Matplotlib - Axis Ranges
  • Matplotlib - Axis Scales
  • Matplotlib - Axis Ticks
  • Matplotlib - Formatting Axes
  • Matplotlib - Axes Class
  • Matplotlib - Twin Axes
  • Matplotlib - Figure Class
  • Matplotlib - Multiplots
  • Matplotlib - Grids
  • Matplotlib - Object-oriented Interface
  • Matplotlib - PyLab module
  • Matplotlib - Subplots() Function
  • Matplotlib - Subplot2grid() Function
  • Matplotlib - Anchored Artists
  • Matplotlib - Manual Contour
  • Matplotlib - Coords Report
  • Matplotlib - AGG filter
  • Matplotlib - Ribbon Box
  • Matplotlib - Fill Spiral
  • Matplotlib - Findobj Demo
  • Matplotlib - Hyperlinks
  • Matplotlib - Image Thumbnail
  • Matplotlib - Plotting with Keywords
  • Matplotlib - Create Logo
  • Matplotlib - Multipage PDF
  • Matplotlib - Multiprocessing
  • Matplotlib - Print Stdout
  • Matplotlib - Compound Path
  • Matplotlib - Sankey Class
  • Matplotlib - MRI with EEG
  • Matplotlib - Stylesheets
  • Matplotlib - Background Colors
  • Matplotlib - Basemap
  • Matplotlib Event Handling
  • Matplotlib - Event Handling
  • Matplotlib - Close Event
  • Matplotlib - Mouse Move
  • Matplotlib - Click Events
  • Matplotlib - Scroll Event
  • Matplotlib - Keypress Event
  • Matplotlib - Pick Event
  • Matplotlib - Looking Glass
  • Matplotlib - Path Editor
  • Matplotlib - Poly Editor
  • Matplotlib - Timers
  • Matplotlib - Viewlims
  • Matplotlib - Zoom Window
  • Matplotlib Plotting
  • Matplotlib - Bar Graphs
  • Matplotlib - Histogram
  • Matplotlib - Pie Chart
  • Matplotlib - Scatter Plot
  • Matplotlib - Box Plot
  • Matplotlib - Violin Plot
  • Matplotlib - Contour Plot
  • Matplotlib - 3D Plotting
  • Matplotlib - 3D Contours
  • Matplotlib - 3D Wireframe Plot
  • Matplotlib - 3D Surface Plot
  • Matplotlib - Quiver Plot
  • Matplotlib Useful Resources
  • Matplotlib - Quick Guide
  • Matplotlib - Useful Resources
  • Matplotlib - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

What is Reverse Axes?

In Matplotlib reverse axes refers to changing the direction of an axis, flipping it from its default orientation. This action alters the visual representation of the plot by reversing the order of the data along a specific axis usually the x-axis or y-axis.

Reversing X-axis

To reverse the x-axis in Matplotlib we can use the plt.gca().invert_xaxis() function. This method inverts the direction of the x-axis effectively flipping the plot horizontally. Data points that were originally plotted from left to right will now be displayed from right to left.

Here is a detailed breakdown of how to reverse the x-axis:

Steps to Reverse the X-axis

The below are the steps to be followed to reverse the x-axis.

Create a Plot

Generate a plot with our data using Matplotlib.

Example

import matplotlib.pyplot as plt# Sample datax = [1, 2, 3, 4, 5]y = [2, 4, 6, 8, 10]# Plot with default axis orientationplt.plot(x, y, marker='o')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Default X-axis')plt.show()
Output

Matplotlib - Reverse Axes (2)

Reverse the X-axis

After creating the plot use plt.gca().invert_xaxis() to reverse the x-axis.

Example

import matplotlib.pyplot as plt# Sample datax = [1, 2, 3, 4, 5]y = [2, 4, 6, 8, 10]# Plot with default axis orientationplt.plot(x, y, marker='o')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Default X-axis')plt.show()# Reversing the x-axisplt.plot(x, y, marker='o')plt.gca().invert_xaxis() # Reverse x-axisplt.xlabel('Reversed X-axis')plt.ylabel('Y-axis')plt.title('Reversed X-axis')plt.show()
Output

Matplotlib - Reverse Axes (3)Matplotlib - Reverse Axes (4)

The second plot will display the same data as the first one but the x-axis will be reversed. Data points that were initially on the left side will now appear on the right side by altering the visual representation of the data.

Use Cases for Reversing the X-axis

Flipping Time Series Data − When we are plotting time series data then reversing the x-axis might better align with the chronological order.

Reorienting Geographical Plots − In some geographical plots reversing the x-axis could match the expected orientation or conventions.

Reversing the x-axis provides an alternative perspective for visualizing data allowing us to present information in a different order or orientation for clearer insights or better alignment with conventions.

This function reverses the direction of the x-axis by flipping the plot horizontally. Data points that were originally plotted from left to right will now be displayed from right to left.

Reversing Y-axis

The plt.gca().invert_yaxis() function reverses the direction of the y-axis by flipping the plot vertically. Data points that were originally plotted from bottom to top will now be displayed from top to bottom.

The reversing of Y - axis is also as same as the reversing the X - axis of a plot which we seen in the upper section. The below are the steps to be followed for reversing the Y - axis.

Create a Plot

Generate a plot with our data using Matplotlib.

Example
import matplotlib.pyplot as plt# Sample datax = [1, 2, 3, 4, 5]y = [2, 4, 6, 8, 10]# Plot with default axis orientationplt.plot(x, y, marker='o')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Default Y-axis')plt.show()
Output

Matplotlib - Reverse Axes (5)

Reverse the Y-axis

After creating the plot use plt.gca().invert_yaxis() to reverse the y-axis.

Example
import matplotlib.pyplot as plt# Sample datax = [1, 2, 3, 4, 5]y = [2, 4, 6, 8, 10]# Plot with default axis orientationplt.plot(x, y, marker='o')plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Default Y-axis')plt.show()# Reversing the x-axis# Reversing the y-axisplt.plot(x, y, marker='o')plt.gca().invert_yaxis() # Reverse y-axisplt.xlabel('X-axis')plt.ylabel('Reversed Y-axis')plt.title('Reversed Y-axis')plt.show()
Output

Matplotlib - Reverse Axes (6)Matplotlib - Reverse Axes (7)

Print Page

Previous Next

Advertisem*nts

';adpushup.triggerAd(ad_id); });

Matplotlib - Reverse Axes (2024)
Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5575

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.