# How to plot the orientation of particles?

**URL:** <https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262>\
**Category:** Visualization\
**Tags:** question, plotting, plots\
**Created:** [February 8, 2023, 9:07am UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262 "2023-02-08T09:07:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jyoti\_Sharma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jyoti_sharma/32/31910_2.png) [@Jyoti\_Sharma](https://discourse.julialang.org/u/Jyoti_Sharma)\
**Post date:** [February 8, 2023, 9:07am UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/1 "2023-02-08T09:07:32Z")

</div>

Dear all,  
I am trying to plot the orientation of 2D Brownian particles. I have the x and y positions and the orientation of each particle. Given this, I have successfully plotted the x and y positions of the particles, and now I want to superimpose their respective orientation on top it.  
Any help will be highly appreciated.  
Many thanks for considering my request.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 8, 2023, 9:28am UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/2 "2023-02-08T09:28:53Z")

</div>

The Plots.jl package has a function `quiver` that should do what you want (plot little arrows).

> **help?\> quiver**
>
> ```julia
> search: quiver quiver!
> 
> quiver(x,y,quiver=(u,v))
> quiver!(x,y,quiver=(u,v))
> 
> Make a quiver (vector field) plot. The ith vector extends from (x[i],y[i]) to (x[i] + u[i], y[i] + v[i]).
> 
> Keyword arguments
> ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
> 
> • arrow::Union{Bool, Plots.Arrow}: Defines arrowheads that should be displayed at the end of path line segments (just before a NaN
> and the last non-NaN point). Used in quiverplot, streamplot, or similar. Aliases: (:arrows,).
> 
> Example
> ≡≡≡≡≡≡≡≡≡
> 
> julia> quiver([1,2,3],[3,2,1],quiver=([1,1,1],[1,2,3]))
> 
> ```

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 8, 2023, 9:46am UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/3 "2023-02-08T09:46:47Z")

</div>

