# Proper way to plot a linear combination

**URL:** https://discourse.julialang.org/t/proper-way-to-plot-a-linear-combination/94632
**Category:** General Usage
**Tags:** question, plotting, linearalgebra
**Created:** [February 14, 2023, 4:03pm UTC](https://discourse.julialang.org/t/proper-way-to-plot-a-linear-combination/94632 "2023-02-14T16:03:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 14, 2023, 4:03pm UTC](https://discourse.julialang.org/t/proper-way-to-plot-a-linear-combination/94632/1 "2023-02-14T16:03:25Z")

</div>

Hello, good afternoon.

Im trying to plot a linear combination in a 3d plane, subset of two vectors, with julia and plots and I’m failing with this tasks. Julia plots works a bit different than matplotlib.  
What is the correct way to do that?

```julia
let
	gr()
	# x-axis range
	xlim = [-4,4];
	a = [3, 5, 1];
	b = [0, 2, 2];

	xscalars = rand(xlim[1]:xlim[2], 100);
	@info xscalars;
	
	yscalars = rand(xlim[1]:xlim[2], 100);
	@info yscalars;
	
	c = [(a * xs) + (b .* ys) for (xs, ys) in zip(xscalars, yscalars)];
	@info c;
	
	scatter(a, b, c)
end

```

![image](https://global.discourse-cdn.com/julialang/original/3X/d/3/d393c346f889445e08e288a33aca55325316f751.png)

---

<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 14, 2023, 4:40pm UTC](https://discourse.julialang.org/t/proper-way-to-plot-a-linear-combination/94632/2 "2023-02-14T16:40:26Z")

</div>

I think you want:

```julia
scatter(Tuple.(c))

```

but this is the workaround to plot vectors of vectors as points with GR. With other backends that might be different.

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 14, 2023, 4:52pm UTC](https://discourse.julialang.org/t/proper-way-to-plot-a-linear-combination/94632/3 "2023-02-14T16:52:58Z")

</div>

Seems to work with ploty also.  
I’ll check the points to see if there are any issues.

![image](https://global.discourse-cdn.com/julialang/original/3X/4/5/45ab9c43426313b67ffb1a6f571c9273eba4f96d.png)

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 14, 2023, 5:01pm UTC](https://discourse.julialang.org/t/proper-way-to-plot-a-linear-combination/94632/4 "2023-02-14T17:01:17Z")

</div>

It worked. I’ll never find this solution for this plot. Thank you. An vector of vectorts would never do that in plots.

---

<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 14, 2023, 6:50pm UTC](https://discourse.julialang.org/t/proper-way-to-plot-a-linear-combination/94632/5 "2023-02-14T18:50:22Z")

</div>

Note that you could avoid the vector of vectors and generate x, y, z components from the start. See the simple method below using a generator expression:

```julia
using Plots; gr()
xlim = -4:4
a, b = [3, 5, 1], [0, 2, 2]
xs, ys = rand(xlim, 100), rand(xlim, 100)
x, y, z = (a*xs + b*ys for (a,b) in zip(a,b))
scatter(x, y, z)

```
