Server Side Programming Articles

Page 407 of 2109

Putting a newline in Matplotlib label with TeX in Python

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 5K+ Views

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 More

Generating a movie from Python without saving individual frames to files

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 454 Views

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 More

How to prevent numbers being changed to exponential form in Python Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 1K+ Views

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

Performing an opening operation on an image using OpenCV

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 773 Views

In this program, we will perform the opening operation on an image using OpenCV. Opening removes small objects from the foreground of an image, placing them in the background. This technique can also be used to find specific shapes in an image. Opening is mathematically defined as erosion followed by dilation. The function we use for this task is cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel). Original Image Algorithm Step 1: Import cv2 and numpy Step 2: Read the image Step 3: Define the kernel (structuring element) Step 4: Pass the image and kernel to cv2.morphologyEx() function ...

Read More

Plotting dates on the X-axis with Python\'s Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 34K+ Views

Using Pandas, we can create a dataframe and set datetime values as the index. Matplotlib's gcf().autofmt_xdate() automatically formats date labels on the X-axis for better readability. Steps to Plot Dates on X-axis Create a list of date strings and convert them to datetime using pd.to_datetime() Prepare your data values (e.g., [1, 2, 3]) Create a DataFrame and assign the data to a column Set the DataFrame index using the datetime values Plot the DataFrame using plt.plot() Format the X-axis dates using plt.gcf().autofmt_xdate() Display the plot with plt.show() Example import pandas as pd import ...

Read More

Dilating images using the OpenCV function dilate()

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 881 Views

In this tutorial, we will learn how to dilate an image using the dilate() function in OpenCV. Dilation is a morphological operation that adds pixels to the boundaries of objects in an image, effectively expanding or thickening white regions and shrinking black regions. What is Image Dilation? Dilation expands the foreground objects in a binary or grayscale image. It uses a kernel (structuring element) that slides over the image, and for each position, it replaces the center pixel with the maximum value in the kernel's neighborhood. Syntax cv2.dilate(src, kernel, iterations=1, borderType=cv2.BORDER_CONSTANT, borderValue=0) Parameters ...

Read More

Eroding an image using the OpenCV function erode()

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 670 Views

In this program, we will erode an image using the OpenCV function erode(). Erosion of an image means to shrink the image by reducing the size of white regions or foreground objects. If any of the pixels in a kernel is 0, then all the pixels in the kernel are set to 0. One condition before applying an erosion function on an image is that the image should be a grayscale image. What is Image Erosion? Image erosion is a morphological operation that reduces the boundaries of foreground objects (white pixels). It works by sliding a structuring element ...

Read More

Show only certain items in legend Python Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 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
Prasad Naik
Updated on 25-Mar-2026 482 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
Prasad Naik
Updated on 25-Mar-2026 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
Showing 4061–4070 of 21,090 articles
« Prev 1 405 406 407 408 409 2109 Next »
Advertisements