JuliaPy / PyPlot.jl

Plotting for Julia based on matplotlib.pyplot
https://github.com/JuliaPy/PyPlot.jl
MIT License
475 stars 87 forks source link

get_legend_handles_labels() for errorbar plot not working properly #457

Open carstenbauer opened 5 years ago

carstenbauer commented 5 years ago

Related to https://github.com/JuliaPy/PyPlot.jl/issues/401.

I'm simply trying to reverse the order of the legend entries of two errorbar plots.

Pure python (from within Julia):

py"""
import matplotlib.pyplot as plt
import numpy as np
plt.errorbar(np.random.rand(10), np.random.rand(10), np.random.rand(10), label='first', marker='o', linestyle="")
plt.errorbar(np.random.rand(10), np.random.rand(10), np.random.rand(10), label='second', marker='o', linestyle="")
h,l = plt.gca().get_legend_handles_labels()
plt.legend(reversed(h),reversed(l), frameon=True)
"""

image

Using PyPlot:

errorbar(rand(10), rand(10), rand(10), label="first", marker="o", linestyle="")
errorbar(rand(10), rand(10), rand(10), label="second", marker="o", linestyle="")
h,l = gca().get_legend_handles_labels()
legend(reverse(h),reverse(l), frameon=true)

image

Note that the handles are wrong (should be markers with error bars).

PyPlot + ugly fix:

errorbar(rand(10), rand(10), 0.1 .* rand(10), marker="o", linestyle="none", label="first")
errorbar(rand(10), rand(10), 0.1 .* rand(10), marker="o", linestyle="none", label="second")

py"""
import matplotlib.pyplot as plt
h, l = plt.gca().get_legend_handles_labels()
plt.legend(reversed(h), reversed(l), frameon=True)
"""

image

Presumably a PyObject conversion issue? In python, h is a container

[<ErrorbarContainer object of 3 artists>,
 <ErrorbarContainer object of 3 artists>]

In Julia I find

2-element Array{Tuple{PyObject,Tuple{PyObject,PyObject},Tuple{PyObject}},1}:
 (PyObject <matplotlib.lines.Line2D object at 0x000000006FAB9F98>, (PyObject <matplotlib.lines.Line2D object at 0x000000006FAD6780>, PyObject <matplotlib.lines.Line2D object at 0x000000006FAD6EF0>), (PyObject <matplotlib.collections.LineCollection object at 0x000000006FAD6320>,))
 (PyObject <matplotlib.lines.Line2D object at 0x000000006FADF860>, (PyObject <matplotlib.lines.Line2D object at 0x000000006FADFB70>, PyObject <matplotlib.lines.Line2D object at 0x000000006FADFCC0>), (PyObject <matplotlib.collections.LineCollection object at 0x000000006FADF550>,))
stevengj commented 5 years ago

Yes, the issue is that since ErrorbarContainer is a subtype of tuple, PyCall is too aggressive about converting it to a Julia tuple. Yet another thing that will be eventually fixed by JuliaPy/PyCall.jl#617

For now, you can suppress the conversion with:

h,l = pycall(gca().get_legend_handles_labels, Tuple{Vector{PyObject}, Vector{String}})