Julia PyPlot add legend

is there a simple way to add a legend to the plot with PyPlot? There are different solutions online but none worked.
I have this command:

myPlot = plot(x, y1, x, y2, linewidth=2.0)
savefig("./plot.png", dpi = 300, format = "png", transparent = false)

where y1 and y2 are two vectors sharing the same x values. But legend() does not provide **1. Automatic detection of elements to be shown in the legend**: it only produces a small square in the top right corner. likewise ax.legend or legend(loc="upper right").
Thank you

Aren’t you supposed to label your plots? Maybe split the plots into two and add individual labels as label="whatever you want" in each plot.

As explained in the docs, legend() only works if you pass label= to plot or explicitly call set_label. Otherwise, you need to pass the desired legends to legend, e.g.:

legend(["first line", "second line"])
1 Like

Thank you, legend(["first line", "second line"]) worked. And yes, I am using Pyplot. But I did not understand the syntax to place the label directly in the plot call:

julia> myPlot = plot(x, y1, x, y2, linewidth=2.0, label='Inline label')
ERROR: syntax: invalid character literal

If I do

myPlot = plot(x, y1, x, y2, linewidth=2.0, label=["first line", "second line"])

the label is gone

In Python, a string can be either double- or single-quoted ("foo" or 'foo'), for reasons I don’t comprehend. In Julia, strings must use double quotes. Single quotes are used for literals 'x' of the character type Char (Python doesn’t have a character type).