It is also common to plot the orientation not with arrows (that can get messy quickly), but instead color the particles in different colors, according to their orientation, e.g. in this paper  
[https://www.researchgate.net/figure/Simulation-snapshot-of-active-Brownian-particles-in-an-elongated-box-employing-periodic\_fig1\_337966798](https://www.researchgate.net/figure/Simulation-snapshot-of-active-Brownian-particles-in-an-elongated-box-employing-periodic_fig1_337966798)

Here is another thread discussing how to achieve that: [How to color points in scatter plot by value?](https://discourse.julialang.org/t/how-to-color-points-in-scatter-plot-by-value/11740)

It boils down to something like

```julia
julia> using Plots
julia> N = 100
julia> xs = rand(N); ys = rand(N); φs = 2π .* rand(N);
julia> scatter(xs, ys, marker_z=φs, color=:rainbow)

```

---

<div class="post-metadata">

**Author:** ![Jyoti\_Sharma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jyoti_sharma/32/31910_2.png) [@Jyoti\_Sharma](https://discourse.julialang.org/u/Jyoti_Sharma)\
**Post date:** [February 8, 2023, 4:26pm UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/4 "2023-02-08T16:26:19Z")

</div>

Thank you for the reply.  
This method works in my code. However, I am not getting color gradient map along with the plot.  
What could be the reason?  
My code reads the following:  
scatter(graph\_wall[1][i][:,1], graph\_wall[1][i][:,2], marker\_z=graph\_wall[2][i,1], color=:rainbow, aspect\_ratio=:equal, lims=(-L/2, L/2),markersize=350R/L, marker =:circle,legend=false, title = “$Np particles, steps $Nt, ellipse a=L/2, b=L/3”)  
where i is for the time loop.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Jyoti\_Sharma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jyoti_sharma/32/31910_2.png) [@Jyoti\_Sharma](https://discourse.julialang.org/u/Jyoti_Sharma)\
**Post date:** [February 8, 2023, 4:28pm UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/5 "2023-02-08T16:28:39Z")

</div>

Thank you for the reply.  
In my case, it will probably not work.  
Quiver makes an arrow from one point to another point while orientation is not a point.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 8, 2023, 5:21pm UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/7 "2023-02-08T17:21:04Z")

</div>

> [@Jyoti\_Sharma](#):
>
> Quiver makes an arrow from one point to another point while orientation is not a point.

Arrows may also be given with _location, orientation, magnitude_

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 8, 2023, 6:34pm UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/8 "2023-02-08T18:34:36Z")

</div>

Just a small comment: enclosing the code in three backticks ( ` ) makes it a lot easier to read and copy/paste.

````julia
```julia

... your code ...

```

````

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 8, 2023, 6:36pm UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/9 "2023-02-08T18:36:33Z")

</div>

You’re using `legend=false` which turns off the colorbar on the side. In my snippet, the output looks like this

![orientation_color](https://global.discourse-cdn.com/julialang/original/3X/0/7/07f7fff826e36e883db845e31c52b5aa9078073b.png)

> [@Jyoti\_Sharma](#):
>
> Quiver makes an arrow from one point to another point while orientation is not a point.

It works, but it might be a bit tricky to get used to. Mathematically, an orientation is usually represented as a (unit) vector. But a point in the x-y-plane is also represented by a vector.

What `quiver` does is attaching small arrows to the x-y-positions you specify. The length and direction of the arrows is given by the u-v-coordinates. Here is the same example as with the colors, but using `quiver` instead:

```julia
quiver(xs, ys, quiver=(0.05 .* cos.(φs), 0.05 .* sin.(φs)))
scatter!(xs, ys)

```

![orientation_quiver](https://global.discourse-cdn.com/julialang/original/3X/8/f/8ffda2061dc659380b7d0eb1c59cc911d123d55b.png)

We first draw the arrows at the particles’ positions. The vector for the orientation is computed from the angle (which is very convenient in 2D) and they are shortened to a length of `0.05` so they don’t span the whole plot.  
On top of the arrows, we can also plot the particle markers.

---

<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 8, 2023, 7:57pm UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/10 "2023-02-08T19:57:31Z")

</div>

For good-looking arrows, finer control and fast display, you can use Plots’ shapes:

 ![Plots_gr_Oriented_Particles](https://global.discourse-cdn.com/julialang/original/3X/7/0/70bbcabd2fe6f652d18d92a40278228464b4ddc8.png)

> **Plots gr() code**
>
> ```julia
> using Plots; gr(legend=false)
> 
> function R(θ) # Rotation matrix
> sθ, cθ = sincosd(θ)
> return [cθ -sθ; sθ cθ]
> end
> 
> function Sarrow(θ)
> M = [Ref(R(u)) .* [[0, 0],[0,0.8],[0.15,0.6],[0,1], [-0.15,0.6], [0,0.8]] for u in θ]
> return [Shape(first.(m), last.(m)) for m in M]
> end
> 
> N = 100
> x, y, θ = rand(N), rand(N), rand(0:359, N)
> scatter(x, y, ms=2, c=:white, msc=:black, msw=0.2, ratio=1, lims=(0,1))
> scatter!(x, y, marker=Sarrow(θ), ms=10, c=:black, msc=:black, msw=0.2)
> 
> ```

---

<div class="post-metadata">

**Author:** ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)\
**Post date:** [February 8, 2023, 9:52pm UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/11 "2023-02-08T21:52:32Z")

</div>

It’s not exactly what you are after, but if you end up wanting to plot trajectories of atomic simulations there is some functionality in [Molly.jl](https://juliamolsim.github.io/Molly.jl/stable).

---

<div class="post-metadata">

**Author:** ![Jyoti\_Sharma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jyoti_sharma/32/31910_2.png) [@Jyoti\_Sharma](https://discourse.julialang.org/u/Jyoti_Sharma)\
**Post date:** [February 9, 2023, 8:47am UTC](https://discourse.julialang.org/t/how-to-plot-the-orientation-of-particles/94262/12 "2023-02-09T08:47:53Z")

</div>

Thank you for the reply.  
I made the [legend= true], showing the colour gradient bar.  
Works for my purpose now.  
Thanks again for the help.
