- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Placing plot on Tkinter main window in Python
Oftentimes, we need to deal with plots in our Tkinter GUI-based application. To support the plots for the available data points, Python provides a Matplotlib package that can be imported into the application easily. In order to add a plot for the given data points, we have to install several other packages such as NumPy along with Matplotlib. NumPy is a Python library that helps to deal with scientific calculation in the Data.
Example
In this example, we will create data points for the car prices starting from (100000) with the units in the range of 1000 to 5000.
#Import the required Libraries from tkinter import * import numpy as np import matplotlib.pyplot as plt #Create an instance of Tkinter frame win= Tk() #Set the geometry of tkinter frame win.geometry("750x250") def graph(): car_prices=np.random.normal(100000, 5000, 1000) plt.hist(car_prices, 20) plt.show() #Create a button to show the plot Button(win, text= "Show Graph", command= graph).pack(pady=20) win.mainloop()
Output
If we will run the above code, it will display a window that contains a button "Show Graph".
When we click the button, it will display the graph on the screen.
- Related Articles
- How to put a Toplevel window in front of the main window in Tkinter?
- Creating a Frameless window in Python Tkinter
- Creating a Transparent window in Python Tkinter
- How to keep the window focus on the new Toplevel() window in Tkinter?
- Initialize a window as maximized in Tkinter Python
- Removing the TK icon on a Tkinter window
- How to center a window on the screen in Tkinter?
- How to close only the TopLevel window in Python Tkinter?
- How do I position the buttons on a Tkinter window?
- How to create a histogram with main title in the outer margin of plot window in base R?
- Forcing Tkinter window to stay on top of fullscreen in Windows 10?
- Window Resizer Control Panel in Tkinter
- How to put a Tkinter window on top of the others?
- Display the Host Name and IP Address on a Tkinter Window
- Default window color Tkinter and hex color codes in Tkinter

Advertisements