# How can I add a legend

**URL:** <https://discourse.julialang.org/t/how-can-i-add-a-legend/34667>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [February 15, 2020, 5:40am UTC](https://discourse.julialang.org/t/how-can-i-add-a-legend/34667 "2020-02-15T05:40:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bayerndd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bayerndd/32/12858_2.png) [@bayerndd](https://discourse.julialang.org/u/bayerndd)\
**Post date:** [February 15, 2020, 5:40am UTC](https://discourse.julialang.org/t/how-can-i-add-a-legend/34667/1 "2020-02-15T05:40:27Z")

</div>

Hi!

I plot a data, for example y = [1, 2, 3, 999, 34, 35, 34, 23, 345, 234, 23] and with classes=[1, 2, 1, 2, 3, 1, 2, 3, …]  
Then I write:  
plot(x, y, linecolor = classes)  
So I get a plot with one line with three different colours (like a piecewise line).

The question is how can label three different colours since when I use plot(label = [“1” “2” “3”]), the plot only shows “1” since there is actually one line. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 15, 2020, 11:26am UTC](https://discourse.julialang.org/t/how-can-i-add-a-legend/34667/2 "2020-02-15T11:26:30Z")

</div>

Welcome @bayerndd,

which plotting package are you using and please check

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![bayerndd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bayerndd/32/12858_2.png) [@bayerndd](https://discourse.julialang.org/u/bayerndd)\
**Post date:** [February 15, 2020, 1:39pm UTC](https://discourse.julialang.org/t/how-can-i-add-a-legend/34667/3 "2020-02-15T13:39:28Z")

</div>

Hi! I am using Plots only

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 16, 2020, 12:01am UTC](https://discourse.julialang.org/t/how-can-i-add-a-legend/34667/4 "2020-02-16T00:01:24Z")

</div>

I think, what you want is not possible in a more or less single plot command.  
The way to go is to decompose the single line into pieces for each color and plot them successively into the same plot.  
Here is how I solved it:

```julia
using Plots 

function decompose(classes,c)
	r1=falses(length(classes)+1)
	r2=falses(length(classes)+1)
	b=0
	for i in 1:length(classes)
		if classes[i]==c
			if b==0
				b=1
				r1[i]=true
				r1[i+1]=true
			else
				b=0
				r2[i]=true
				r2[i+1]=true
			end
		else
			i>1&&!r1[i-1] ? r1[i]=false : nothing
			i>1&&!r2[i-1] ? r2[i]=false : nothing
		end
	end
	(r1,r2)
end

x=1:11 #x-axis
y = [1, 2, 3, 999, 34, 35, 34, 23, 345, 234, 23] #y-axes 11 points
classes=[1, 2, 1, 2, 3, 1, 2, 3, 2, 3] # 11 points define 10 lines => 10 color entries

yp1=Array{Union{Missing, Int64}}(undef,length(y))
yp2=Array{Union{Missing, Int64}}(undef,length(y))

p=plot()
for c in sort(unique(classes))
	#global p #not needed
	yp1.=missing
	yp2.=missing
	yp1[decompose(classes,c)[1]]=y[decompose(classes,c)[1]]
	yp2[decompose(classes,c)[2]]=y[decompose(classes,c)[2]]
	plot!(p,x,yp1,linecolor=c,label=c)
	plot!(p,x,yp2,linecolor=c,label=nothing)
end
display(p)

```

![out](https://global.discourse-cdn.com/julialang/original/3X/9/b/9b7104a40c913b8f15391b3722110aba5dc86ece.png)
