Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79427

Re: Matplotlib: getting a figure to show without plt.show()

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subsequent': 0.05; 'modify': 0.07; 'none:': 0.07; 'subject:getting': 0.07; '[0,': 0.09; 'matplotlib': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'screen.': 0.09; 'subject:()': 0.09; 'python': 0.11; 'def': 0.12; 'changes': 0.15; '"control"': 0.16; 'buttons,': 0.16; 'clickable': 0.16; 'first:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:show': 0.16; 'termination.': 0.16; 'wrote:': 0.18; 'backend': 0.19; 'starts': 0.20; 'appears': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'question': 0.24; 'script': 0.25; 'second': 0.26; 'post': 0.26; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'thus': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'depth': 0.31; 'layer': 0.31; 'skip:q 20': 0.31; 'way?': 0.31; 'another': 0.32; 'style': 0.33; 'sense': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'false': 0.36; 'doing': 0.36; 'shows': 0.36; 'wrong': 0.37; 'list.': 0.37; 'button': 0.38; 'window': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'mailing': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'around.': 0.60; 'tell': 0.60; 'new': 0.61; 'first': 0.61; 'maximum': 0.63; 'different': 0.65; 'here': 0.66; 'hang': 0.67; 'below.': 0.71; 'special': 0.74; 'click': 0.77; 'ashamed': 0.84; 'plotted': 0.84; 'exceeded': 0.97
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Matplotlib: getting a figure to show without plt.show()
Date Wed, 22 Oct 2014 12:38:02 +0200
Organization None
References <caorpjFo1amU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9a9d.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15064.1413974302.18130.python-list@python.org> (permalink)
Lines 98
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1413974302 news.xs4all.nl 2896 [2001:888:2000:d::a6]:38029
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79427

Show key headers only | View raw


Peter Pearson wrote:

> I'm using Matplotlib to present a "control" window with clickable
> buttons, and to plot things in another window when you click buttons,
> in the style of the code below.  I'm ashamed of the stinky way I
> use "first" to call plt.show the first time data are plotted but then
> to call fig.canvas.draw for subsequent data plots.  

> def callback(event):
>     global n, first
>     fig = plt.figure(2)
>     fig.clear()
>     plt.plot([0,1],[0,n])
>     n += 1  # (Pretending something changes from one plot to the next.)
>     if first:
>         first = False
>         plt.show()
>     else:
>         fig.canvas.draw()

> Can someone tell me the right way?

I don't see what's wrong with doing something different the first time 
around.

> If I call plt.show every time, then after about 40 plots I get
> "RuntimeError: maximum recursion depth exceeded while calling a Python
> object", which makes sense because I'm getting one layer deeper in
> callbacks with every plot (plt.show doesn't return).  But if I call
> fig.canvas.draw every time, the window for the data plot never appears
> on my screen.

If your backend uses tkinter, then the problem may be that plt.show() starts 
a new mainloop. Over here this causes the script to hang on termination. 
Through try-and-error I came up with

import matplotlib.pyplot as plt
from matplotlib.widgets import Button

def callback(event):
    global n, fig2

    if fig2 is None:
        fig2 = plt.figure(2)

    fig2.clear()
    fig2.gca().plot([0, .5, 1], [0, 1/n, 1])
    fig2.show()

    n += 1 

n = 1
fig2 = None

plt.figure(1)

quit_button = Button(plt.axes([.1, .3, .4, .2]), "Quit")
quit_button.on_clicked(lambda x: plt.close("all"))

plot_button = Button(plt.axes([.1, .1, .4, .2]), "Plot")
plot_button.on_clicked(callback)

plt.show()

If you don't mind that the second window shows up immediately you can modify 
that to

import matplotlib.pyplot as plt
from matplotlib.widgets import Button

def callback(event):
    global n

    fig2.clear()
    fig2.gca().plot([0, .5, 1], [0, 1/n, 1])
    fig2.show()

    n += 1 

n = 1

plt.figure(1)

quit_button = Button(plt.axes([.1, .3, .4, .2]), "Quit")
quit_button.on_clicked(lambda x: plt.close("all"))

plot_button = Button(plt.axes([.1, .1, .4, .2]), "Plot")
plot_button.on_clicked(callback)

fig2 = plt.figure(2)

plt.show()

and thus avoid the special case. 

As I'm not an expert for matplotlib you might also post your question on the 
matplotlib mailing list.

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Matplotlib: getting a figure to show without plt.show() Peter Pearson <pkpearson@nowhere.invalid> - 2014-10-22 04:57 +0000
  Re: Matplotlib: getting a figure to show without plt.show() Peter Otten <__peter__@web.de> - 2014-10-22 12:38 +0200
    Re: Matplotlib: getting a figure to show without plt.show() Peter Pearson <pkpearson@nowhere.invalid> - 2014-10-22 16:38 +0000

csiph-web