# Plot series data by Plots

Hi commuinty,
Q1: Is there a way to use x, y and legend (as below) to plot data in series?
#y matrix: row corresponding to each x value, column for each series (marked by legend) of data

``````x=[2 4 6 8 10 12]#corresponding to each row
y= [10.6667   10.3333   8.0      14.6667;
10.0       7.33333  7.66667  17.0;
8.0       7.33333  8.33333  12.0;
4.33333   5.33333  7.33333  11.3333;
4.66667   4.0      4.0      10.6667;
4.33333   3.33333  4.0      12.0]

legend=["a" "b" "c" "d"] #each series-column
using Plots
#expected points distribution as like:
plot(y,legend) #but x and legend not accordingly specified

#trial1:
for i in 1:4 #for 4 series
if i==1
plot(x,y[:,i],label=legend[i])
else
plot!(x,y[:,i],label=legend[i])
end
end #not generating any graph

``````
``````#trial2
for i in 1:4 #for 4 series
if i==1
p=plot(x,y[:,i],label=legend[i])
else
push!(p,x,y[:,i])#,label=legend[i] #not work probably as not animation?
end
end
``````

Q2: Also, within a for loop, scatter(,label=legend[i]) tend to replicate the mark of i-th legend the number of row times. This post was discussing about the same problem, but the suggested commands to avoid replication seems not compiled, while the one for animation (in trial2) doesn’t work as well.

EDIT: @BLI 's solution is better

Is this what you want?

If so, make `x` a vector, and not a row matrix:

``````x=[2, 4, 6, 8, 10, 12]#corresponding to each row
``````
2 Likes

you need a package, I used Plots

``````using Plots
``````

type that, you’ll get an error

``````ERROR: ArgumentError: Package boogers not found in current path:
- Run `import Pkg; Pkg.add("Plots")` to install the Plots package.
``````

just highlight

``````import Pkg; Pkg.add("Plots")
``````

then renter ‘using Plots’, any you’ll be set

then to make a line graph just enter

``````plot(x,y,)
``````

I’d give my dataplot a name like “z”

``````z= dataframe( x=[2 ,4, 6, 8, 10, 12], y= [10.6667, 10.3333, 8.0, 14.6667, 10.0     ])

then enter
``````

plot(z.x, z.y)

Exactly, thank you！

This would be helpful for dataframes, thank you.

Sure thing, also sorry for overexplaing stuff like how to add packages, I have a tendency to jump in without completely reading things.