Switch between PyPlot and Plots

I found the following example in julialang regarding switching between backends:

#Pkg.add("Plots")
using Plots
plotly() # Choose the Plotly.jl backend for web interactivity
plot(rand(5,5),linewidth=2,title="My Plot")
#Pkg.add("PyPlot") # Install a different backend
pyplot() # Switch to using the PyPlot.jl backend
plot(rand(5,5),linewidth=2,title="My Plot2") # The same plotting command works``` 

But I have 2 problems when I run this code:

  1. I only see the second plot, and not the first one when I run the entire code. How can I see both plots at the same time?

  2. I’d like to have using PyPlot ion() in my juliarc to get the figures in another window. But when I have this I get the following error message:

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

UndefVarError: plot not defined

can I solve this somehow so I can still include that in my juliarc.jl?

Check out the Plots.jl Tutorial

https://juliaplots.github.io/tutorial/

display(plot(...)). It’s explained in the tutorial.

This is because both package export plot. I would suggest doing import PyPlot instead, and then do PyPlot.plot(...) and Plots.plot(...). Or you can just use the Plots.jl PyPlot backend.

2 Likes

If you would like to display the two plots in the same window, you should call the plot!() function, with the exclamation mark: this modifies the first plot.
If instead you would like to plot the second graph in a new window, you call plot(..., reuse=false).

1 Like

Thank you!

  1. You cannot show plots generated by different backends in the same window.
  2. @nako95 , could you edit the title to “Switch between pyplot and plotly backends for Plots”? You are not shifting between PyPlot and Plots - those two packages cannot be loaded simultaneously, as they both define plot.
    Also, loading PyPlot and calling the ion() function in the juliarc file will affect PyPlot behavior, but should not affect the behaviour of Plots. You can’t mix-and-match Plots and PyPlot functionality.