How to get all the legends from a plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 00:19:29

2K+ Views

To get all the legends from a plot in matplotlib, we can use the get_children() method to get all the properties of an axis, then iterate through them. If an item is an instance of a Legend, we can extract the legend texts. Steps Set the figure size and adjust the padding between subplots. Create x data points using numpy. Create a figure and a set of subplots. Plot sin(x) and cos(x) using plot() method with different labels and colors. Get the ... Read More

How to create a Boxplot with Matplotlib?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 00:19:12

285 Views

A boxplot (also called a box-and-whisker plot) is a statistical visualization that displays the distribution of data through quartiles. Matplotlib provides simple methods to create boxplots for data analysis. Basic Boxplot with Single Dataset Let's start with a simple example using matplotlib's built-in boxplot function ? import matplotlib.pyplot as plt import numpy as np # Generate sample data data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20] # Create figure and plot boxplot plt.figure(figsize=(8, 6)) plt.boxplot(data) plt.title('Simple Boxplot') plt.ylabel('Values') plt.show() Multiple Boxplots with Custom Labels Here's ... Read More

How to show a bar and line graph on the same plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 00:18:46

22K+ Views

To show a bar and line graph on the same plot in Matplotlib, you can combine both plot types using the same axes. This technique is useful for displaying two different data perspectives or comparing trends with categorical data. Basic Approach The key steps are ? Create a DataFrame with your data Create a figure and axes using subplots() Plot both bar and line graphs on the same axes Display the combined plot Example Here's how to create a combined bar and line plot ? import pandas as pd import matplotlib.pyplot ... Read More

How to plot blurred points in Matplotlib?

Rishikesh Kumar Rishi
Updated on 26-Mar-2026 00:18:28

1K+ Views

Matplotlib allows you to create blurred points by combining Gaussian filters and image transformations. This technique is useful for creating artistic effects, heatmap-like visualizations, or emphasizing data points with varying importance. Basic Approach The process involves creating a marker, applying a Gaussian filter for blur effect, and positioning it on the plot using BboxImage. Example Here's how to create blurred points with varying blur intensities ? import matplotlib.pyplot as plt from scipy import ndimage from matplotlib.image import BboxImage from matplotlib.transforms import Bbox, TransformedBbox import numpy as np plt.rcParams["figure.figsize"] = [8, 6] plt.rcParams["figure.autolayout"] = ... Read More

How to create a Tkinter error message box?

Dev Prakash Sharma
Updated on 26-Mar-2026 00:17:58

21K+ Views

The Tkinter library has many built-in functions and methods which can be used to implement the functional part of an application. We can use messagebox module in Tkinter to create various popup dialog boxes. The messagebox property has different types of built-in popup windows that the users can use in their applications. If you need to display the error messagebox in your application, you can use showerror("Title", "Error Message") method. This method can be invoked with the messagebox itself. Syntax messagebox.showerror(title, message, **options) Parameters title − The title of the error dialog ... Read More

How to reconfigure Tkinter canvas items?

Dev Prakash Sharma
Updated on 26-Mar-2026 00:17:42

11K+ Views

Using the Canvas widget, we can create text, images, graphics, and visual content. When you need to modify Canvas items dynamically, Tkinter provides the itemconfig() method to configure properties and attributes of Canvas items after creation. Syntax canvas.itemconfig(item_id, **options) Parameters: item_id − The ID of the canvas item to modify **options − Configuration options like fill, width, outline, etc. Example: Reconfiguring Line Properties Here's how to change the color and width of a line using itemconfig() − # Import the required libraries from tkinter import * # Create ... Read More

How to set justification on Tkinter Text box?

Dev Prakash Sharma
Updated on 26-Mar-2026 00:17:21

4K+ Views

The Text widget supports multiline user input from the user. We can configure the Text widget properties such as its font properties, text color, background, etc., by using the configure() method. To set the justification of our text inside the Text widget, we can use tag_add() and tag_configure() methods. We will specify the value of "justify" as CENTER, LEFT, or RIGHT. Basic Text Justification Here's how to create a Text widget with center justification ? # Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win ... Read More

How to get the widget name in the event in Tkinter?

Dev Prakash Sharma
Updated on 26-Mar-2026 00:17:01

3K+ Views

Tkinter is a Python library used to create GUI-based applications. When working with multiple widgets and event handling, you often need to identify which specific widget triggered an event. This can be achieved using the event.widget object and its properties. Getting Widget Name Using event.widget The most direct way to get a widget's name in an event is by accessing event.widget in your event handler function. Here's how to get different types of widget identification ? import tkinter as tk def on_button_click(event): # Get the widget that triggered the event ... Read More

How to delete lines from a Python tkinter canvas?

Dev Prakash Sharma
Updated on 26-Mar-2026 00:16:37

4K+ Views

The Canvas widget has many use-cases in GUI application development. We can use a Canvas widget to draw shapes, creating graphics, images, and many other things. To draw a line in Canvas, we can use create_line(x, y, x1, y1, **options) method. In Tkinter, we can draw two types of lines − simple and dashed. If you want your application to delete the created lines, then you can use the delete() method. Using delete() Method The delete() method removes canvas objects by their ID. When you create a line using create_line(), it returns an ID that you can ... Read More

How to insert the current time in an Entry Widget in Tkinter?

Dev Prakash Sharma
Updated on 26-Mar-2026 00:16:22

3K+ Views

To work with the date and time module, Python provides the datetime package. Using the datetime package, we can display the current time, manipulate datetime objects and use them to add functionality in Tkinter applications. To display the current time in an Entry widget, we first import the datetime module and create an instance of its object. The Entry widget can then display this formatted time information ? Basic Example − Current Date Here's how to display the current date in an Entry widget ? # Import the required libraries from tkinter import * import ... Read More

Advertisements