Plotting from vscode

I am trying a simple plot using vscode:

using PyPlot
x = linspace(0, 10, 200); y = sin.(x);
plot(x, y, "b-", linewidth=2)

And the result is simply PyObject <matplotlib.lines.Line2D object at 0x1249b77f0> and no graph!
I tried the same in Jupyter and the graph is showed. Why?

3 Likes

First I would try if you can display the current figure with: display(gcf())
If nothing appears you may have to install a Python GUI toolkit as it is outlined in the documentation:
https://github.com/JuliaPy/PyPlot.jl

4 Likes

Thank you. display(gcf()) shows the current figure.

3 Likes

If you’re going to be doing a lot of interactive plotting then it’s better to turn on the interactive plotting mode.

using PyPlot
ion()
...

After that display(gcf()) should not be necessary.

1 Like

I tried do plot with ion() and without display(gif())with no success.

https://github.com/JuliaEditorSupport/julia-vscode/issues/325

1 Like

Another possibility is to use PyPlot as the plotting backend with Plots.jl.
Then calling display(gcf()) is not necessary and VS Code shows the plot immediately.
Added to this is the benefit that you can switch easilly to one of the 15 different plotting backends.

using Plots
pyplot()
x = linspace(0,2*pi,1000); y = sin.(3 * x + 4 * cos.(2 * x));
plot(x, y, color="blue", w=2)
2 Likes

Yes it seems a better solution.
But I noticed that the syntax for the command plothas changed: color and winstead of “b-“ and line width.

Is it due to Plots package?

Plots.jl is a common interface to the various backends. Not all the keywords for all backends are supported though. A dedicated list of supported attributes is provided in the documentation to the package.
Some examples for PyPlot are here:
http://docs.juliaplots.org/latest/examples/pyplot/

1 Like

Thank you for showing me the link to the Plots site. It is very interesting and useful.

Hello fiolks.
I have this piece of code that works in REPL, but not in vscode.

using TestImages
img = testimage(‘cameraman’)
plot(img)

Why?, Any ideas?

now I see that I need to use ImageView.
I open another thread…