# Plot two legends (e.g. one for color and one for shape) with \`Plots.jl\`

**URL:** <https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [June 4, 2020, 4:28pm UTC](https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746 "2020-06-04T16:28:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [June 4, 2020, 4:28pm UTC](https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746/1 "2020-06-04T16:28:10Z")

</div>

How can I make a plot like this in `Plots.jl` (this was made with `matplotlib`):

![image](https://global.discourse-cdn.com/julialang/original/3X/1/4/14bf1c117375c95e80530cec12f49a994af4c557.png)

Basically four lines:

```julia
plot( data1, color=c1, linestyle=s1)
plot!(data2, color=c2, linestyle=s1)
plot!(data3, color=c1, linestyle=s2)
plot!(data4, color=c2, linestyle=s2)

```

And now I would like one legend that explains the linestyle and one that explains the color. Is that feasible directly through `Plots.jl`?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 4, 2020, 4:55pm UTC](https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746/2 "2020-06-04T16:55:34Z")

</div>

Not sure about the second legend (I guess in matplotlib this operates directly with legend objects?), but you could simply add two invisible lines to the plot to have the legend entries:

```julia
plot(rand(10), color=1, label = "random")
plot!(rand(10), color=2, label = "optimized")
plot!(rand(10), color=1, linestyle=:dot, label = "")
plot!(rand(10), color=2, linestyle=:dot, label = "")
plot!([1], [0], label = "validation", color = "black")
plot!([1], [0], linestyle = :dot, label = "cost", color = "black")

```

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [June 4, 2020, 5:33pm UTC](https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746/3 "2020-06-04T17:33:39Z")

</div>

Indeed, the matplotlib code operates on legend objects and series handles directly.

The invisible lines suggestion would work great, thanks!

---

<div class="post-metadata">

**Author:** ![Ivan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivan/32/208442_2.png) [@Ivan](https://discourse.julialang.org/u/Ivan)\
**Post date:** [April 12, 2024, 10:37am UTC](https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746/4 "2024-04-12T10:37:04Z")

</div>

The solution proposed here is quick and elegant, but in my workflow I will be plotting clouds of points (PCA kind of plot)

I care about how two different variables cluster together, so one variable determines the colour and a different variable determines markershape. I work with several datasets, so the amount of colours/shapes I use will vary a lot.

It would be nice to have the option to add a different legend for each of these things (linetype, markershape, markersize, linecolour, markercolour…)

Has there been any update on this since this post was first solved?

Also - is there any backend that is especially good for legend manipulation? I tend to use PlotlyJS because I love the interactivity of the plot, but I find I lack some wanted freedom with my legends in general (e.g. exact position, or number of columns in legend). Any comments appreciated! Thanks

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 12, 2024, 11:04am UTC](https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746/5 "2024-04-12T11:04:02Z")

</div>

If you need that level of control the answer is probably to just use Makie, where you can build any legend you want.

---

<div class="post-metadata">

**Author:** ![Ivan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivan/32/208442_2.png) [@Ivan](https://discourse.julialang.org/u/Ivan)\
**Post date:** [April 15, 2024, 7:04am UTC](https://discourse.julialang.org/t/plot-two-legends-e-g-one-for-color-and-one-for-shape-with-plots-jl/40746/6 "2024-04-15T07:04:01Z")

</div>

Thanks! I’ll have a look at it
