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

Peter Pearson pkpearson at nowhere.invalid
Wed Oct 22 18:38:17 CEST 2014


On Wed, 22 Oct 2014 12:38:02 +0200, Peter Otten wrote:
> Peter Pearson wrote:
[snip]
>> 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?
[snip]

> 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()

Thank you for pointing out two useful things:

 * I like fig2.gca().plot better than plt.plot, since it makes it
   clear that I'm talking about fig2 and not some other part of the
   vast and inscrutable plt kingdom.

 * fig2.show is different from plt.show, and in fact this solves my
   problem exactly, since fig2.show returns immediately.

By the way, I will dispense with your "if fig2 is None" test.  In real
life, I say something like fig2 = plt.figure("Packet Sizes").  If a
figure named "Packet Sizes" doesn't exist, plt.figure creates it,
otherwise it returns a pointer to the existing figure -- *and* it titles
the figure's window with "Packet Sizes" rather than "2".

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

Sound advice.  Next time.  Thanks again.


-- 
To email me, substitute nowhere->runbox, invalid->com.



More information about the Python-list mailing list