# \[Plots\] Efficient scatter plot of a 2×N Array

**URL:** https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803
**Category:** Visualization
**Tags:** question, plotting, array
**Created:** [December 3, 2019, 2:10pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803 "2019-12-03T14:10:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)
#### Post date: [December 3, 2019, 2:10pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803/1 "2019-12-03T14:10:06Z")

</div>

When we want to plot a series of points we simply write:

```julia
points = rand(10000)
scatter(points)

```

this assumes a `y = rand(10000)` and ` x = 1:10000`. From the [Official documentation - Argument Passing Behavior](https://docs.julialang.org/en/v1/manual/functions/#Argument-Passing-Behavior-1)

> Julia function arguments follow a convention sometimes called “pass-by-sharing”, which means that values are not copied when they are passed to functions.

I know that in this case the variable `points` is not copied when passed to `scatter`.

Now consider having a 2×500 Array of points, i.e., 500 points in a 2D space. For this case, the scatter plot requires you to pass both x and y series separately. We usually do this by writing

```julia
scatter(points[1, :], points[2, :])

```

In this case `points[1, :]` and `points[2, :]` are copies of each row of `points` that are created before being passed to the function. A way to prevent this is

```julia
scatter(view(points, 1, :), view(points, 2, :))

```

but it is a bit verbose, is there a way to achieve the same behavior without calling view?

---

<div class="post-metadata">

### Author: ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)
#### Post date: [December 3, 2019, 2:59pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803/2 "2019-12-03T14:59:35Z")

</div>

> [@4aHxKzD](#):
>
> A way to prevent this is
> 
> ```julia
> scatter(view(points, 1, :), view(points, 2, :))
> 
> ```
> 
> but it is a bit verbose, is there a way to achieve the same behavior without calling view?

This still calls `view` but is already less verbose:

```julia
@views scatter(points[1,:], points[2,:])

```

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [December 3, 2019, 3:06pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803/3 "2019-12-03T15:06:12Z")

</div>

You can also write `scatter(eachrow(points)...)`, which is the same views but a bit shorter.

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [December 3, 2019, 3:07pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803/4 "2019-12-03T15:07:20Z")

</div>

Did you benchmark this?  
I doubt that the copying would take any noticeable amount of time with a 2x500 array, compared the plotting (and I suspect if the array is actually large enough for this to matter, you want a 2d histogram rather than a scatter plot).

---

<div class="post-metadata">

### Author: ![4aHxKzD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/4ahxkzd/32/10833_2.png) [@4aHxKzD](https://discourse.julialang.org/u/4aHxKzD)
#### Post date: [December 3, 2019, 4:33pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803/5 "2019-12-03T16:33:11Z")

</div>

I used 500 for the sake of example, but yes in practice it might not be relevant. I asked out of curiosity and still managed to learn one or two things from the answers.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 3, 2019, 5:26pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803/6 "2019-12-03T17:26:15Z")

</div>

It is very likely that plotting time will also scale at least linearly or worse (when a render has to figure out overlap in case of overplotting) with the number of points.

Also, [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Copying-data-is-not-always-bad-1) .

The key to efficient Julia code is to be aware of various performance pitfalls and optimization techniques, then benchmark and profile, and apply them _where needed_.

---

<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: [December 3, 2019, 11:24pm UTC](https://discourse.julialang.org/t/plots-efficient-scatter-plot-of-a-2xn-array/31803/7 "2019-12-03T23:24:58Z")

</div>

One of the first things Plots does is - copy the input. So copying manually first should introduce a constant but quite small time increase.
