Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
randomstuff opened this issue Mar 20, 2017 · 7 comments
Closed

Comments

@randomstuff
Copy link

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.

@story645
Copy link
Member

story645 commented Apr 3, 2017

slice objects have always been around in Python and if we go this route, both formal slice objects and index slicing of the df[a:b] variety should probably be supported.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Apr 4, 2017
@tacaswell
Copy link
Member

The request is to just take the start / stop values from the slice, not to try to slice into anything right?

This seems like it would cause confusion with

x = range(100, 200)
fig, ax = plt.subplots()
ax.plot(x, x)
ax.set_xlim(slice(10, 50))

as people might expect that we would pick the range slicing into the data of one of the artists.

This should be easy enough to implement (the set_*lim code already have code for unpacking an iterable, just add a isinstance(left, slice) there and patch up the logic), I don't think this is harmful, but am also not fully sold on the value (but it does seem like a nice quality of life thing).

From the example this really only makes sense when working with slices into non-trivial panads dataframes/series? I expect it to be nice with xarray as well?

If we do this, exact work:

  • in set_xlim, set_ylim, and set_zlim adjust the input validation to also extract sensible data from a slice object.
    • don't change lower/upper limit if s.start or s.stop is None
    • do we look at the sign of the step to invert the order?
  • update docstrings
  • tests
  • whats_new entry
  • example

@anntzer
Copy link
Contributor

anntzer commented Apr 15, 2017

FWIW I am against the proposal.

  1. A start, stop pair is not the same thing as a slice (which is a start, stop, step triplet -- what do you do want to do when someone gives you a nontrivial step).
  2. The usability gain seems negative. I doubt anyone plans to pass literals slices (it's not as if the slice literal was really nice) and the use case in the OP is better solved by calling ax.margins(x=0).

@nvedant07
Copy link

Are we going ahead with this request? If yes, Can I work on it?

@anntzer
Copy link
Contributor

anntzer commented May 5, 2017

I am still voting no.

@efiring
Copy link
Member

efiring commented May 5, 2017

I am strongly opposed. As far as I can see, this API change adds complexity and confusion, with no real gain.

@tacaswell
Copy link
Member

I am ambivalent to negative and there are 2 other 👎 so closing this as wont-fix, sorry.

@tacaswell tacaswell modified the milestones: unassigned, 2.1 (next point release) May 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants