cphyc / matplotlib-label-lines

Label line using matplotlib.
MIT License
301 stars 28 forks source link

Can this work with areas? #76

Closed JulienPalard closed 2 years ago

JulienPalard commented 2 years ago

I'm trying to label areas, like here: https://github.com/JulienPalard/python-versions/blob/8aa82b48375f4771476fc821b0e8495d958173bb/python-versions.py#L161

It does not appear to work, I tried:

    to_plot.plot.area(stacked=True, figsize=(10, 10 * 2 / 3))
    labelLines(plt.gca().get_lines())
    plt.savefig("python-versions-pct.png")

And I'm getting:

Traceback (most recent call last):
  File "/home/mdk/clones/JulienPalard/python-versions/python-versions.py", line 170, in <module>
    plot_pct()
  File "/home/mdk/clones/JulienPalard/python-versions/python-versions.py", line 163, in plot_pct
    labelLines(plt.gca().get_lines())
  File "/home/mdk/.local/lib/python3.9/site-packages/labellines/core.py", line 222, in labelLines
    xdata = ensure_float(line.get_xdata())
AttributeError: 'PolyCollection' object has no attribute 'get_xdata'
cphyc commented 2 years ago

Hi @JulienPalard, the original aim of this package is to work with lines rather than areas, so I have no plan to implement it for surfaces myself, but I'd be happy to include such a code.

Note that in your case, you may be able to find a workaround by adding another line plot (the top/bottom/center of your area), eventually, keep it transparent and label this one.

cphyc commented 2 years ago

Closing this due to the lack of activity. If anyone is interested in implementing I would be happy to integrate such a change.