matplotlib buttons not displaying on the same window - python

I've made a simple GUI game with matplotlib, I can get it running well on PyCharm if I check "Run with Python Console" in config options.
The problem comes when I try to run it from terminal by doing
python MyGame.py
Indeed, when doing this, the matplotlib window only displays the plots, but not the widget buttons, and the latters only appear in a second window, when I close the first one...
Is there a way to get all the plots and the buttons in the same window, as it does in PyCharm with my config?
Here is an example of code having the unexpected behaviour
import matplotlib.pyplot as plt
import matplotlib.widgets as widgets
ax = plt.subplot()
ax.plot(.5,.6, marker='o', markersize=15, color='k')
plt.show()
buttonShape = plt.axes([.4, .2, .2, .1])
button = widgets.Button(buttonShape, "I am a button")
plt.show()
Thanks,
Vic

You got two plt.show() in your code.
The first one shows the figure you have constructed so far. Then you close it with the mouse. And then you start constructing a new one.
Just remove the first plt.show()

Related

Visualizing Python interactive plots outside of Jupyter

I am making some small tests in Jupyter. The user should see two plots, one on the left and one on the right. Than after clicking somewhere on the left plot something on the right plot should happen. For example here a click on the left plot will produce a red dot on the right plot in the same place:
%matplotlib notebook
def onclick(event):
ax.clear()
ax2.clear()
ax.set_xlim(0,10)
ax.set_ylim(0,10)
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
ax2.plot([event.xdata],[event.ydata],'o',color='red')
ax.figure.canvas.draw_idle()
ax2.figure.canvas.draw_idle()
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax.set_xlim(0,10)
ax.set_ylim(0,10)
ax2.set_xlim(0,10)
ax2.set_ylim(0,10)
cid = fig.canvas.mpl_connect('button_press_event',onclick)
This works, but the visualization in Jupyter is not the best. Is there a way to have this kind of interactive plots working outside of a Jupyter notebook ? Maybe in a window with varying dimensions ? As an example, how would one proceed with the example above ?
You can use plotly and export your plots to html file:
https://plotly.com/python/interactive-html-export/
I think I found a possible simple answer to my needs. Simply put the code in the question in a python script, remove the 'magic' command and insert at the end:
plt.show(block=True)
This way matplotlib will just use a different backend than notebook or inline in Jupiter and plot the figure in a separate window. This window is compatible with the interactive commands from matplotlib. I am not sure about widget compatibility though I will try and update the answer.
Update: I do not think ipywidgets can be used with plt.show() since these are not plotted in an axes, but there are some widgets within matplotlib (matplotlib.widgets) that can be used. Though, the resulting speed in my case is not satisfactory so I would avoid combining matplotlib widgets with matplotlib event catching.

How can I open plot viewer in vs code jupyter?

I am using python to plot in VS code Jupiter on my Mac. The plot is shown in the Interactive View, while I cannot do more thing like zoom or save in the view. I refer to Python Interactive window(see Plot Viewer)and it says:
Within the Python Interactive window, double-click any plot to open it in the viewer, or select the expand button on the upper left corner of the plot.
Expand Button
Plot Viewer
The expand button is supposed to be shown and open the Plot Viewer, while I don't have the button and there is nothing happen after I double-click the plot.
No Button Shows
My test code:
import seaborn as sns
import numpy as np
x = np.random.randn(100)
ax = sns.distplot(x)
Could you please help with my issue? I wonder whether I miss any extension. Thank you very much!
If still does not work please check the version of the Python and Jupyter extensions.
Default the renderer used in Interactive Python windows uses the PNG renderer.
You can change it to use the TkAgg renderer (external window)
Add %matplotlib to the script
%matplotlib
import seaborn as sns
import numpy as np
x = np.random.randn(100)
ax = sns.distplot(x)
You can use other plot renderers

How to record an interactive plot?

1.This becomes the interactive plot I am referring to. You will need to open the figure that appears at the bottom of the screen (if it does not display automatically) immediately after you start the program, in order to see the "animation". My question is: how do I record all of the updates in this interactive plot as a video? I do not want to record the screen. I also do not want to replace my current code fully with an animation object. Is there any way to create a video recording this interactive plot by supplementing the interactive plotting code rather than fully replacing it?
import matplotlib.pyplot as plt
plt.ion()
plt.figure()
for i in range(1000):
plt.axis((0,10,0,100))
plt.plot([1,2,3,4,5,6,7,8,9],[i,i,i,i,i,i,i,i,i],'ro')
plt.pause(.1)
plt.cla()
plt.clf()

Remove window title in matplotlib figure

Running the following code in a jupyter notebook (python3)
%matplotlib notebook
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(9,2.5))
The resulting figure is embedded in a window with huge borders that are wasting space. Up to now, I only found a function to set the window title of a figure "canvas.set_window_title". How can the window layout be changed? In particular, how can the window header be removed, the area where the window title and the "Stop Interaction" Button resides?
EDIT: I want to keep the interactive mode of matplotlib.
Screenshot of the output.
replace:
%matplotlib notebook
with
%matplotlib inline
Edit:
I see you want to keep interactive... I don't know if it is possible; inline is not interactive. You may have to settle for a floating window.

Matplotlib figure stucked (grey window)

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.

Categories

Resources