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
Programming Articles
Page 410 of 2547
Loading and displaying an image using the Pillow library
In this program, we will read or load an image using the Pillow library. The Pillow library provides the Image.open() method that takes the file path or filename as a string parameter. To display the image, we use the show() function which opens the image in the default image viewer. Installing Pillow First, install the Pillow library if you haven't already ? pip install Pillow Basic Image Loading and Display Here's how to load and display an image using Pillow ? from PIL import Image import io import base64 # ...
Read MoreHow to overplot a line on a scatter plot in Python?
Overplotting a line on a scatter plot combines scattered data points with a trend line or reference line. This technique is useful for showing relationships, trends, or theoretical models alongside actual data points. Basic Approach Create the scatter plot first using scatter(), then add the line using plot() on the same axes ? import matplotlib.pyplot as plt import numpy as np # Generate sample data x_data = np.linspace(0, 10, 20) y_data = 2 * x_data + 1 + np.random.normal(0, 2, 20) # Linear with noise # Create scatter plot plt.figure(figsize=(8, 6)) plt.scatter(x_data, y_data, ...
Read MoreBarchart with vertical labels in Python/Matplotlib
When creating bar charts in Python using Matplotlib, you can rotate axis labels to improve readability, especially when dealing with long label names. The xticks() function with the rotation parameter allows you to set labels vertically or at any angle. Basic Bar Chart with Vertical Labels Here's how to create a bar chart with vertical x-axis labels − from matplotlib import pyplot as plt bars_heights = [14, 8, 10] bars_label = ["A label", "B label", "C label"] plt.bar(range(len(bars_label)), bars_heights) plt.xticks(range(len(bars_label)), bars_label, rotation='vertical') plt.show() Different Rotation Angles You can specify custom angles ...
Read MoreDownsampling an image using OpenCV
In this program, we will down sample an image. Downsampling is decreasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming out of an image. We will use the pyrDown() function in the OpenCV library to complete this task. What is Image Downsampling? Image downsampling reduces the number of pixels in an image by decreasing its spatial resolution. OpenCV's pyrDown() function performs Gaussian pyramid downsampling, which smooths the image and reduces its size by half in each dimension. Algorithm Step 1: Read the image using cv2.imread() Step ...
Read MoreCalculating the mean of all pixels for each band in an image using the Pillow library
In this tutorial, we will calculate the mean pixel values for each color channel in an image using Python's Pillow library. RGB images have three channels (Red, Green, Blue), so we'll get a list of three mean values representing the average intensity of each color channel. Original Image Algorithm Step 1: Import the Image and ImageStat libraries from PIL Step 2: Open the target image file Step 3: Create an ImageStat.Stat object from the image Step 4: Use the mean property to get average pixel values for each channel Example Here's ...
Read MoreHow to save a figure remotely with pylab in Python?
Using the savefig() method of the pyplot package, we can save matplotlib figures to remote locations or specific directories by providing the complete file path. Setting Up the Backend When saving figures without displaying them, it's recommended to use the 'Agg' backend, which is designed for file output ? import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt Basic Figure Saving Create a simple plot and save it to the current directory ? import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt # Create a simple plot plt.plot([1, 2, 3, ...
Read MorePerforming white TopHat operation on images using OpenCV
In this tutorial, we will perform the TopHat operation on images using OpenCV. TopHat operation is a morphological transformation that extracts small elements and details from images by highlighting bright objects on dark backgrounds. We will use the cv2.morphologyEx() function with the cv2.MORPH_TOPHAT operation. What is TopHat Operation? TopHat (also called White TopHat) is defined as the difference between the input image and its opening. It highlights small bright details that are smaller than the structuring element ? TopHat = Original Image - Opening Original ...
Read MorePutting a newline in Matplotlib label with TeX in Python
When creating plots with Matplotlib, you may need to add newlines to axis labels for better formatting. This is easily achieved using the escape character in your label strings. Basic Example with Newlines in Labels Here's how to add newlines to both X and Y axis labels ? import matplotlib.pyplot as plt # Create simple data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Plot with newlines in labels plt.plot(x, y, 'b-', linewidth=2) plt.ylabel("Y-axis with newline") plt.xlabel("X-axis with newline") plt.title("Plot with Newlines in Labels") ...
Read MoreGenerating a movie from Python without saving individual frames to files
Creating animated movies in Python using matplotlib's FuncAnimation allows you to generate smooth animations without saving individual frames to disk. This approach is memory-efficient and perfect for real-time particle simulations. Key Concepts The animation works by repeatedly calling an update function that modifies particle positions and returns updated plot elements. FuncAnimation handles the timing and display automatically. Steps to Create the Animation Initialize particles with position, velocity, force, and size properties Create a matplotlib figure with specified dimensions Add axes with appropriate x and y limits Create initial scatter plot for particle positions Define an ...
Read MoreHow to prevent numbers being changed to exponential form in Python Matplotlib?
When plotting large numbers in Matplotlib, the axis labels often switch to scientific notation (exponential form) automatically. You can prevent this by using the ticklabel_format() with style='plain' parameter. Syntax plt.ticklabel_format(style='plain') The style='plain' parameter turns off scientific notation and displays numbers in their regular decimal format. Example Here's how to prevent exponential notation when plotting data ? import matplotlib.pyplot as plt # Plot data that would normally trigger scientific notation plt.plot([1, 2, 3, 4, 5], [11000, 12000, 13000, 14000, 15000]) # Prevent scientific notation on y-axis plt.ticklabel_format(style='plain') plt.title('Numbers in ...
Read More