"plot not defined" and / or "twinx not defined"

Julia v1.7.0
I faced two issues:

LoadError: UndefVarError: plot not defined
and
LoadError: UndefVarError: twinx not defined

It took a while, how to achieve the result I was looking for.
Below my simple example copied from the internet, if you have the same issues,
it might be necessary to put “Plots.” before the two commands “plot” and “twinx”,
just play around, by switch back and forth with “true” and “false” inside the if-clause:

using Measures, Plots; gr()
x12=randn(10)
y12=1000*randn(10)
lcx =:blue; lwx=2
lcy =:red; lwy=3
if true
    Plots.plot([x12[1]], lc=lcy, lw=lwy, label="y12", right_margin=15mm)
    Plots.plot!(x12, label="x12", lc=lcx, lw=lwx, legend=:topleft)
    Plots.plot!(twinx(), y12, lc=lcy, lw=lwy, label="")
else
    plot([x12[1]], lc=lcy, lw=lwy, label="y12", right_margin=15mm)
    plot!(x12, label="x12", lc=lcx, lw=lwx, legend=:topleft)
    plot!(Plots.twinx(), y12, lc=lcy, lw=lwy, label="")
end

P.S.:
As far as I understand, it should work without the additional “Plots.”,
maybe one reason on my installation is the fact that I need for the purpose
of data import the package “asammdf” and this is installed via PyCall
from the channel “source-forge”. The installation took a while and I was
warned at the end that I may face issues, because there are some incompatibilities
between packages with the same name originated from different
channels.

1 Like

Please make an MWE that reproduces your error:

In particular for this example it would be important to understand which packages are using and how you are loading them.

Thanks for completing the MWE. I cannot reproduce this - note that because your if/else block start with if true, the else branch is never hit, so you can boil the example down to:

using Measures, Plots
x12=randn(10)
y12=1000*randn(10)
lcx =:blue; lwx=2
lcy =:red; lwy=3
Plots.plot([x12[1]], lc=lcy, lw=lwy, label="y12", right_margin=15mm)
Plots.plot!(x12, label="x12", lc=lcx, lw=lwx, legend=:topleft)
Plots.plot!(twinx(), y12, lc=lcy, lw=lwy, label="")

I also removed gr(), as this is the default Plots backend so no need to activate it unless you’ve previously activated another backend.

Your error suggests a name clash, i.e. you might have done using SomeOtherPlottingPackage before in the same session? Although in that case Plots.plot should still work as you’re qualifying the name.

I would encourage you to re-run my amended MWE above in a fresh Julia session. If you’re still seeing errors please post the output of versioninfo() and ]st.

1 Like

Thanks a lot for the hint to restart Julia! :slight_smile:
It is quite strange for me. After a restart I was able to run the example without adding “Plots.”

You may have loaded another package that exports plot, like so:

julia> using Plots, Makie

julia> plot(1:3, 1:3)
ERROR: UndefVarError: plot not defined

This prints

WARNING: both Makie and Plots export "plot"; uses of it in module Main must be qualified

the first time it happens, but that’s easy to overlook.

1 Like

I have to admit I am really frustrated with setting up my plot with two Y-axis.
And what I did, I searched a lot and tried out different stuff.
Also “Makie” was on this list. But it was not present in this script.
Next time I run in some strange trouble, I will restart Julia first,
before I invest more time in my internet research for solutions …
Thanks a lot!

I think it’s likely that Sebastian is right, this is a not too uncommon thing.

That’s where the MWE comes in handy: when you ask for help somewhere it’s always good to condense the problem down to the fewest lines of code than generate the error, and then re-run that condensed version of the code in a fresh REPL session to ensure others will be able to reproduce it when they run it. Quite often I figure out the error myself in that process.