Matplotlib figure stucked (grey window) - python

I have been having some trouble with matplotlib since I started using python. When I use the interactive mode, I have to run ipython --pylab --wthread to get plots (if I don't use the --wthread command, it does not plot anything). So far, it hasn't been a problem.
Now, I want to:
Enter a loop
Plot something. This plot is a big plot with two subplots.
AFTER, show a button pannel with easygui to let the user decide depending on what he sees on the plot
close the plot
repeat for each thing to plot in my list.
I am finding several difficulties right now with this:
1) if I try to run the script in an interactive way using the run script.py command, then it does not plot annything, but directly jumps to the button pannel thing. However, if I stop the script, the plots appear. I am pretty sure that what is happening is that the "run" command does not show plots untill the script is done with the for loop. But I need the plots to appear before the buttons.
2) After some tries, I found this code to work (for some mystical reason to me...). I have tried other ways, but removing any of the show() or draw() commands just make the script not to work
fig=plt.figure(figsize=(16,8))
plt.show()
ax1=fig.add_subplot(121)
ax2.things...
ax2=fig.add_subplot(122)
ax2.things
plt.draw()
plt.show()
plt.draw()
showthebuttonsthing...
Even if this is working, matplotlib seems not to get along well with loops, and after 5 seconds of not pressing any button an just waiting, my matplotlib window gets grey. This might sound as something stupid, but colors are important for the decision I want the user the make...
3) I can't find a way to make a python script show plots if I run the python script outside ipython...
I guess there is something really wrong with my matplotlib configuration, but can't find a way to make this work...can anyone give me an hand on this?
Thanks a lot in advance!

It looks like you've almost got it, but try doing things in this order instead:
fig = plt.figure(figsize=(16,8))
ax = [fig.add_subplot(121),fig.add_subplot(122)]
ax[0].things
ax[1].things
plt.show()
#show the button panel
However, the best method may be to integrate the plot into your GUI. I am not familiar with EasyGUI, but it seems to be based on tk. This example should help you with embedding the figure into a tk window.

Related

matplotlib.pyplot continue after calling show

for debugging purposes I would like to have my script show intermediate steps and not just the final plot when working with matplotlib. The problem is that after calling plt.show(), the axis is reset and drawing starts anew. Is there a way to call plt.show() and continue working with the same axis?
I am working inside PyCharm.
Code concept:
import statements
create part of plot
plt.show()
create next part of plot
plt.show() # Should show whatever was in the first plotted window plus what was added in the meantime
create final part of plot
plt.show() # Should show whatever was in the second plotted window plus what was added in the meantime
Thanks!
Edit:
System: Windows 10 running Python 3.7.1 and matplotlib 3.3.3
Your best bet to save the plot as it evolves and then compare the different saved figures.
#create part of plot
plt.savefig('first_part')
#create rest of plot
plt.savefig('second_part')

Matplotlib and Jupyter notebook multiple interactive plots

I am facing the following problem: I need two interactive plots to work at the same time on Jupyter, however they are interfering. When I rotate the cell of the first, the second plot stops being interactive and becomes "inline". Other times one of the plots looks like this:
The expected result was something like this
I imagine the problem is in the implementation I made. As you can see below, I use plt.something to put things in the figure (both for plot 1 and plot 2).
I'm using the %matplotlib notebook environment and tried to implement using fig1, ax1 = plt.subplots(). I would like to know if it is possible to do this type of implementation, where there is no conflict between plots? Maybe I am using matplotlib badly, so I would like some suggestions.

plt.show() not showing data instead holding it for next plot (spyder)

I have been using the same setup for quite some time now but suddenly I am no longer allowed to plot more than one graph in a program.
Usually I can plot multiple plots after each other and let the program run through it. It executes the next lines of code after closing the first window. However, recently the first plot is not shown but instead the data is added to the last plot.
I have included a sample code which used to give me two plots but now only one.
import matplotlib.pyplot as plt
import numpy as np
random_num = np.random.randint(0,5,10)
random_num_2 = np.random.randint(0,100,10)
plt.plot(random_num, 'ko')
plt.show()
plt.plot(random_num_2, 'g*')
plt.show()
The first image shows the output from my program. But I would like to have them separated into two plots like Figure 2 and 3 show.
Maybe I should add that I am using Python 3.6 with Spyder 3.2.4. The graphics option is set to display it in Qt5 even though I tried all settings and only 'Inline' shows me the results the way I want it.
Sorry if this is a very simple question. I have tried googling but I only come up with questions about my topic where the way mine works would be the solution not the problem.
#TheresaOtt. I would suggest you create a new figure instance (plt.figure()) for each plot and use only once at the end the plt.show() command.

Change plot window size in IPython notebook

I am using IPython Notebook to make some plots. For most of them I use
%matplotlib inline but for a few I change to %matplotlib notebook to be able to zoom in and stuff like that.
Now to the problem, the window displaying my plot (independent of what matplotlib setting I am using) suddenly became much smaller. Not the figure itself, just the window. I am really confused why this happened, if it was beacause I was switching between the two matplotlib settings or something else I made by mistake.
It is really annoying since I have to scroll in the window to se my whole figure unless I want to minimize it a lot. So if you have any idea how to make the plot window larger, please enlighten me.
Here you can se an example of what I mean by window: Small window. The 'window' according to me is where you can see "Figure 1" and the red button and ends where it cuts my plot.
Click to expand fully and show smaller. Double click to completely collapse it.

python and update figure in matplotlib

I've done a search on SO but haven't quite found the right "solution" to my problem. I am running a loop on some data that I wish to plot. At each step of the loop -- I plot the figure with plt.show(). However, since this is a blocking function, i am stuck until I manually close the window and then the loop continues and the next plot shows up.
What I would like to do is be able to bind a key press event to close the figure and continue the loop (rather than using the mouse to "X" out of the figure).
If this is not possible, I would like to set a timer to close the figure and continue the loop.
All my issues seem to deal with the fact that plt.show() is blocking everything else -- any way around this?
Some notes on my plots: They use the same axes, but contain a scatter plot, fill boxes, and annotations -- which are always changing.
Thanks!
Try using ion from matplotlib.pyplot:
import matplotlib.pyplot as pp
pp.ion()
fig = pp.figure()
More info about ion and interactive vs non-interactive usage here
Alternatively if you want to go with the button press approach assign a callback
def moveon(event):
pp.close()
cid = fig.canvas.mpl_connect('key_press_event', moveon)
pp.show()
An event timer is more tricky because the show command is blocking, so it would probably have to involve threading.

Categories

Resources