# Recipe to plot several line segments on the same graph

**URL:** <https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698>\
**Category:** General Usage\
**Tags:** documentation, plots, recipe\
**Created:** [February 15, 2023, 5:52pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698 "2023-02-15T17:52:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [February 15, 2023, 5:52pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/1 "2023-02-15T17:52:35Z")

</div>

A custom type contains a vector of matrices. Each two-column matrix defines a continuous line segment.

To plot this type, a `plot!()` function needs to be called as many times as there are line segments.  
I am not sure which macro from RecipesBase I need to use so as not to introduce the  
dependency on Plots.

Would that be a “user recipe” where the `@series` macro is called in a loop?

Thanks.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 15, 2023, 5:59pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/2 "2023-02-15T17:59:05Z")

</div>

```julia
julia> x = [rand(2,2) for _ in 1:10];

julia> using Plots

julia> plot([(m[1,:],m[2,:]) for m in x])

```

gives:

![image](https://global.discourse-cdn.com/julialang/original/3X/2/9/290df6585a912b6c60129041ecebbbb4b52f7952.png)

something like that can work, probably.

---

<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:** [February 15, 2023, 7:39pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/3 "2023-02-15T19:39:38Z")

</div>

An alternative would be to use:

```julia
plot(Tuple.(collect.(eachcol.(m.x))))

```

.  
MWE:

```julia
struct mytype
    x::Vector{Matrix{Float64}}
end

using RecipesBase

@recipe function f(m::mytype)
    @series begin
        Tuple.(collect.(eachcol.(m.x)))
    end
end

m = mytype([[sort(rand(5)) rand(5)] for _ in 1:10])

using Plots
plot(m)

```

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [February 15, 2023, 8:00pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/4 "2023-02-15T20:00:54Z")

</div>

RecipesBase is a lightweight Package without dependencies that allows to define custom visualizations with the [`@recipe`](https://docs.juliaplots.org/stable/RecipesBase/api/#RecipesBase.@recipe-Tuple%7BExpr%7D) macro.

Package developers and users can define recipes to tell [Plots.jl](https://github.com/JuliaPlots/Plots.jl) how to plot custom types without depending on it.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 15, 2023, 8:07pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/5 "2023-02-15T20:07:43Z")

</div>

I clearly misunderstood what you where asking, sorry. No, I’m not used to defining new plot recipes.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [February 15, 2023, 8:40pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/6 "2023-02-15T20:40:08Z")

</div>

Thank you. I didn’t realize that vector of tuples can `plot()` the disjointed line segments. Is it anywhere in Plots.jl documentation?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [February 15, 2023, 8:44pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/7 "2023-02-15T20:44:06Z")

</div>

Plots.jl documentation could be more detailed when it comes to `plot()` function, I think:

> There are lots of ways to pass in data, and lots of keyword arguments… just try it and it will likely work as expected.

Phew, thanks a bunch, API reference! That was really helpful.

---

<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:** [February 15, 2023, 9:11pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/8 "2023-02-15T21:11:53Z")

</div>

> [@Mikhail\_Kagalenko](#):
>
> I didn’t realize that vector of tuples can `plot()`

I think you raise a very good point. The Plots.jl documentation could be greatly improved in the [Input Data](https://docs.juliaplots.org/stable/input_data/#input-data) section.

I learn by poking around.  
Maybe the following simple example could be added to the docs:

```julia
x1, x2 = [1, 0], [2, 3] # vectors
y1, y2 = [4, 5], [6, 7] # vectors
m1, m2 = [x1 y1], [x2 y2] # matrices

plot([m1, m2]) # array of matrices -> 4 series, plots each matrix column, x assumed to be integer count
plot([[x1,y1], [x2,y2]]) # array of array of arrays -> 4 series, plots each individual array, x assumed to be integer count 
plot([(x1,y1), (x2,y2)]) # array of tuples of arrays -> 2 series, plots each tuple as new series

```

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [February 18, 2023, 1:04pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/9 "2023-02-18T13:04:41Z")

</div>

It probably should. At present, this method is undiscoverable except by reading code or trial and error. I tried a tuple of tuples and array of arrays, whereas the internet search only brought the suggestion to insert NaNs into a vector.

---

<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:** [February 26, 2023, 1:24pm UTC](https://discourse.julialang.org/t/recipe-to-plot-several-line-segments-on-the-same-graph/94698/10 "2023-02-26T13:24:42Z")

</div>

Fyi, suggested adding the last minimal example to Plots.jl documentation via [this PR](https://github.com/JuliaPlots/PlotDocs.jl/pull/341).
