Plots.jl: Plot does not show up for unexpected reasons

Created on 23 Nov 2016  路  3Comments  路  Source: JuliaPlots/Plots.jl

Whether Plots.scatter (or anything else) displays the plot or not seems to depend on the code that is not related to plotting. Following code does not display the plot, but with a line of println commented out, it displays the plot correctly. I tested these on Julia version 0.5.0 and Plots version 0.10.1.

I also found that the version correctly working above also fails (does not show the plot) when run from the command line...

import Plots

function main()
    Plots.plotly()
    xs = collect(1:0.1:4)
    ys = xs .^ 2
    Plots.scatter(xs,ys)

    # When commented out, the graph shows up.
    println("Hey")
end

main()

Most helpful comment

Alternatively, you can call display(Plots.scatter(xs,ys)) to make it display your plot object. This would work in REPL.

All 3 comments

Plots are not displayed just because there is a plot command somewhere. That would be very annoying if you want to incrementally build a plot. Instead a plot is shown when the REPL tries to display the generated plot object. This is exactly the same as the REPL not printing the xs and ys in your function.

For example:

import Plots

function main()
    Plots.plotly()
    xs = collect(1:0.1:4)
    ys = xs .^ 2
    p = Plots.scatter(xs,ys)
    println("Hey")
    return p # Return the plot object
end

main()

Alternatively, you can call display(Plots.scatter(xs,ys)) to make it display your plot object. This would work in REPL.

Oh, it's my bad! Thanks, I figured out it is stated in the doc.

https://juliaplots.github.io/basics/

The graphic is not shown implicitly, only when "displayed".

https://juliaplots.github.io/output/

A Plot is only displayed when returned (a semicolon will suppress the return), or if explicitly displayed with display(plt), gui(), or by adding show = true to your plot command.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asinghvi17 picture asinghvi17  路  3Comments

crstnbr picture crstnbr  路  3Comments

apalugniok picture apalugniok  路  3Comments

lstagner picture lstagner  路  5Comments

Cody-G picture Cody-G  路  3Comments