Closed
Description
Bug summary
Using matplotlib to plot multiple columns from a pandas DataFrame in a single plot using separate function calls.
- This first function call returns a plot object with one line plot.
- The second function call refers to this plot object using the
ax
kwarg
This fails with the following error:
AttributeError: 'Legend' object has no attribute 'legendHandles'. Did you mean: 'legend_handles'?
Code for reproduction
>>> import pandas as pd
>>> df = pd.DataFrame({"x": [1,2,3], "y1": [10,20,10], "y2": [10, 25, 5]})
>>> p = df.plot(kind="line", x="x", y="y1", color="green", xlabel="X value", ylabel="Series 1")
>>> df.plot(ax = p, kind="line", x="x", y="y2", color="blue", xlabel="X value", ylabel="Series 2")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/.venv/lib/python3.11/site-packages/pandas/plotting/_core.py", line 1000, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "~/.venv/lib/python3.11/site-packages/pandas/plotting/_matplotlib/__init__.py", line 71, in plot
plot_obj.generate()
File "~/.venv/lib/python3.11/site-packages/pandas/plotting/_matplotlib/core.py", line 454, in generate
self._make_legend()
File "~/.venv/lib/python3.11/site-packages/pandas/plotting/_matplotlib/core.py", line 792, in _make_legend
handles = leg.legendHandles
^^^^^^^^^^^^^^^^^
AttributeError: 'Legend' object has no attribute 'legendHandles'. Did you mean: 'legend_handles'?
Actual outcome
Error
Expected outcome
Second series written to plot
Additional information
Python 3.11.9 (main, Apr 6 2024, 17:59:24) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Pandas: 1.5.3
Matplotlib:
- 3.9.1.post1 FAILS
- 3.9.1 FAILS
- 3.9.0 FAILS
- 3.8.4 OK
Operating system
Ubuntu 24.04 (x64)
Matplotlib Version
3.9.0
Matplotlib Backend
TkAgg
Python version
3.11.9
Jupyter version
n/a
Installation
pip