# Two legends in PyPlot

**URL:** <https://discourse.julialang.org/t/two-legends-in-pyplot/95946>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 12, 2023, 2:08am UTC](https://discourse.julialang.org/t/two-legends-in-pyplot/95946 "2023-03-12T02:08:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 12, 2023, 2:08am UTC](https://discourse.julialang.org/t/two-legends-in-pyplot/95946/1 "2023-03-12T02:08:51Z")

</div>

Hello everyone! How to add two legends in PyPlot (only). I tried to replicate some [Python examples](https://riptutorial.com/matplotlib/example/32429/multiple-legends-on-the-same-axes) but they use the add\_artist function which I don’t find in Julia.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 12, 2023, 7:46am UTC](https://discourse.julialang.org/t/two-legends-in-pyplot/95946/2 "2023-03-12T07:46:19Z")

</div>

Do you have an MWE where `add_artist` errors in Julia? IME it does work exactly the same as in Python, often with literally no code changes.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 13, 2023, 5:18pm UTC](https://discourse.julialang.org/t/two-legends-in-pyplot/95946/4 "2023-03-13T17:18:54Z")

</div>

```julia
using PyPlot

"""
When using PyPlot, I avoid using ax.plot, fig.add_subplot, etc.
It seems that here I could not.
MWE = message error warning?
IME = ?
"""

function test()
	
	# Generate data for plotting
	x = range(0, 2π, 100)
	y0 = sin.(x)
	y1 = sin.(x * 0.9) * 0.9

	# Find their maxima and minima and store
	maxes, mines = zeros(2,2), zeros(2,2)
	for (k,y) in enumerate([y0,y1])
		i = argmax(y); maxes[k,:] = [x[i], y[i]]
		i = argmin(y); mines[k,:] = [x[i], y[i]]
	end

	# plot y0, y1
	fig = figure()
	ax = fig.add_subplot(111)
	ax.plot(x, [y0 y1], label=["y0","y1"])
	
	# Plot maxima and minima
	maxline, = ax.plot(maxes[:,1], maxes[:,2], "r^")
	minline, = ax.plot(mines[:,1], mines[:,2], "ko")

	leg1 = ax.legend(loc=3) 
	leg2 = ax.legend([maxline,minline], ["max","min"], loc=1)
	ax.add_artist(leg1)

end

```

![Figure_1](https://global.discourse-cdn.com/julialang/original/3X/2/c/2c34dc77e3aad9137b81545e85f17aab5c785de9.png)

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 13, 2023, 5:35pm UTC](https://discourse.julialang.org/t/two-legends-in-pyplot/95946/5 "2023-03-13T17:35:09Z")

</div>

Thanks @aplavin.
