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

**URL:** <https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [December 2, 2021, 8:21am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435 "2021-12-02T08:21:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 2, 2021, 8:21am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435/1 "2021-12-02T08:21:53Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 2, 2021, 8:30am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435/2 "2021-12-02T08:30:29Z")

</div>

Please make an MWE that reproduces your error:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

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

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 2, 2021, 9:02am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435/3 "2021-12-02T09:02:52Z")

</div>

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:

```julia
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`.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 2, 2021, 9:13am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435/4 "2021-12-02T09:13:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [December 2, 2021, 9:21am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435/5 "2021-12-02T09:21:21Z")

</div>

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

```julia
julia> using Plots, Makie

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

```

This prints

```julia
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.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [December 2, 2021, 9:26am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435/6 "2021-12-02T09:26:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 2, 2021, 9:41am UTC](https://discourse.julialang.org/t/plot-not-defined-and-or-twinx-not-defined/72435/7 "2021-12-02T09:41:36Z")

</div>

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.
