Thanks to visit codestin.com
Credit goes to github.com

Skip to content

span_where fails with timeseries on the x-axis #15973

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

Closed
szeitlin opened this issue Dec 18, 2019 · 3 comments · Fixed by #24864
Closed

span_where fails with timeseries on the x-axis #15973

szeitlin opened this issue Dec 18, 2019 · 3 comments · Fixed by #24864

Comments

@szeitlin
Copy link

Bug report

Bug summary

Similar to #3872

Code for reproduction

I can't attach a CSV or an .ipynb, but I'll zip them up together and attach that.
span_where.zip

collection = collections.BrokenBarHCollection.span_where(df.index,ymin=0,ymax=df['numeric_col'].max(),
                                                         where=df['bool_col']==True,
                                                            facecolor='g', 
                                                            edgecolor='red', 
                                                            linewidth=2.0, linestyle='--', 
                                                            alpha=0.2, 
                                                            label='span')
axes.add_collection(collection)

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-264dcc0313f6> in <module>
      6                                                             linewidth=2.0, linestyle='--',
      7                                                             alpha=0.2,
----> 8                                                             label='span')
      9 axes.add_collection(collection)

/opt/anaconda3/envs/eda/lib/python3.7/site-packages/matplotlib/collections.py in span_where(x, ymin, ymax, where, **kwargs)
   1131 
   1132         collection = BrokenBarHCollection(
-> 1133             xranges, [ymin, ymax - ymin], **kwargs)
   1134         return collection
   1135 

/opt/anaconda3/envs/eda/lib/python3.7/site-packages/matplotlib/collections.py in __init__(self, xranges, yrange, **kwargs)
   1111                   (xmin + xwidth, ymin),
   1112                   (xmin, ymin)] for xmin, xwidth in xranges]
-> 1113         PolyCollection.__init__(self, verts, **kwargs)
   1114 
   1115     @staticmethod

/opt/anaconda3/envs/eda/lib/python3.7/site-packages/matplotlib/collections.py in __init__(self, verts, sizes, closed, **kwargs)
   1044         Collection.__init__(self, **kwargs)
   1045         self.set_sizes(sizes)
-> 1046         self.set_verts(verts, closed)
   1047         self.stale = True
   1048 

/opt/anaconda3/envs/eda/lib/python3.7/site-packages/matplotlib/collections.py in set_verts(self, verts, closed)
   1065                     codes[0] = mpath.Path.MOVETO
   1066                     codes[-1] = mpath.Path.CLOSEPOLY
-> 1067                     self._paths.append(mpath.Path(xy, codes))
   1068                 else:
   1069                     self._paths.append(mpath.Path(xy))

/opt/anaconda3/envs/eda/lib/python3.7/site-packages/matplotlib/path.py in __init__(self, vertices, codes, _interpolation_steps, closed, readonly)
    125             and codes as read-only arrays.
    126         """
--> 127         vertices = _to_unmasked_float_array(vertices)
    128         if vertices.ndim != 2 or vertices.shape[1] != 2:
    129             raise ValueError(

/opt/anaconda3/envs/eda/lib/python3.7/site-packages/matplotlib/cbook/__init__.py in _to_unmasked_float_array(x)
   1388         return np.ma.asarray(x, float).filled(np.nan)
   1389     else:
-> 1390         return np.asarray(x, float)
   1391 
   1392 

/opt/anaconda3/envs/eda/lib/python3.7/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

TypeError: float() argument must be a string or a number, not 'Timestamp'

Expected outcome

this works:

axes = df['numeric_col'].plot() 
axes.fill_between(df.index,0,df['numeric_col'].max(),where=df['bool_col']==False, interpolate=True, color='lightskyblue')

Screen Shot 2019-12-17 at 5 39 52 PM

Matplotlib version

  • Operating system: Mac OS 10.14.6

  • Matplotlib version: 3.1.1

  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline

  • Python version: python 3.7.5

  • Jupyter version (if applicable):
    jupyter 1.0.0 py37_7
    jupyter_client 5.3.4 py37_0
    jupyter_console 6.0.0 py37_0
    jupyter_core 4.6.1 py37_0

  • Other libraries:
    pandas 0.25.3

installed with conda

conda default channel

@jklymak
Copy link
Member

jklymak commented Dec 18, 2019

Can you reproduce with synthetic data and the python code (i.e. w/o the CSV or ipynb)? That will save us a lot of time, and not expose us to whatever is in your *.zip. Thanks for your help!

@szeitlin
Copy link
Author

I did. That's what's in the .zip. It's literally a 1000-line CSV file and an ipython notebook showing exactly how to reproduce the bug.

@anntzer anntzer added Difficulty: Hard https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues topic: units and array ducktypes labels Dec 19, 2019
@anntzer
Copy link
Contributor

anntzer commented Dec 19, 2019

Basically, the problem right now is that unit handling is done by the Axes methods (which massage unitful data into unitless data before passing it to construct the artist), but span_where just creates an artist independent of the Axes and therefore has no way of accessing the unit support system (because the artist needs to be fully init'ed before being added to the Axes).
In fact that seems quite difficult to fix as it is; I guess the simplest would be to have an Axes method that duplicates span_where?


edit: I guess that may just be adding where=... support to Axes.broken_barh.

Still, the architectural point about Artist-level unit support remains.

@anntzer anntzer removed the Difficulty: Hard https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues label Dec 19, 2019
@QuLogic QuLogic added this to the v3.7.0 milestone Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants