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

Skip to content

[Bug]: Enforce that Line data modifications are sequences #28927

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
andyfaff opened this issue Oct 3, 2024 · 2 comments · Fixed by #28943
Closed

[Bug]: Enforce that Line data modifications are sequences #28927

andyfaff opened this issue Oct 3, 2024 · 2 comments · Fixed by #28943
Milestone

Comments

@andyfaff
Copy link
Contributor

andyfaff commented Oct 3, 2024

Bug summary

It used to be possible to use Line2D.set_ydata with a single scalar. From a quick search I think this behaviour was changed in #22329, and I guess it makes sense for a lot of cases. However, I don't think mandating a sequence really makes sense when Line2D was created by ax.axhline, or ax.axvline, because those two methods use scalars themselves, not sequences.

Code for reproduction

fig, ax = plt.subplots()
l = ax.axvline(x=10)
l.set_xdata(15)

Actual outcome

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[8], line 4
      2 fig, ax = plt.subplots()
      3 l = ax.axvline(x=10)
----> 4 l.set_xdata(15)

File [~/miniforge3/envs/dev3/lib/python3.12/site-packages/matplotlib/lines.py:1289](http://localhost:8888/lab/tree/data/reduce/~/miniforge3/envs/dev3/lib/python3.12/site-packages/matplotlib/lines.py#line=1288), in Line2D.set_xdata(self, x)
   1276 """
   1277 Set the data array for x.
   1278 
   (...)
   1286 set_ydata
   1287 """
   1288 if not np.iterable(x):
-> 1289     raise RuntimeError('x must be a sequence')
   1290 self._xorig = copy.copy(x)
   1291 self._invalidx = True

RuntimeError: x must be a sequence

Expected outcome

A plot with horizontal/vertical line

Additional information

No response

Operating system

No response

Matplotlib Version

3.9.2

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

pip

@timhoffm
Copy link
Member

timhoffm commented Oct 3, 2024

There is no easy solution.

The created Line2D is a generic independent object, which doesn't know about the "single y-value" property of a horizontal line.
Not allowing scalar values makes sense there.

Options are:

  1. Accept scalar values in Line2D - we decided explicitly against this in Enforce that Line data modifications are sequences #22329.
  2. Create a (sub?)class HorizontalLine2D - This would go into the direction of semantic Artists, which we are basically open for. But I'm not convinced it makes sense to have an individual Artist for every function.
  3. Make the behavior of Line2D configurable, easiest through a simple Line2D._allow_scalar_y that axvline would overwrite. But this is either quite hacky because do is silently (i.e. also the set_ydata documentation would not reflect it), or we'd have to promote it to a regular feature, which does not really make sense for Line2D and adds complexity.
  4. Do nothing and live with the surprise and inconvenience that you have to pass two values.

My preference is (4) > (2) > (1) > (3)

Edit: There is one more way that's in a sense even hackier than (3) but may limit unintended fallout. If a scalar is passed: Check if all existing values are the same, and if so, replace the values. 🤯

@andyfaff
Copy link
Contributor Author

andyfaff commented Oct 4, 2024

I came across this because I'd had code that was working for a long time (axvline + axhline), and suddenly set_ydata/set_xdata failed, without a visible deprecation warning. I fixed the code in my project, and because we make regular releases it'll limit the fallout. Perhaps a documentation fix will be enough?

timhoffm added a commit to timhoffm/matplotlib that referenced this issue Oct 7, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Oct 7, 2024
@rcomer rcomer added this to the v3.10.0 milestone Oct 7, 2024
kyracho pushed a commit to kyracho/matplotlib that referenced this issue Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants