black sheep 369
black sheep 369

Reputation: 624

How to plot multiple curves on same plot in julia?

I am using julia to build linear regression model from scratch. After having done all my mathematical calculations, I need to plot a linear regression graph

I have a scatter plot and linear fit (Linear line) plots separately ready, How do I combine them or use my linear fit plot on scatter plot?

Basically, how do I draw multiple plots on a single plot in Julia?

Note: Neither do I know python or R

x = [1,2,3,4,5]
y = [2,3,4,5,6]

plot1 = scatter(x,y)
plot2 = plot(x,y)  #line plot

#plot3 = plot1+plot2 (how?)  

Upvotes: 4

Views: 7036

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

Julia doesn't come with one built-in plotting package, so you need to choose one. Popular plotting packages are Plots, Gadfly, PyPlot, GR, PlotlyJS and others. You need to install them first, and with Plots you'll also need to install a "backend" package (e.g. one of the last three mentioned above).

With Plots, e.g., you'd do

using Plots; gr() # if GR is the plotting "backend" you've chosen
scatter(point_xs, point_ys) # the points
plot!(line_xs, line_ys)     # the line

The key here is the plot! command (as opposed to plot), which modifies an existing plot rather than creating a new one.

More simply you can do

scatter(x,y, smooth = true) # fits the trendline automatically

See also http://docs.juliaplots.org/latest/

(disclaimer: I'm associated with Plots - others may give you different advice)

Upvotes: 4

Related Questions