ERROR: UndefVarError: title! not defined

Hi all,
Trying to add titles and x/y labels to a plot. I copied code straight from Plots.jl and it fails with an error.

x = 10 .^ range(0, 4, length=100)
y = @. 1/(1+x)

plot(x, y, label="1/(1+x)")
plot!(xscale=:log10, yscale=:log10, minorgrid=true)
xlims!(1e+0, 1e+4)
ylims!(1e-5, 1e+0)
title!("Log-log plot")
xlabel!("x")
ylabel!("y")

ERROR: UndefVarError: title! not defined
Stacktrace:
[1] top-level scope
@ ~/Documents/ISU/Classes/AnS_562/2023/Part_A/Julia/04-Lab_03.jl:237

What am I doing wrong?

I can not reproduce the issue. After doing using Plots and copying your code, everything works. My system is:

julia> versioninfo()
Julia Version 1.10.0-DEV.514
Commit 8860d952959 (2023-02-06 20:14 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, skylake)
  Threads: 8 on 8 virtual cores
Environment:
  JULIA_IMAGE_THREADS = 1

(tmp.cU6pvZTIm1) pkg> st
Status `/tmp/tmp.cU6pvZTIm1/Project.toml`
  [91a5bcdd] Plots v1.38.5

Any chance you have imported two different plotting libraries? Plots and something else. In such a situation you usually get a warning about conflicting exported names and julia requires you to specify which library you want.

Yeah, so I went back to reopen Julia, it seemed to work from a fresh session.

When I tried to load Plots in a current session (already running other code) it gave me this warning when I loaded Plots, then I restarted completely and it worked.

julia> using Plots
WARNING: using Plots.xlabel! in module Main conflicts with an existing identifier.
WARNING: using Plots.title! in module Main conflicts with an existing identifier.
WARNING: using Plots.ylabel! in module Main conflicts with an existing identifier.

Okay, so what do I do in a current session where it will not work? I tried Plots.title!("My Title") and nothing, still doesn’t work and not sure what to do when it fails.

What else have you imported in the session in which you are getting this warning? Could you post a complete example of the code that is causing the problem and the output of ] status so we know what is installed in your environment?

# load packages
using RDatasets
using Pipe
using LinearAlgebra
using CSV
using DataFrames
using DataFramesMeta
using GLM
using MixedModels
using StatsBase
using FreqTables
using UnicodePlots
using Plots

Is currently what I had loaded. I already posted the code above that failed, literally any example or anything with any of those 3 functions at the end title!("My Title") for an example. I just had my session up, and ran that code and it failed… Seems like this should never happen. Especially when I use the Plots. notation with it right? Not sure why another package has title!() as a function… Here is my ] status.

(@v1.8) pkg> status
Status `~/.julia/environments/v1.8/Project.toml`
⌃ [69666777] Arrow v2.4.2
  [6e4b80f9] BenchmarkTools v1.3.2
  [336ed68f] CSV v0.10.9
  [324d7699] CategoricalArrays v0.10.7
⌃ [a93c6f00] DataFrames v1.4.4
  [1313f7d8] DataFramesMeta v0.13.0
  [31c24e10] Distributions v0.25.80
  [e30172f5] Documenter v0.27.24
⌃ [587475ba] Flux v0.13.11
  [da1fdf0e] FreqTables v0.4.5
  [38e38edf] GLM v1.8.1
  [7073ff75] IJulia v1.24.0
  [82e4d734] ImageIO v0.6.6
  [6218d12a] ImageMagick v1.2.2
⌃ [86fae568] ImageView v0.11.3
  [42fd0dbc] IterativeSolvers v0.9.2
  [945b72a4] MarketData v0.13.12
  [e1d29d7a] Missings v1.1.0
  [ff71e718] MixedModels v4.8.2
  [b98c9c47] Pipe v1.3.0
⌃ [91a5bcdd] Plots v1.38.3
  [1a8c2f83] Query v1.0.0
  [ce6b1742] RDatasets v0.7.7
  [860ef19b] StableRNGs v1.0.0
  [2913bbd2] StatsBase v0.33.21
  [f3b207a7] StatsPlots v0.15.4
  [bd369af6] Tables v1.10.0
  [9e3dc215] TimeSeries v0.23.1
  [9d95f2ec] TypedTables v1.4.1
⌃ [b8865327] UnicodePlots v3.3.4
⌃ [1986cc42] Unitful v1.12.2
  [37e2e46d] LinearAlgebra
  [10745b16] Statistics
  [4607b0f0] SuiteSparse
Info Packages marked with ⌃ have new versions available and may be upgradable.

I will update to see wha that does maybe.

Indeed, this should not happen, especially if you use the Plots.<name> expression. Let me try to run this myself to see what happens. It is a weird enough issue that I would be surprised if updating matters.

UnicodePlots, Plots, Plotly all have the same signature for plotting methods. I think its a good idea use import instead of using. This way julia will force you to use the namespace UnicodePlots.scatter to plot your graphics, and will make sure you will have not function names colisions.

1 Like

I did using UnicodePlots; using Plots and got a warning about what is going to happen when I tried title!:

> title!("Log-log plot")
WARNING: both Plots and UnicodePlots export "title!"; uses of it in module Main must be qualified
ERROR: UndefVarError: `title!` not defined

Then typing Plots.title!("Log-log plot") worked fine. If you try that minimal example, do you still have issues?

using UnicodePlots
using Plots

...
title!(...) # does not work
Plots.title!(...) # works fine

As jcbritobr mentioned, it is a good idea to not do using on two namespaces that have overlapping symbol names. But anyway, I am still confused why Plots.title! does not work for you.

2 Likes

So I restarted VS Code, and reloaded everything without UnicodePlots and it worked… Didn’t consider that they would cause that much issue. I guess others must not use them together like I do. I guess it is a bit odd for me to use both at the same time. Wonder what I did to not get Plots.title!() to work for me… I’ll keep trying here. Thanks for your help!

Thanks, I will try import. When I loaded without UnicodePlots, it worked just fine. I’ll just have to check on this before using both. Thanks for your help.

1 Like

for a bit of context: In python you are taught to never do from library import * because that mixes unrelated namespaces. In julia that advice is not given, because the whole point of multimethods is to expand other’s namespaces, not to create yet another namespace.

However, when you have two packages that do mostly the same tasks and have functions of the same names in their own separate namespaces, mixing them causes this type of issues. I now usually do not have problems with it, by being proactive when I see the aforementioned warning both LibA and LibB export some_symbol;

1 Like

Got it, thanks. I only learned a little Python, I gave up after I couldn’t install any packages with pip or conda… So glad Julia has a 1st class package manager.

Anyway, I will look into this loading vs using and make sure I check conflicts next time I have this happen. Just not always obvious to a newbie what is happening. This has probably happened to me before and I didn’t know it… Thanks!