0

I referred to the following useful tutorial video:

https://www.youtube.com/watch?v=8exB6Ly3nx0

And I used his codes (see 6:28); however, the graph was plotted under the cell instead of the independent window.

How to overcome this? Thanks!

enter image description here

1
  • 2
    Did you try running %matplotlib at the beginning? Commented Dec 17, 2021 at 10:41

2 Answers 2

1

Here is a solution that does what you want. Jupyter-notebooks don't use the tk backend for plotting by default so you have to explicitly instruct matplotlib to use it.

That can either be done as shown below, or using the notebook magic %matplotlib as metioned by @acw1668 in the comments.

I personally find that not practical, and there are other solution to use interactive plots inside a notebook, as e.g. jupyter-lab widgets that I would rather refer you to.

from tkinter import *
from PIL import ImageTk, Image
import numpy as np

import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')

root = Tk()
root.title("dsafsdff")
root.geometry("400x200")

def graph():
    f, a = plt.subplots(1, 1)
    house_prices = np.random.normal(0., 1, 1000)
    a.hist(house_prices)
    plt.show()

my_button = Button(root, text="afdgfsdgf", command=graph)
my_button.pack()
root.mainloop()   
Sign up to request clarification or add additional context in comments.

Comments

0

Use %matplotlib tk in the first line to render graphics in an independent window managed by tkinter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.