# Proper way to plot a linear combination

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?

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

xscalars = rand(xlim:xlim, 100);
@info xscalars;

yscalars = rand(xlim:xlim, 100);
@info yscalars;

c = [(a * xs) + (b .* ys) for (xs, ys) in zip(xscalars, yscalars)];
@info c;

scatter(a, b, c)
end
`````` I think you want:

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

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

2 Likes

Seems to work with ploty also.
I’ll check the points to see if there are any issues. It worked. I’ll never find this solution for this plot. Thank you. An vector of vectorts would never do that in plots.

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:

``````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)
``````
1 Like