Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8554509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:53:56+00:00 2026-06-11T14:53:56+00:00

I am a total matplotlib noob, at the moment making small changes to example

  • 0

I am a total matplotlib noob, at the moment making small changes to example programs and seeing what happens, and attempting to comprehend the extensive but not well ordered documentation.

I’m trying to add a graph display to an existing tk gui based python program. I am quite happy for the graph to float in a new window, I don’t need it embedded in the tk (yet, next week perhaps).

The existing program has a tk.Button that the user presses, which calls a function which retrieves some data and updates max/min/average in some labels.

I want to enhance the functionality so that as well as updating the labels, it also pops up a graph of the data. If the graph is already there, the next button press should replace the old trace with a new one and re auto scale the axes. If the user closes the graph window, the next button press should pop up a new one.

A fresh graph every press was easy, adding the if not self.fig test gets me down to one. It added persistent traces until I added plt.clf(). However if the user closes the graph window, the code below doesn’t recreate it. I think I need something like if graph visible, or if graph exists, rather than testing on self.fig. Or maybe I’ve missed the obvious way to do it, and there’s a much better way? Help much appreciated.

import Tkinter as tk
import os, sys
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt

class Plotter():
    def __init__(self):
        self.fig=None
        self.ax=None    

    def do_stuff(self):
        # make plausible data
        a=np.arange(0,3,0.1)
        b=npr.random(1)
        c=np.power(a,b)            

        if not self.fig:
            self.fig = plt.figure()
        else:
            plt.clf()

        self.ax = self.fig.add_subplot(111)
        self.ax.plot(a,c,'k--')
        self.ax.set_yticklabels([])
        self.ax.set_xticklabels([])

        plt.show()


p=Plotter()
root=tk.Tk()

rt = tk.Button(root, text='Capture', command=p.do_stuff)
rt.grid(row=0, column=0)

qt = tk.Button(root, text='quit', command=sys.exit)
qt.grid(row=1, column=0)

root.mainloop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:53:57+00:00Added an answer on June 11, 2026 at 2:53 pm

    Edited with new solution: Another solution is to use pyplot.fignum_exists to check if your figure is still around. So you could do:

    if self.fig is None or not pyplot.fignum_exists(self.fig.number):
        self.fig = pyplot.figure()
    # self.fig will now be active, either via an old still-open figure or a newly-created one
    

    Old solution for posterity:

    The thing is that if closing the figure won’t affect your self.fig, although it will render that Figure object useless. One option is to use pyplot.gcf() instead of pyplot.figure(), which stands for “get current figure”. This will return an existing figure if one is open, or it will make a new one if there is no active figure. So in your do_stuff method you can use pyplot.gcf() and it will return your old figure object or a new one, and you don’t need to worry about which. So just do:

    def do_stuff(self):
        # make plausible data
        fig = pyplot.gcf()
        fig.clf()
        self.ax = fig.add_subplot(111)
        # etc.
    

    This obviously has limitations. In particular, if any other part of your program also creates or uses matplotlib figures, then using gcf might cause weird results since the “current” figure might be some other figure that some other function created. For what you’re doing right now this seems like a workable solution, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Total noob question, but here. CSS .product__specfield_8_arrow { /*background-image:url(../../upload/orng_bg_arrow.png); background-repeat:no-repeat;*/ background-color:#fc0; width:50px !important; height:33px
Total noob question here, but not finding the answer via search. What is the
Total noob here with javascript. I'm trying to alter a function. This is the
Total noob here. This is my first c# attempt, its a console application that
Total noob to anything lower-level than Java, diving into iPhone audio, and realing from
Total newbie question, but I cant figure out what im doing wrong. I want
total n00b question here but I can't seem to find the answer searching around
Total newbie to XPath and Java here. What I'm attempting to do is something
Total noob, having trouble using a variable to loop through row. I'd like to
total newbie here. i was trying to replace a character in char * but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.