Show only certain items in legend Python Matplotlib

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 18:01:54

2K+ Views

In Python Matplotlib, you can control which items appear in the legend by using the plt.legend() method with a list of labels. This allows you to show only specific plot elements in the legend rather than all plotted data. Basic Syntax The plt.legend() method accepts a list of labels to display ? plt.legend(["label1", "label2"], loc=location, frameon=True/False) Parameters labels − List of strings to show in the legend loc − Location of the legend (0 for best location) frameon − Boolean flag to show/hide legend border Example Here's how to ... Read More

Blurring an image using the OpenCV function blur()

Prasad Naik
Updated on 25-Mar-2026 18:01:35

498 Views

In this tutorial, we will learn how to blur an image using the OpenCV blur() function. Blurring reduces image noise and detail by averaging pixel values in a neighborhood defined by a kernel. Algorithm Step 1: Import OpenCV and NumPy libraries Step 2: Load the input image Step 3: Define the kernel size for blurring Step 4: Apply the blur() function with image and kernel parameters Step 5: Display or save the blurred result Understanding the blur() Function The cv2.blur() function performs simple box filtering. It takes the average of all pixels in the ... Read More

Draw a filled polygon using the OpenCV function fillPoly()

Prasad Naik
Updated on 25-Mar-2026 18:01:13

8K+ Views

In this tutorial, we will learn how to draw a filled polygon using OpenCV's fillPoly() function. This function fills a polygon defined by a set of vertices with a specified color. Syntax cv2.fillPoly(image, pts, color) Parameters The fillPoly() function accepts the following parameters ? image ? The input image on which to draw the polygon pts ? Array of polygon vertices (points) color ? Fill color of the polygon in BGR format Algorithm Step 1: Import cv2 and numpy Step 2: Define the polygon vertices (endpoints) Step 3: ... Read More

Making matplotlib scatter plots from dataframes in Python's pandas

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 18:00:55

1K+ Views

Creating scatter plots from pandas DataFrames using matplotlib is a powerful way to visualize relationships between variables. We can use the DataFrame structure to organize our data and create colorful scatter plots with proper labeling. Steps to Create a Scatter Plot Import matplotlib and pandas libraries Create lists for your data variables (x-axis, y-axis, and colors) Build a pandas DataFrame from your data Create figure and axes objects using plt.subplots() Add axis labels using plt.xlabel() and plt.ylabel() Generate the scatter plot using ax.scatter() method Display the plot with plt.show() Example Here's how to create ... Read More

Manually add legend Items Python Matplotlib

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 18:00:36

23K+ Views

In Matplotlib, you can manually add legend items using the plt.legend() method. This allows you to create custom legends with specific labels, positions, and styling options like borders. Basic Syntax plt.legend(labels, loc=location, frameon=True/False) Parameters labels − List of strings for legend labels loc − Location of the legend (0 for best location) frameon − Boolean to show/hide the legend border Example Here's how to create a plot with manually added legend items ? import matplotlib.pyplot as plt # Set axis labels plt.xlabel("X-axis") plt.ylabel("Y-axis") # Plot two ... Read More

Make 3D plot interactive in Jupyter Notebook (Python & Matplotlib)

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 18:00:19

4K+ Views

Interactive 3D plots in Jupyter Notebook allow you to rotate, zoom, and pan your visualizations. Matplotlib provides built-in interactivity when using the %matplotlib notebook or %matplotlib widget magic commands. Setting Up Interactive Mode To enable interactivity, use the appropriate magic command at the beginning of your notebook ? %matplotlib notebook # or use %matplotlib widget for newer versions import matplotlib.pyplot as plt import numpy as np Creating an Interactive 3D Sphere Here's how to create an interactive 3D wireframe sphere ? import matplotlib.pyplot as plt import numpy as np # ... Read More

How do you plot a vertical line on a time series plot in Pandas?

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 18:00:00

4K+ Views

When working with time series data in Pandas, you often need to highlight specific dates or events by adding vertical lines to your plots. This can be achieved using matplotlib's axvline() method on the plot axes. Creating a Time Series DataFrame First, let's create a sample time series dataset with dates as the index ? import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with date range df = pd.DataFrame(index=pd.date_range("2019-07-01", "2019-07-31")) df["value"] = range(1, 32) # Sample values for each day print(df.head()) ... Read More

How to create a simple screen using Tkinter?

Prasad Naik
Updated on 25-Mar-2026 17:59:39

642 Views

Tkinter is Python's standard GUI library for creating desktop applications. We will create a simple screen using the Tkinter library to demonstrate the basic setup. Algorithm Step 1: Import tkinter. Step 2: Create an object of the tkinter class. Step 3: Display the screen using mainloop(). Example Code Here's how to create a basic Tkinter window ? import tkinter as tk # Create the main window window = tk.Tk() # Set window title window.title("Simple Tkinter Screen") # Set window size window.geometry("400x300") # Start the event loop window.mainloop() ... Read More

Python program to display various datetime formats

Prasad Naik
Updated on 25-Mar-2026 17:59:25

387 Views

The datetime module in Python provides powerful classes for manipulating dates and times. The strftime() method allows you to format dates and times in various ways using format codes. Basic Date Formatting Let's start with common date formats using format codes ? import datetime today = datetime.date.today() print("Day of the week:", today.strftime("%A")) print("Week number:", today.strftime("%W")) print("Day of the year:", today.strftime("%j")) Day of the week: Monday Week number: 47 Day of the year: 332 Various Date and Time Formats Here are more formatting options with complete date and time ... Read More

How to have logarithmic bins in a Python histogram?

SaiKrishna Tavva
Updated on 25-Mar-2026 17:59:04

6K+ Views

In Python, creating a logarithmic histogram involves using logarithmically spaced bins instead of linear ones. This is particularly useful when your data spans several orders of magnitude. We can achieve this using NumPy for generating logarithmic bins and matplotlib for plotting. Logarithmic bins are spaced exponentially rather than linearly, making them ideal for data that follows power-law distributions or spans wide ranges. Basic Example with Logarithmic Bins Let's create a simple histogram with logarithmic bins ? import matplotlib.pyplot as plt import numpy as np # Create sample data data = np.random.exponential(scale=2, size=1000) # ... Read More

Advertisements