Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 514 of 855
Blurring an image using the OpenCV function blur()
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 MoreDraw a filled polygon using the OpenCV function fillPoly()
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 MoreMaking matplotlib scatter plots from dataframes in Python's pandas
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 MoreManually add legend Items Python Matplotlib
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 MoreMake 3D plot interactive in Jupyter Notebook (Python & Matplotlib)
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 MoreHow do you plot a vertical line on a time series plot in Pandas?
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 MoreHow to create a simple screen using Tkinter?
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 MorePython program to display various datetime formats
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 MoreHow to have logarithmic bins in a Python histogram?
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 MoreHow to use regular expressions (Regex) to filter valid emails in a Pandas series?
A regular expression is a sequence of characters that define a search pattern. In this program, we will use these regular expressions to filter valid and invalid emails in a Pandas series. We will define a Pandas series with different emails and check which email is valid using Python's re library for regex operations. Email Validation Regex Pattern The regex pattern for email validation contains several components ? ^: Anchor for the start of the string [a-z0-9]: Character class to match lowercase letters and digits [\._]?: Optional dot or underscore character @: Required @ symbol ...
Read More