1

I want to create a plot with a couple of subplots using Plots.jl. Here's an example:

using Plots
gr()

p = plot(1:10, 1:10)
q = plot(1:10, 10:-1:1) 

plot(p, q)

enter image description here

That works exactly as expected! But say I want to use the same plot twice like so:

plot(p, p)

enter image description here

Hmmm. Only one plot. Perhaps I need to copy the plot first:

plot(p, copy(p))

but that give an error:

ERROR: MethodError: no method matching copy(::Plots.Plot{Plots.GRBackend})
Closest candidates are:
copy(::Expr) at expr.jl:36
copy(::Core.CodeInfo) at expr.jl:64
copy(::BitSet) at bitset.jl:46
...
Stacktrace:
1 top-level scope at REPL[216]:1

How can I plot the same subplot twice?

Dan
  • 11,370
  • 4
  • 43
  • 68

1 Answers1

1

You have the right idea, but try deepcopy() instead of copy(). deepcopy() often works on arbitrary objects which have no specific copy() method.

Bill
  • 5,600
  • 15
  • 27
  • Thanks, Bill — that works perfectly. Can you comment on why it won't plot it twice _without_ copying first? – Dan Jan 05 '21 at 02:59
  • I don't know, but suspect it is due to how subplots are stored--Plots uses dicts for storing most of its substructures. If stored in a Dict, you may not be able to put identical keys in the dict, and so the situation is forbidden. The people who maintain Plots may know. – Bill Jan 05 '21 at 05:48
  • It looks like it's potentially a bug: https://github.com/JuliaPlots/Plots.jl/issues/3123 – Dan Jan 05 '21 at 12:22