# How to plot a line in 3D using Plots.jl?

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267>\
**Category:** Visualization\
**Tags:** plotting, plots, 3d, plot\
**Created:** [January 2, 2024, 4:35pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267 "2024-01-02T16:35:34Z")\
**Posts on this page:** 9\
**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:** [January 2, 2024, 4:35pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/1 "2024-01-02T16:35:34Z")

</div>

How to plot a line in a 3D axis?

Say, I want a parabola in the xy-plane (z=0)  
The straightforward options do not work;

This one results an empty canvas [-1, 1] x [-1, 1]:

```julia
using Plots
xs = -5:1:5
plot(xs, xs.^2, 0)

```

This one produces a 2D plot:

```julia
using Plots
xs = -5:1:5
plot(xs, xs.^2, [0])

```

For a different plane, the code throws an error:

```julia
using Plots
xs = -5:1:5
plot(xs, [0], xs.^2)

BoundsError: attempt to access 1-element Vector{Float64} at index [1:11]

```

(Why is it even trying to access 11 elements of a vector which has only 1 element?)

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [January 2, 2024, 4:45pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/2 "2024-01-02T16:45:46Z")

</div>

See it as a line in a 3D space (x(s),y(s),z(s)) where s allows to go along the curve, for example the index of the vectors x,y,z

```julia
using Plots
xs = -5:1:5
plot(xs, zeros(size(xs)),xs.^2)

```

---

<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:** [January 2, 2024, 4:54pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/3 "2024-01-02T16:54:12Z")

</div>

Thanks!

Also, does `xs, zeros(size(xs))` mean that `plot()` can only plot in 3D in “square” domains, where the dimensions of `xs` and `ys` match?

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [January 2, 2024, 4:58pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/4 "2024-01-02T16:58:34Z")

</div>

xs and ys must have the same number of elements in the discrete space (same vector size), but their range in the physical domain can be anything: the axes are clearly not those of a square

```julia
using Plots
xs = -5:1:5
plot(xs, xs.^2,zeros(size(xs)))

```

---

<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:** [January 2, 2024, 5:49pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/5 "2024-01-02T17:49:39Z")

</div>

Which means one cannot have an arbitrary domain and resolution, which is not obvious.

---

<div class="post-metadata">

**Author:** ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)\
**Post date:** [January 2, 2024, 6:44pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/6 "2024-01-02T18:44:44Z")

</div>

What do you mean by

> [@homocomputeris](#):
>
> Which means one cannot have an arbitrary domain and resolution, which is not obvious.

Resolution just depends on how many values you use and the arbitrary domain just depends on the values of the positions. So what it the exact problem you think there is ?

---

<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:** [January 8, 2024, 11:23am UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/7 "2024-01-08T11:23:00Z")

</div>

I mean that if I want to plot on [0, 1]x[0, 100] with the same 0.1 step (“resolution”), I won’t be able to, because the vectors will have different number of elements.

---

<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:** [January 8, 2024, 11:58am UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/8 "2024-01-08T11:58:41Z")

</div>

That is really confusing. A line in 3D is made of N points, each of them with 3 components. You can control the density of points along the line, but not along X and Y independently. Perhaps you meant a surface in 3D, where the sampling along X and Y can be defined independently?

---

<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:** [January 8, 2024, 12:15pm UTC](https://discourse.julialang.org/t/how-to-plot-a-line-in-3d-using-plots-jl/108267/9 "2024-01-08T12:15:54Z")

</div>

> [@rafael.guerra](#):
>
> A line in 3D is made of N points, each of them with 3 components

Yeah, this makes more sense.
