# Change z order/index in Plots.jl

**URL:** <https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250>\
**Category:** Visualization\
**Tags:** question, plots\
**Created:** [September 25, 2020, 8:48am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250 "2020-09-25T08:48:28Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)\
**Post date:** [September 25, 2020, 8:48am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/1 "2020-09-25T08:48:29Z")

</div>

Is there a way to change the order in which plots are drawn in `Plots.jl`? (`z index` or `z order` in other suites)

For example

```julia
plot(rand(5)) // unfilled
plot!(2rand(5); fill=0) // filled

```

I would like to show unfilled above filled without changing the order of the plotting commands (one reason is that for consistency reasons I do not want to change the colors of the objects and want to use the standard colors)

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 25, 2020, 9:40am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/2 "2020-09-25T09:40:05Z")

</div>

Create the plots as

```julia
p1 = plot(rand(5))
p2 = plot(2rand(5), fill=0)

```

Compose them later in whatever order you want them to appear.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 25, 2020, 10:08am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/3 "2020-09-25T10:08:39Z")

</div>

Not exactly what you asked for, but you can do

```julia
julia> plot(2rand(5); fill=0, color=2)

julia> plot!(rand(5); color=1)

```

---

<div class="post-metadata">

**Author:** ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)\
**Post date:** [September 25, 2020, 10:17am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/4 "2020-09-25T10:17:44Z")

</div>

not sure how to compose plotting objects `p1` and `p2` onto single subplots later on, only to layouts…

---

<div class="post-metadata">

**Author:** ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)\
**Post date:** [September 25, 2020, 10:22am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/5 "2020-09-25T10:22:10Z")

</div>

thx, the color parameter almost does it

At the moment I have like ten series in one graph and it helps that each of them is at the same position for each graph. Also I’d like to not change to order of the legend. So it would be nice to not need to change the order of plotting commands but yeah, the color parameter almost does it

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 25, 2020, 11:50am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/6 "2020-09-25T11:50:59Z")

</div>

Looks like it’s not as simple as I seemed to remember. Reordering your `plot!` calls is probably simpler. Note that you don’t have to change the order the legend is displayed (it will be displayed in the order of the elements in the `labels` keyword argument).

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [April 26, 2021, 6:43pm UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/7 "2021-04-26T18:43:04Z")

</div>

There is an [“internal” argument](https://github.com/JuliaPlots/Plots.jl/blob/1ceb3a561f5e9f20471636865180d603d09753ed/src/args.jl#L525-L526)`:series_plotindex`, which is relevant, e.g.:  
[https://github.com/JuliaPlots/Plots.jl/blob/1ceb3a561f5e9f20471636865180d603d09753ed/src/backends/pyplot.jl#L449](https://github.com/JuliaPlots/Plots.jl/blob/1ceb3a561f5e9f20471636865180d603d09753ed/src/backends/pyplot.jl#L449)

but it doesn’t seem to affect e.g. the GR backend.

---

<div class="post-metadata">

**Author:** ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)\
**Post date:** [May 3, 2025, 9:06am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/8 "2025-05-03T09:06:33Z")

</div>

I was curious, is there a fix for this in 2025?

---

<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:** [May 3, 2025, 1:15pm UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/9 "2025-05-03T13:15:42Z")

</div>

I don’t know a one-liner in Plots.jl, but with a dozen lines of code we can reorder a past plot series while preserving its legends order:

 ![Plots_reorder_series_but_not_legends](https://global.discourse-cdn.com/julialang/original/3X/7/e/7ee8133053344dd10cd7c4615627aafd08788f0d.png)

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [May 5, 2025, 2:18pm UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/10 "2025-05-05T14:18:44Z")

</div>

If the legend order is not important you can use the `z_order` keyword:

```julia
julia> using Plots

julia> plot(1:5)

julia> plot!(2:6, fill=true, z_order=1)
```

---

<div class="post-metadata">

**Author:** ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)\
**Post date:** [May 6, 2025, 11:22am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/11 "2025-05-06T11:22:39Z")

</div>

Ah, nice! So that’s the solution, isn’t it?

---

<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:** [May 6, 2025, 11:54am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/12 "2025-05-06T11:54:11Z")

</div>

OP doesn’t want to change the order of the labels in the legend, which `z_order` does.

---

<div class="post-metadata">

**Author:** ![abavoil](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@abavoil](https://discourse.julialang.org/u/abavoil)\
**Post date:** [September 21, 2026, 9:50am UTC](https://discourse.julialang.org/t/change-z-order-index-in-plots-jl/47250/13 "2026-09-21T09:50:37Z")

</div>

In the hope that it will help others wandering here, here is my workaround :

```julia
using Plots

using Plots

y1 = rand(10)
y2 = 2rand(10)
y3 = 3rand(10)

y1_pars = (;c=:red, ms=5, label="data 1")
y2_pars = (; c=:orange, lw=5, label="data 2")
y3_pars = (; c=:blue, fill=true, label="data 3")

plot(y3; y3_pars..., label="")
plot!(y2; y2_pars..., label="")
scatter!(y1; y1_pars..., label="")

scatter!([]; y1_pars...)
plot!([]; y2_pars...)
plot!([]; y3_pars...)

```

When plotting the data (determines z-order), legend is untouched as `label` is being rewritten to the empty string, then I plot empty data but with legend entries to build the legend in the order I like, independently of z-order.

Edit: added a scatter plot because it also works!
