Repository navigation
Closed
Closed
Copy link
Milestone
Description
Bug report
Bug summary
plt.errorbar( ..., marker=<matplotlib.markers.MarkerStyle object>) raises a `TypeError: float() argument must be a string or a number, not 'MarkerStyle' unexpectedly.
Code for reproduction
#!/usr/bin/env python3
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.markers import MarkerStyle
print(f"{matplotlib.__version__=}")
print(matplotlib.get_backend())
# I want to use this marker in an errorbar plot
m = MarkerStyle("D")
m._transform.scale(1.0, 0.6)
print(m)
# I can use it in a scatter plot
fig, ax = plt.subplots()
plt.scatter([1,2,3],[1,2,3], s=225, marker="d")
plt.scatter([1,2,3],[2,3,4], s=225, marker=m, color="crimson")
plt.errorbar([1,2,3],[3,4,5], yerr=[0.5,0.5,0.5], marker='o', linestyle='none', color='green')
plt.show()
# which produces a figure similar to https://stackoverflow.com/a/49662571/
# But when I use it in errorbar
fix, ax = plt.subplots()
plt.errorbar([1,2,3],[3,4,5], yerr=[0.5,0.5,0.5], marker=m, linestyle='none', color='green')
plt.show()Actual outcome
The first figure is produced with a correctly-rotated marker, similar to this Stack Overflow answer, which looks like this:
After closing the first figure, the second figure, in contrast, gives an error.
> ./example.py
matplotlib.__version__='3.2.2'
<matplotlib.markers.MarkerStyle object at 0x106e89d00>
Traceback (most recent call last):
File "./example.py", line 25, in <module>
plt.errorbar([1,2,3],[3,4,5], yerr=[0.5,0.5,0.5], marker=m, color='green')
File "/usr/local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2524, in errorbar
return gca().errorbar(
File "/usr/local/lib/python3.8/site-packages/matplotlib/__init__.py", line 1565, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/usr/local/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 3265, in errorbar
data_line = mlines.Line2D(x, y, **plot_line_style)
File "/usr/local/lib/python3.8/site-packages/matplotlib/lines.py", line 378, in __init__
self._marker = MarkerStyle(marker, fillstyle)
File "/usr/local/lib/python3.8/site-packages/matplotlib/markers.py", line 225, in __init__
self.set_marker(marker)
File "/usr/local/lib/python3.8/site-packages/matplotlib/markers.py", line 289, in set_marker
Path(marker)
File "/usr/local/lib/python3.8/site-packages/matplotlib/path.py", line 127, in __init__
vertices = _to_unmasked_float_array(vertices)
File "/usr/local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 1317, in _to_unmasked_float_array
return np.asarray(x, float)
File "/usr/local/lib/python3.8/site-packages/numpy/core/_asarray.py", line 83, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number, not 'MarkerStyle'
Expected outcome
The second figure should show a sequence of diamonds that are the thin_diamond marker but rotated 90˚ (so that it's wider than it is tall), like the red diamonds in the first figure, but with error bars of width 0.5 (like the green circles in the first figure).
Matplotlib version
- Operating system: macOS Catalina 10.15.6
- Matplotlib version: 3.2.2
- Matplotlib backend (
print(matplotlib.get_backend())): MacOSX - Python version: 3.8.5
- Jupyter version (if applicable):
- Other libraries:
Installed mpl via pip3.
Metadata
Metadata
Assignees
Labels
No labels

Activity
[-]plt.errorbar raises error when given `marker=<matplotlib.markers.MarkerStyle object>`[/-][+]plt.errorbar raises error when given marker=<matplotlib.markers.MarkerStyle object>[/+]evanberkowitz commentedon Sep 28, 2020
A temporary workaround is to
scatterwith the marker anderrorbarwithmarker=None.Even with this workaround, however, my manual construction of a legend fails. For my 'real' example (non minimal-working-example) I need to construct a legend manually.
In the script below when
WORKAROUND_PROBLEM=Truethe legend construction fails, when it isFalsethe original problem is demonstrated.The result of the legend version of the problem is
The expected outcome is that the figure on which the legend is failing should look like the second figure (scatter+errorbar with the legend given by a
label).jklymak commentedon Sep 28, 2020
Someone simply forgot to add that functionality to
errorbar. Probably an easy enough fix?evanberkowitz commentedon Sep 28, 2020
I hope so! The tracebacks of both the
errorbarand legend work-around share some common lines---there might be a place 'upstream' to make this fix?evanberkowitz commentedon Sep 28, 2020
@jklymak Can you point me in the right direction (maybe to a specific file?) where I can compare
errorbarandscatter? I tried grepping around but didn't find anything obvious...QuLogic commentedon Sep 28, 2020
errorbarandscatterare defined inlib/matplotlib/axes/_axes.pyevanberkowitz commentedon Sep 28, 2020
OK, now that I have a virtual development environment set up (having followed these instructions), the behavior is different!
I removed the workaround part of my script---here's what I'm currently testing:
I changed how I was getting my new marker by rotating 'd' rather than scaling 'D'.
The first figure remains the same (OK, I added a legend in the upper left using the built-in
label=...andplt.legend()---the legend shows the wide diamond correctly)The second figure now plots the markers in the body of the figure correctly but the marker does not show correctly in the legend---it isn't wider than it is tall / rotated! Instead, it's taller than it is wide (like the usual 'd').
When I run I now get no exceptions
[exit 0]
QuLogic commentedon Sep 28, 2020
I think the original issue would have been fixed by #17930, because
markeris now passed through the normal process that would happen viaplot. If there's something wrong with the legends, it's probably inlegend_handler.py.That being said, I'm not sure modifying a private attribute like
_transformis a supported approach. This may or may not be okay if you subclass it instead.evanberkowitz commentedon Sep 29, 2020
@QuLogic indeed---you're right. I think that the problem is that when
legline_markeris constructed and its propertiesupdate_proped, the marker isn't truly copied---I think the new one is built from the string from the old one (in this case 'd'). Insertingto (losslessly?) copy accomplishes what I need (but, as you point out, is not generic). If the marker copy was deep, there would be no problem. But I have found myself down a rabbit's hole of
HandlerBase'supdate_prop,_update_prop,_default_update_propand the link. I believe I wind up at line 70 oflegend_hanlder.py,at which point I do not understand what happens---legend handlers do not seem to have an
update_from(and neither doHandlerLine2DorHandlerBaseinheirit fromArtist(which does have such a method).Obviously my little hack (line 244 above) is not a permanent solution. How do you suggest I proceed?
evanberkowitz commentedon Sep 29, 2020
I see---the
legend_handleis aLine2D, not a legend handler! My issue seems to stem from the fact that inLine2D'supdate_frommethod, the marker isn't copied so much as it is reconstructed:while almost every other property is copied in the
self = otherway, Markers are reconstructed from theget_marker()andget_fillstyle(). I see that these lines have been this way since 2011---is there a good reason to not just copy but to reconstruct the marker?evanberkowitz commentedon Sep 29, 2020
With my #18603 I get a figure identical to my hacky solution of 2 comments ago.