Unable to make multiple lines in Plots

I tried to plot 2 lines on the same graph

using Plots

f(x)=x^2
d(x)=2x

plot(f)
plot!(d)

But when I try to run this I get an error.

ERROR: UndefVarError: plot! not defined
Stacktrace:
 [1] top-level scope at REPL[673]:1

Do I have all the right packeges? Do I have the right syntax? including comas and semicolons?

Using Julia 1.5.0 the code works fine:

image

Perhaps try ] update. The “]” is important it lets you enter “pkg” mode. Then backspace to leave it afterwards. If it still does not work, try:

using Pkg
Pkg.rm("Plots")
Pkg.add("Plots")

Kind regards

1 Like

I might try that. A thing came up on VS Code, saying indexing packages, and now I can’t seem to add new packages, or start. packages. I can’t do one plot now. I just get an error.

julia> plot(f)
ERROR: error compiling _plot!: error compiling _display: could not load library "libGR.dll"
The specified module could not be found.

Stacktrace:
 [1] #plot#131(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(plot), ::Function) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\plot.jl:57
 [2] plot(::Function) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\plot.jl:51
 [3] top-level scope at REPL[3]:100:

You have to build GR, try:

using Pkg
Pkg.build("Plots")
Pkg.build("GR")

Kind regards

Awesome! It all seems to work now. Now how do I add a horizontal line?

when I add:

hline!(1)

I get :

hline!(1)
ERROR: BoundsError: attempt to access 0-element Array{Float64,1} at index [1]
Stacktrace:
 [1] getindex(::Array{Float64,1}, ::Int64) at .\array.jl:744
 [2] expand_extrema!(::Plots.Subplot{Plots.GRBackend}, ::RecipesPipeline.DefaultsDict) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\axes.jl:350
 [3] _expand_subplot_extrema(::Plots.Subplot{Plots.GRBackend}, ::RecipesPipeline.DefaultsDict, ::Symbol) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\pipeline.jl:361        
 [4] add_series!(::Plots.Plot{Plots.GRBackend}, ::RecipesPipeline.DefaultsDict) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\pipeline.jl:312
 [5] _process_seriesrecipe(::Plots.Plot{Plots.GRBackend}, ::RecipesPipeline.DefaultsDict) at C:\Users\Brett\.julia\packages\RecipesPipeline\tkFmN\src\series_recipe.jl:44
 [6] _process_seriesrecipe(::Plots.Plot{Plots.GRBackend}, ::RecipesPipeline.DefaultsDict) at C:\Users\Brett\.julia\packages\RecipesPipeline\tkFmN\src\series_recipe.jl:58
 [7] _process_seriesrecipes!(::Plots.Plot{Plots.GRBackend}, ::Array{Dict{Symbol,Any},1}) 
at C:\Users\Brett\.julia\packages\RecipesPipeline\tkFmN\src\series_recipe.jl:25
 [8] recipe_pipeline!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{Int64}) 
at C:\Users\Brett\.julia\packages\RecipesPipeline\tkFmN\src\RecipesPipeline.jl:96        
 [9] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{Int64}) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\plot.jl:167
 [10] #plot!#136(::Base.Iterators.Pairs{Symbol,Symbol,Tuple{Symbol},NamedTuple{(:seriestype,),Tuple{Symbol}}}, ::typeof(plot!), ::Plots.Plot{Plots.GRBackend}, ::Int64) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\plot.jl:158
 [11] (::RecipesBase.var"#kw##plot!")(::NamedTuple{(:seriestype,),Tuple{Symbol}}, ::typeof(plot!), ::Plots.Plot{Plots.GRBackend}, ::Int64) at .\none:0
 [12] #plot!#135(::Base.Iterators.Pairs{Symbol,Symbol,Tuple{Symbol},NamedTuple{(:seriestype,),Tuple{Symbol}}}, ::typeof(plot!), ::Int64) at C:\Users\Brett\.julia\packages\Plots\uCh2y\src\plot.jl:150
 [13] (::RecipesBase.var"#kw##plot!")(::NamedTuple{(:seriestype,),Tuple{Symbol}}, ::typeof(plot!), ::Int64) at .\none:0
 [14] #hline!#378 at C:\Users\Brett\.julia\packages\RecipesBase\aQmWx\src\RecipesBase.jl:403 [inlined]
 [15] hline!(::Int64) at C:\Users\Brett\.julia\packages\RecipesBase\aQmWx\src\RecipesBase.jl:403
 [16] top-level scope at REPL[14]:1

try hline!([1])

Yup, lol. I should have found that. I don’t see anything though about changing the spacing of x

using Plots
f(x)=x^2
plot(1:10,f)

I get jagged lines, I’d like to know if i can make the line smoother, or if I need to change the backend to do that.

just use a more fine-grained x, like plot(range(0, 1, length = 1000), f)