Skip to content

[Bug]: set_xlim, set_xbounds being ignored when set_aspect('equal', 'datalim') being used #28673

Closed
@kjayawar

Description

@kjayawar

Bug summary

When ax.set_aspect('equal', 'datalim') is used, proceeding ax.set_xbound() or ax.set_xlim() calls being ignored

Code to reproduce the error

import matplotlib.pyplot as plt
import numpy as np

# Generate some random data
x = np.arange(0, 10, 0.1)
y = np.random.randn(len(x))

# Create a plot
fig, ax = plt.subplots()
plt.ion()
ax.set_aspect('equal', 'datalim')


# Set x-axis limits
ax.set_xlim(2, 5)


# Print the x and y limits
print(f"x-axis limits before plotting: {ax.get_xlim()}")
print(f"y-axis limits before plotting: {ax.get_ylim()}")

ax.plot(x, y*10)


# Print the x and y limits
print(f"x-axis limits after plotting: {ax.get_xlim()}")
print(f"y-axis limits after plotting: {ax.get_ylim()}")

# Set x-axis limits
ax.set_xbound(lower=2, upper=5)
# ax.set_xlim(2, 5)

# Print the x and y limits
print(f"x-axis limits after setting xlim: {ax.get_xlim()}")
print(f"y-axis limits after setting xlim: {ax.get_ylim()}")

# Show the plot
plt.show()

What is been tried to mitigate the issue
ax._autoscaleXon = False

Actual outcome

This code produces a plot with x-axis not limited to the given xrange 2-5.
image

Expected outcome

xlimit obeys the given range, with y range automatically adjusted to the given aspect ratio

Additional information

No response

Operating system

windows

Matplotlib Version

3.8.2

Matplotlib Backend

QtAgg

Python version

Python 3.9.7

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions