Skip to content

Remove restriction in plt.margins(m) to 0 <= m <= 1  #9334

Closed
@ImportanceOfBeingErnest

Description

@ImportanceOfBeingErnest

Setting the margin (ax.margins) to something outside the range between 0 and 1

ax.margins(x=3)

result in an error. This is due to the lines:

if m < 0 or m > 1:
        raise ValueError("margin must be in range 0 to 1")

in the axes._base's set_xmargin method.
This limitation seems rather arbitrary to me.

If one comments out the restriction to 0 <= m <= 1, setting ax.margins(3) produces a result as expected, so it does not even seem to have any bad implications

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0,0,1,0,1])*1.9+10.466
y = np.array([0,1,1,0,0])*1.9+10.466
x2 = np.array([1,0,0.5,1,1])*1.9+10.466
y2 = np.array([0,1,1.5,1,0])*1.9+10.466

plt.plot(x,y)
plt.plot(x2,y2)

plt.margins(3)

plt.show()

image

Sure, we can replace the single line plt.margins(3) by

margin=3
xmax = np.max([x.max(),x2.max()])
xmin = np.min([x.min(),x2.min()])
ymax = np.max([y.max(),y2.max()])
ymin = np.min([y.min(),y2.min()])

plt.gca().set_xlim(xmin-(xmax-xmin)*margin, xmax+(xmax-xmin)*margin)
plt.gca().set_ylim(ymin-(ymax-ymin)*margin, ymax+(ymax-ymin)*margin)

but if there isn't really any reason for the restriction, this would be overly complicated.

Therefore I would like to ask if there is a reason for the restriction 0 <= m <= 1, and if not whether we could simply get rid of it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions