Plotting from vscode

plotting

#1

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?


#2

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


#3

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


#4

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.


#5

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


#6

#7

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)

#8

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?


#9

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/


#10

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