How to rotate a simple matplotlib Axes?

To rotate a simple matplotlib axes, we can use the Affine2D transformation along with floating_axes. This technique creates a rotated coordinate system for plotting data at different angles.

Required Imports

First, import the necessary packages for creating rotated axes ?

import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes

Steps to Rotate Axes

The rotation process involves these key steps:

  • Create an affine transformation − Define the rotation angle using Affine2D().rotate_deg()
  • Set axis limits − Define the coordinate range for both x and y axes
  • Create grid helper − Use GridHelperCurveLinear() to handle the transformation
  • Add floating subplot − Create the rotated axes and add it to the figure

Example

Here's a complete example that rotates axes by 25 degrees ?

import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes

# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create figure
fig = plt.figure()

# Define axis limits (x_min, x_max, y_min, y_max)
scales = (0, 5, 0, 5)

# Create 2D affine transformation with 25-degree rotation
t = Affine2D().rotate_deg(25)

# Create grid helper for curved coordinate transformation
h = floating_axes.GridHelperCurveLinear(t, scales)

# Create floating subplot with rotated axes
ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=h)

# Add the subplot to figure
fig.add_subplot(ax)

# Display the plot
plt.show()

How It Works

The rotation mechanism works through coordinate transformation:

  • Affine2D().rotate_deg(25) creates a transformation matrix that rotates coordinates by 25 degrees
  • GridHelperCurveLinear() handles the mapping between the original and rotated coordinate systems
  • FloatingSubplot() creates axes that can be positioned and oriented freely within the figure

Different Rotation Angles

You can easily change the rotation angle by modifying the parameter in rotate_deg() ?

import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes

fig, axes = plt.subplots(1, 3, figsize=(12, 4))
angles = [15, 45, 90]

for i, angle in enumerate(angles):
    # Create transformation for each angle
    t = Affine2D().rotate_deg(angle)
    scales = (0, 5, 0, 5)
    
    # Create grid helper and floating subplot
    h = floating_axes.GridHelperCurveLinear(t, scales)
    ax = floating_axes.FloatingSubplot(fig, 1, 3, i+1, grid_helper=h)
    
    fig.add_subplot(ax)
    ax.set_title(f'Rotated {angle}°')

plt.tight_layout()
plt.show()

Conclusion

Rotating matplotlib axes requires Affine2D transformations and floating axes. Use rotate_deg() to specify the angle and GridHelperCurveLinear() to handle coordinate mapping. This technique is useful for creating custom plot orientations and specialized visualizations.

---
Updated on: 2026-03-26T02:27:39+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements