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

Marker kwarg in grid raise exception #25910

Open
jjgomera opened this issue May 18, 2023 · 4 comments
Open

Marker kwarg in grid raise exception #25910

jjgomera opened this issue May 18, 2023 · 4 comments

Comments

@jjgomera
Copy link

Description

Using marker kwarg in grid raise a exception

Version used

python 3.11.2, really I found this bug same time ago using 3.9 or so
matplotlib 3.6.3
Although the error names Tkinter, the error is also reproduced with qt backend

Code to reproduce

from matplotlib import pyplot

x = [0, 1]
y = [0, 1]
pyplot.plot(x, y)
pyplot.grid(True, marker="o")
pyplot.show()

And the exception raised...

jjgomera@msi:~/Programacion/pychemqt$ python3 bug.py 
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.11/tkinter/__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/tkinter/__init__.py", line 861, in callit
    func(*args)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/_backend_tk.py", line 251, in idle_draw
    self.draw()
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 10, in draw
    super().draw()
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 405, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 3082, in draw
    mimage._draw_list_compositing_images(
  File "/usr/lib/python3/dist-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 3100, in draw
    mimage._draw_list_compositing_images(
  File "/usr/lib/python3/dist-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1304, in draw
    ticks_to_draw = self._update_ticks()
                    ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1192, in _update_ticks
    major_ticks = self.get_major_ticks(len(major_locs))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1534, in get_major_ticks
    tick = self._get_tick(major=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1483, in _get_tick
    return self._tick_class(self.axes, 0, major=major, **tick_kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 422, in __init__
    super().__init__(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 166, in __init__
    self.gridline = mlines.Line2D(
                    ^^^^^^^^^^^^^^
TypeError: matplotlib.lines.Line2D() got multiple values for keyword argument 'marker'


@jklymak
Copy link
Member

jklymak commented May 18, 2023

Did this ever work? What did it do, or do you expect it to do?

@ksunden
Copy link
Member

ksunden commented May 18, 2023

For what its worth, i do find it unlikely that it would actually do what you expect.

Here is what I get if I modify the grid to have markers (just by editing the last line in the traceback to have marker='o' instead of marker='', i.e. not making it so that explicitly passing doesn't error):

Figure_1

This seems unlikely to be desirable, I don't think? but it wouldn't be too hard to add handling for marker like we do for other kwargs there... it is also be possible to workaround it by setting the marker after enabling the grid for the first time, though that can still be fragile when continuing to manipulate the plot (i.e. it will still raise later on):

plt.grid()
plt.grid(marker="o")

or

plt.gca().xaxis.grid(marker="o")
plt.gca().yaxis.grid(marker="o")

It's not the most elegant, but it works.

If, for instance, you were expecting a lattice of points at each intersection of the grid lines, that is not something that actually fits into the way grid is implemented, as it is done on the individual axis (e.g. X axis or Y axis, with no knowledge of where the ticks are )

@jjgomera
Copy link
Author

Thanks, I just want to allow the user full control of the graphics settings, in the same way that you can set the line or its color. Thanks for the workaround. You are right, the marks do not make much sense in the grid, but I want to point out that in the documentation of grid, marker and its related kwargs should not be included, or if they do the result must be as you show, but no raise exception.

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.grid.html

@timhoffm
Copy link
Member

I'm -1 on allowing marker for grid. That doesn't make sense. It should be an implementation detail that these lines are implemented using Line2D.

Correcting the list in https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.grid.html is not easily possible, because it is auto-generated from Line2D, which is where the implementation leaks out. I don't consider this an important enough problem to invest any effort. If you want to, modify the docs to something like

Valid keyword arguments are (with the exception of marker):

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

No branches or pull requests

4 participants