Skip to content

[feature request] Accepting slice arguments in ax.set_xlim() (Python 3) #8349

@randomstuff

Description

@randomstuff

Bug report

Bug summary

  • ax.set_xlim() accepts accepts a tuple as argument for setting the limit of the X axis;
  • Python 3, provides the slice type which is used for reprenting a slice and is used for x[2:3] slicing;
  • Pandas uses this in order to get a slice of a Series/DataFrame : df[begin:end] or df[get_range(params)];
  • It would be nice to be able to pass a slice argument instead of a tuple in ax.set_xlim() for consistency/easability.

Code for reproduction

from datetime import date
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

index = pd.DatetimeIndex(start=date(2017,3,1), end=date(2017,3,17), freq="1H")
data = pd.Series(np.random.normal(size=len(index)), index=index)

s = slice(date(2017,3,10), date(2017,3,15))

ax = data[data>0][s].plot()

# Not working:
ax.set_xlim(s)
# Working:
# ax.set_xlim((s.start, s.stop))

plt.show()

Actual outcome

Traceback (most recent call last):
  File "s.py", line 14, in <module>
    ax.set_xlim(s)
  File "/home/foo/.local/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2904, in set_xlim
    left, right = mtransforms.nonsingular(left, right, increasing=False)
  File "/home/foo/.local/lib/python3.5/site-packages/matplotlib/transforms.py", line 2773, in nonsingular
    if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Expected outcome

I'd expect ax.set_xlim(slice(a, b)) to behave as ax.set_xlim((a,b)).

Matplotlib version

Using matplotlib (2.0.0) installed through pip.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions