JuliaPy / PyPlot.jl

Plotting for Julia based on matplotlib.pyplot
https://github.com/JuliaPy/PyPlot.jl
MIT License
469 stars 85 forks source link

Window requires focus before functions return #563

Closed olsoni closed 1 year ago

olsoni commented 1 year ago

Hi, I've been happily using PyPlot for a while and loving it for the most part. One question I've had for a while and found nothing about online is how to make it plot (in interactive mode) without requiring the user to focus the window you are plotting into. Specifically, when I'm using PyPlot, I'll generally do something like this :

using PyPlot
fig = figure(1)
ax = subplot(1, 1, 1)
ax.plot(1:10, 1:10)

Executing those commands in the REPL (or in a script) leaves me with a blank plot until I either alt+tab to it's window or mouse over it. This feels like some window manager backend problem, but in case it was something I'm doing wrong or something internal to the package, I figured I'd bring it up here to see if anyone had insights.

Thanks!

mzaffalon commented 1 year ago

I have never had this problem under Windows or Ubuntu/Linux that I remember of and certainly not with Julia v1.8.5 and PyPlot v2.11.1 under Windows. Do you install PyPlot using the Julia manager Pkg?

olsoni commented 1 year ago

I'm wondering if it is something weird about my setup that's causing the issue. For my "standard use" setup, I have : Julia : 1.6.0 PyPlot : 2.11.0 matplotlib : 3.5.1 (which I believe was originally installed through apt)

Trying to test out a fresh install on my system (Ubuntu 20.04). I downloaded the 1.8.5 tarball and ran it fresh without any packages installed. I ran the following:

ENV["PYTHON"]=""   

import Pkg         

Pkg.add("PyCall")  
Pkg.build("PyCall")
Pkg.add("PyPlot")  

Confirmed that I had a fresh & clean install :

julia> Pkg.status()                             
Status `~/.julia/environments/v1.8/Project.toml`
  [438e738f] PyCall v1.95.1                     
  [d330b81b] PyPlot v2.11.1                     

Then I ran the following as the first plotting commands. As per the readme, matplotlib was installed through Conda.

using PyPlot                

f = figure(1)               
ax = PyPlot.subplot(1, 1, 1)

x = 0:0.01:10               
y = sin.(x)                 

ax.plot(x, y; label="sin")  
ax.legend()                 

This plot did not exhibit the freezing behavior I was experiencing. Plotting additional lines within the same figure also continued to work as expected. I'm going to update the installation & environment I use on a day to day basis and report back if it fixes the problem entirely or if I can still reproduce it somehow.

mzaffalon commented 1 year ago

Note that you do not need to add PyCall explicitly.

olsoni commented 1 year ago

Update on this : updating my installation and environment as described above seems to have resolved the issue for me for all of my day to day usage. It must have been something weird with how things were installed previously / maybe an interaction with an outdated version.

Thanks for the help!