Search code examples
matplotlibjulialegendlegend-properties

Changing the shape of a scatter plot to a line in a Julia PyPlot legend


I'm trying to change the label of a scatter plot to show a line instead of a small dot, as the dot is difficult to see on screen, let alone in print:

Difficult to see dot.

Does the PyPlot library allow for this? The relevant parts of my code are as follows:

println("Importing (and possibly compiling JIT) a few relevant libraries...")
using LaTeXStrings,PyPlot;

println("Calculating a few points...")
samples = 10000;
T = 2 * pi;
x = collect(range(-pi,stop=pi,length=samples));
stepf = sign.(x);
N = 40;

"""
Sums of f
"""
fig, ax = subplots();
figname = "./Plots/filters.pdf";
ax[:scatter](x,stepf,label=latexstring("f(t)"),s=1);

ax[:axis]("off");
ax[:set_xlabel](latexstring("t"));
ax[:legend](loc="lower right");

fig[:savefig](figname);
close(fig)

EDIT

Based on below comments, this is going to boil down to finding a way to access Matplotlib's Line2D objects through Julia.


Solution

  • Here is the code by @ImportanceOfBeingErnest translated from Python to Julia PyCall. Definitely works!

    h,l  = ax[:get_legend_handles_labels]()
    z = PyPlot.plt[:Line2D]([],[], color="C0")
    h[end] = z
    ax[:legend](labels=l,  handles=h, loc="lower right");