# Line labels in subplot impacted by other subplots

**URL:** <https://discourse.julialang.org/t/line-labels-in-subplot-impacted-by-other-subplots/102924>\
**Category:** Visualization\
**Tags:** plotting, plots, recipe\
**Created:** [August 17, 2023, 8:46pm UTC](https://discourse.julialang.org/t/line-labels-in-subplot-impacted-by-other-subplots/102924 "2023-08-17T20:46:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![markowkes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markowkes/32/43198_2.png) [@markowkes](https://discourse.julialang.org/u/markowkes)\
**Post date:** [August 17, 2023, 8:46pm UTC](https://discourse.julialang.org/t/line-labels-in-subplot-impacted-by-other-subplots/102924/1 "2023-08-17T20:46:30Z")

</div>

I have a plot recipe that creates a few subplots. However, the labels that are displayed in the legend are not in the correct order in some cases and I do not understand what is wrong.

For example, in this MWE, the function plot\_works() produces the correct result while plot\_backward() has the sin and cos labels in subplot 2 reversed. The only difference in the recipes is that plot\_works() plots both the sin and cos functions in subplot 1 and plot\_backwards() only plots the sin function.

```julia
userplot Plot_Works
@recipe function f(h::Plot_Works)

    # set up the subplots 
    layout --> (2,1)
    foreground_color_legend --> nothing
    legend --> :outertop

    # Create data for plots 
    t = 0:0.01:2*pi
    y1 = sin.(t)
    y2 = cos.(t)
    
    @series begin 
        subplot := 1
        label := ["sin" "cos"]
        xaxis := ("x")
        yaxis := ("y")
        x=t 
        y=[y1, y2]
        x,y
    end

    @series begin 
        subplot := 2
        label := ["sin" "cos"] ##### Order reversed on plot
        xaxis := ("x")
        yaxis := ("y")
        x=t 
        y=[y1, y2]
        x,y
    end
end

@userplot Plot_Backward
@recipe function f(h::Plot_Backward)

    # set up the subplots 
    layout --> (2,1)
    foreground_color_legend --> nothing
    legend --> :outertop

    # Create data for plots 
    t = 0:0.01:2*pi
    y1 = sin.(t)
    y2 = cos.(t)
    
    @series begin 
        subplot := 1
        label := "sin"
        xaxis := ("x")
        yaxis := ("y")
        x=t 
        y=y1
        x,y
    end

    @series begin 
        subplot := 2
        label --> ["sin" "cos"] ##### Order reversed on plot
        xaxis := ("x")
        yaxis := ("y")
        x=t 
        y=[y1, y2]
        x,y
    end
end

# Make plots
plot_works()
savefig("plot_works.png")

plot_backward()
savefig("plot_backward.png")

```

Output of `plot_works()` (note both figures have sin and cos in correct order)  
 ![plot_works](https://global.discourse-cdn.com/julialang/original/3X/7/d/7d5f242d43dfbfee44f0e54a2f763e19bb77a543.png)

Output of `plot_backward()` (note legend labels are reversed)  
 ![plot_backward](https://global.discourse-cdn.com/julialang/original/3X/f/7/f7df6b62bd52e044fe65d3e1491745b5243d7280.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 17, 2023, 9:58pm UTC](https://discourse.julialang.org/t/line-labels-in-subplot-impacted-by-other-subplots/102924/2 "2023-08-17T21:58:54Z")

</div>

It looks like this [open issue](https://github.com/JuliaPlots/Plots.jl/issues/4668#issue-1583845896) and this [one](https://github.com/JuliaPlots/Plots.jl/issues/4108#issue-1142555268).

A workaround is given at the end: to construct only one series per `@series` block.
