Redraw in GKSTerm Asleep? (and Gray mysteries)

Dear Plots wizards—macos. julia 1.0.0. Plots.jl with standard gr() backend. Vanilla install. Please type this in:

$ julia

julia> using Plots

julia> plot( [-1,10], [-1,10], color=Gray(0.5), w=30, title="gray scale" )

julia> for i=0:10; vline!( [i], color=Gray(i/10.0), w=10 ) end

If you see what I see, it is the diagonal line, but no vertical lines. I have tried this with newly restarted julias, GKSTerm, etc. however, if I do a savefig(), the vertical lines do show up. similarly, if in the julia REPL, I type now (following the above)

julia> hline!( [2.0], color=Gray(0.3))

the vertical lines suddenly appear, too. it’s as if Plots forgot to tell GKSTerm to redraw until it is woken up by the hline. or maybe I am doing something wrong altogether? and/or can I force a redraw?

And, this was a debug run itself—I wanted to show off a gray spectrum. but why does the following only draw black vertical bars?

julia> vline!( 0:10, color=Gray.(reshape(collect(0.0:0.1:1.0),1,11)), w=20 )

And the incorrect vector argument (vline!( 0:10, color=Gray.(collect(0.0:0.1:1.0)), w=20 ) is also baffling.

Any advice would be appreciated.

This is a scoping issue. Change your code to:

p = plot( [-1,10], [-1,10], color=Gray(0.5), w=30, title="gray scale" )

for i=0:10
    vline!(p, [i], color=Gray(i/10.0), w=10 ) 


And it should work

Edit to add output:



Also, for support questions like these, you might want to go to our gitter channel:


Perhaps you could mention that gitter is the preferred support forum explicitly in the README.

There’s a “chat on gitter” badge? In my experience most users tend to look at the badges and ignore the text (people tended for a long time to say “Plots has no documentation” when it was linked twice in that short readme - but people seem to see the blue docs badge).

Gitter chats these days seems to be dominated by slackbots - would it be worth advertising the slack channel as well?

Just to clarify: the existence of a gitter channel is clear, the fact that it is the preferred support option is not.

and then there are people like myself, who have never used gitter, barely used slack, and who are astonished by the fact that a cacophony (without titles on conversations) could ever work. does it really?

(nilshg—could you please point me to where to read up on scoping issues in this context? I am completely unaware of this. I only knew of scoping issues in terms of variables and functions.)

Actually I think I misinterpreted this error - my initial thought was that the issue here was that the mutating vline! function couldn’t see the plot object defined outside the loop, but upon further reflection I think the issue is rather that the for loop doesn’t return the Plot object.

I think a simpler example is:

a = 0
for i = 1:10  
  a += 1  # or global a in 1.0

This snippet returns Nothing - if I want to actually see what happened to a after the loop, I’ll have to explicitly call a.

Similarly, your original code did work, you just didn’t see the result. I thought I had solved the problem by assigning the plot object to a variable p, and then passing this as an argument to vline!. It turns out that this isn’t actually necessary, the key thing is simply that with the assignment to p we have a way of referencing the plot object in a display() call after the loop. My example therefore would have also worked without passing p to vline!:

p = plot( [-1,10], [-1,10], color=Gray(0.5), w=30, title="gray scale" )

for i=0:10
    vline!([i], color=Gray(i/10.0), w=10 ) 


Apologies for the confusion!

Mille Grazie. This explanation helped. I had forgotten that the for was treated like one big statement by the REPL, and thus returned nothing and not the final vline.

any idea why neither of the following produces the same?

julia> vline!( 0:10, color=Gray.(reshape(collect(0.0:0.1:1.0),1,11)), w=20 )  ## only black bars

wouldn’t produce the right graph? the following works, but how do 11 bars use up 31 gray values:

vline!(p, 0:10, color=Gray.( range(0.0, stop=1.0, length=31 )), w=20 )

Can’t quite work it out but it appears color expects a column vector, while your reshape produces a row vector (1x11 Array). Slight improvement:

vline!( 0:10, color=[Gray(0.0 + i*0.1) for i ∈ 0:10], w=10 )  

or alternatively transposing your original:

vline!( 0:10, color=Gray.(reshape(collect(0.0:0.1:1.0),1,11))', w=10 )  

Now for some reason this seems to cycle through 4 shades of gray rather than plotting all 11, but it gets you a bit closer to where you want to be…

There appears to be something wrong with the implementation of vline!? This syntax should work vline!(0:10, line_z = 0:10, color = :grays) but for some reason it does not.