# Plots: Only one series as primary plot

**URL:** https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616
**Category:** Visualization
**Created:** [May 26, 2019, 7:37am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616 "2019-05-26T07:37:14Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 26, 2019, 7:37am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/1 "2019-05-26T07:37:14Z")

</div>

I frequently make figures that look like this

```julia
m = randn(100)
n = m .+ randn(100,50)
plot(m, lab="mean", l=(5,))
plot!(n; lab="realizations", l=(:black, 0.1))

```

![afig](https://global.discourse-cdn.com/julialang/original/3X/3/b/3b798e7e3ae92ca820786b00d41555b6d7c46c6a.png)  
Here, I would prefer to only have one of the “realizations” included in the legend. I created a small struct to solve this, so that the figure looks like this

```julia
plot(m, lab="mean", l=(5,))
plot!(n; lab="realizations", l=(:black, 0.1), primary=onlyone(true))

```

![afig2](https://global.discourse-cdn.com/julialang/original/3X/8/8/88223987755b05adfde05603368b6802d3255a44.png)  
The definition of the struct `onlyone` is below. Is this a common enough problem that people want something like this included in Plots? Either this approach or a keyword argument etc.?

```julia
struct onlyone <: AbstractMatrix{Bool}
    v::Bool
end
function Base.iterate(o::onlyone, state=1)
      state == 1 ? o.v : !o.v, state+1
end
Base.size(o::onlyone) = (1,typemax(Int))
Base.length(o::onlyone) = typemax(Int)
Base.getindex(o::onlyone,i) = i == 1 ? o.v : !o.v
Base.getindex(o::onlyone,i,j) = j == 1 ? o.v : !o.v

```

---

<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: [May 26, 2019, 8:49am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/2 "2019-05-26T08:49:32Z")

</div>

Could you not just plot `n[1]` with Label, and then `n[2:end]` with the same colour and no labels? It’s one extra call so not the prettiest thing ever but seems simpler and more portable?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 26, 2019, 8:58am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/3 "2019-05-26T08:58:28Z")

</div>

Yeah I’ve frequently done something like that, but I wanted to avoid that as it makes my plotting code close to twice as verbose and much harder to overlook.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 26, 2019, 9:10am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/4 "2019-05-26T09:10:41Z")

</div>

…that’s what I do (`plot!(n[:,1],label="realizations",...)`, etc.). Another alternative:

```nohighlight
LAB = hcat("realizations",fill("",1,49))
plot(m,label="mean",lw=5)
plot!(n,label=LAB,lc=:black,la=0.1)

```

…which saves a `plot!()` statement, but adds a variable.

Often, I find the result more (aesthetically) pleasing if add the primary plot lastly — so that it is not hidden under a cloud of realizations. To ensure that the primary plot legend appears first in the label list, that can – of course – be done by replotting the primary plot without label.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 26, 2019, 9:15am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/5 "2019-05-26T09:15:03Z")

</div>

I can understand your point when you make lots of plotting. I probably make simpler plots…  
To me, the two things I miss the most in Plots are:

1. The possibility to specify the number of columns in the label plate (matplotlib has good support for this), and
2. The possibility to add a plot title (i.e., a main title of the plot window in the case of more than one column in the plot layout).

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [May 26, 2019, 10:06am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/6 "2019-05-26T10:06:55Z")

</div>

That’s a nice little trick. My only concern is about whether people would realise this was in there.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 26, 2019, 10:13am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/7 "2019-05-26T10:13:29Z")

</div>

Yeah Plots certainly doesn’t make it impossible for me, it’s just that all available alternatives are awkward and verbose 😕

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 26, 2019, 10:16am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/8 "2019-05-26T10:16:20Z")

</div>

How does anyone realize any feature of a package? It’s mentioned in the documentation! E.g., if it was a keyword `firstprimary=true`, it would be listed under “attributes” in the documentation.

Still, I’m not sure if the approach I implemented is the best. It covers what I usually want to do, but perhaps there might be cases where one want’s to apply other settings to the first of a number of series. My version only supports `Bool`. Maybe one want’s to apply similar logic to non-booleans, even though I couldn’t immediately think of a use case.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [May 26, 2019, 10:31am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/9 "2019-05-26T10:31:43Z")

</div>

I think the inherent logic in Plots to do this would be to keep `push!`ing to the same series, with NaNs separating individual line segments. In this case that would involve putting everything into one vector instead of as now hcatting them. I can see how that might be awkward.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [May 26, 2019, 10:52am UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/10 "2019-05-26T10:52:40Z")

</div>

Maybe I’m not thinking things through, but is there any logic in specifying several series and a single label?

- Could one turn the whole thing around and say that **if a single label is provided in a plot command** , `plot()`/`plot!()` should only add a single label to the legend plate?
- On the other hand, if a row matrix of labels is provided, the standard behavior of cycling through the label list should be used?

If there are any exceptions to this, the user could make a for loop to get around the limitation.

This way, one can avoid new keywords, and get the behavior that is expected/wanted in (almost) all cases?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 26, 2019, 12:17pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/11 "2019-05-26T12:17:00Z")

</div>

To me the preferred interface would be to be able to say `uniquelabels=true`, or something like that, to avoid showing identically-looking labels.

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [May 26, 2019, 12:27pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/12 "2019-05-26T12:27:23Z")

</div>

Would make sense

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 26, 2019, 1:23pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/13 "2019-05-26T13:23:58Z")

</div>

> [@mkborregaard](#):
>
> keep `push!` ing to the same series

I got curious now, is this considered part of the API of Plots, i.e., is the user encouraged to manipulate the `series_list` manually? Or how do you push to a series?

Edit: I managed to answer my own question:  
[http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations](http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations)  
I didn’t know this interface existed, neat!

Edit2: After having tried it a bit, I’m not sure if the documented interface is working

```julia
p = plot(randn(10,2))

push!(p, [1,2]) # Working
x,y = p[1] # Not working
x,y = p[1][1] # Not working
p[1][1] = (1:10, randn(10)) # Not working
p[1] = (1:10, 1:10) # Working

```

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [May 26, 2019, 1:56pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/14 "2019-05-26T13:56:47Z")

</div>

Hmm, where is that interface documented?  
Normally you’re not expected to mess with that - but pushing to a series should be as easy as `push!(plt, 1, newelement)`

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [May 26, 2019, 1:57pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/15 "2019-05-26T13:57:36Z")

</div>

To get x I would normally `p[1][1][:x]`, i.e. “get x from the first series in the first subplot of the plot p”

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 26, 2019, 2:20pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/16 "2019-05-26T14:20:32Z")

</div>

> [@mkborregaard](#):
>
> where is that interface documented?

Here [http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations](http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations)

> Get series data: `x, y = plt[i]` . Set series data: `plt[i] = (x,y)` . Add to the series with `push!` / `append!` .

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [May 26, 2019, 2:37pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/17 "2019-05-26T14:37:38Z")

</div>

hahaha, so yeah that doesn’t work. The closest would be `x, y = Plots.getxy(p, 1)`

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 26, 2019, 3:31pm UTC](https://discourse.julialang.org/t/plots-only-one-series-as-primary-plot/24616/18 "2019-05-26T15:31:47Z")

</div>

> [@baggepinnen](#):
>
> p[1] = (1:10, 1:10) # Working

It seems that at least something along those lines is implemented as the above works. It doesn’t really matter if the interface uses `getindex/setindex!`, but some supported interface to push/pull data from a plot would be very useful.
