# Easy way to plot a circle given as a vector of vectors (pointwise) as line plot?

**URL:** https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574
**Category:** Visualization
**Tags:** question, plotting, plots, plot, stack
**Created:** [October 4, 2023, 1:39pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574 "2023-10-04T13:39:02Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 4, 2023, 1:39pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/1 "2023-10-04T13:39:02Z")

</div>

Is there an easier way to plot a shape (in geometric sense, not `Plots.jl`’s) given as a vector of points (i.e. 2D-vectors)?

```julia
using Plots

# approximately a circle
vector_of_points = [[0.708441362115303, 1.413585198669223],
    [1.0185589103132255, 1.557842996204574],
    [1.3488474334102414, 1.4690134256250358],
    [1.5447662714075574, 1.188657163943744],
    [1.5146337337537332, 0.8479675075866541],
    [1.2725600518547597, 0.6063407865330036],
    [0.9318084528866316, 0.5768437043492579],
    [0.6518197911665652, 0.7732820990802726],
    [0.5636046347857182, 1.1037378826491369],
    [0.7084413621153031, 1.4135851986692225]]

# convert to matrix
matrix_of_point = stack(vector_of_points)

# disassemble the matrix to arguments and values
x = matrix_of_point[1, :]
y = matrix_of_point[2, :]

# almost a circle
plot(x, y)

```

---

<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: [October 4, 2023, 1:47pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/2 "2023-10-04T13:47:23Z")

</div>

sure, `plot(x, y, seriestype = :shape)` should do the trick.

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 4, 2023, 2:02pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/3 "2023-10-04T14:02:58Z")

</div>

Thanks! This function call creates a filled shape, which is not what I’m looking for.

Probably, I should reformulate my question:

1. given a vector of vectors,
2. how to plot a line plot
3. **without** the steps converting the vector of vectors into a matrix and
4. then disassembling (be it `stack`, comprehension, or whatever else) it into `x` and `y` arguments what `plot(x, y)` expects?

This kinda looks like a promise that such simple plotting should be possible:

> Part of the power of Plots lies is in the many combinations of allowed input data. You shouldn’t spend your time transforming and massaging your data into a specific format.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 4, 2023, 2:19pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/4 "2023-10-04T14:19:22Z")

</div>

Here is one method:

```julia
using MappedArrays

# vector_of_points defined as in OP

xvec = mappedarray(first, vector_of_points)
yvec = mappedarray(last, vector_of_points)

plot(xvec, yvec)

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [October 4, 2023, 3:54pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/5 "2023-10-04T15:54:39Z")

</div>

Are you looking for a Plots.jl-specific answer?

If you can use other plotting packages, check Makie.jl and the `linesegments` function. If you are doing geometric processing work, check Meshes.jl as it can plot all sorts of geometries using Makie.jl.

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 4, 2023, 10:21pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/6 "2023-10-04T22:21:38Z")

</div>

Thanks! This definitely saves from some boilerplate.

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 4, 2023, 10:22pm UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/7 "2023-10-04T22:22:28Z")

</div>

I chose `Plots.jl` because it’s the most popular one. I’ll have a look on Maki-e. Thanks!

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [October 5, 2023, 4:43am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/8 "2023-10-05T04:43:49Z")

</div>

`Vector{Vector}` is a relatively uncommon type in Julia for geometry because it’s relatively inefficient with all the pointers, so I’m not surprised you don’t find specific dispatches for it. I’m not sure if Makie, which was mentioned above, converts this signature to points. I’m only aware of `Tuple`s and `SVector`s and `Point`s which are bits types, so store contiguously in memory.

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [October 5, 2023, 5:38am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/9 "2023-10-05T05:38:27Z")

</div>

The simplest way to plot a line fron vect\_points:

```julia
using Plots
plot(getindex.(vect_points,1), getindex.(vect_points, 2)

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [October 5, 2023, 6:08am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/10 "2023-10-05T06:08:53Z")

</div>

or `first.(` and `last.(` if you know there are only two elements each

---

<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: [October 5, 2023, 7:11am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/11 "2023-10-05T07:11:56Z")

</div>

Yes, exactly. Plots tries to digest all sensible types, but the issue is that a vector of vectors is understood in Plots as multiple series (and that use is often very convenient). What kind of process or function does your vector of vectors derive from? It seems unusual, because a vector does not guarantee it will have a length of two, and pushing to any one of the vectors would break the inference as points.  
Making the vectors `static` would give the behaviour you want.

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 5, 2023, 9:01am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/12 "2023-10-05T09:01:32Z")

</div>

> [@mkborregaard](#):
>
> What kind of process or function does your vector of vectors derive from?

Applying an ODE solver to a list of initial conditions, each given as a vector:

```julia
function flow(ic)
    tspan = (0.0, 1.0)
    prob = ODEProblem(problem!, ic, tspan)
    sol = solve(prob, Euler(), dt=0.1)
    last = sol.u[end]
    return last
end

flow.(list_of_ics)

```

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 5, 2023, 9:17am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/13 "2023-10-05T09:17:25Z")

</div>

Unfortunately, Makie also fails in this task:

```julia
ERROR: `Makie.convert_arguments` for the plot type LineSegments{Tuple{Vector{Vector{Float64}}}} and its conversion trait PointBased() was unsuccessful.

The signature that could not be converted was:
::Vector{Vector{Float64}}

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [October 5, 2023, 9:22am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/14 "2023-10-05T09:22:24Z")

</div>

As @jules explained, you need to convert the dynamic vectors to static vectors or tuples before you plot. Try `Tuple.(vectors)` for example.

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 5, 2023, 9:28am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/15 "2023-10-05T09:28:54Z")

</div>

Thanks!

The recipe would be

```julia
Makie.convert_arguments(P::PointBased, m::Vector{Vector{Float64}}) = convert_arguments(P, Tuple.(m))

```

---

<div class="post-metadata">

### Author: ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)
#### Post date: [October 5, 2023, 9:30am UTC](https://discourse.julialang.org/t/easy-way-to-plot-a-circle-given-as-a-vector-of-vectors-pointwise-as-line-plot/104574/16 "2023-10-05T09:30:15Z")

</div>

Thanks for the explanation!
