Python Articles

Page 320 of 855

How to create minor ticks for a polar plot in matplotlib?

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

To create minor ticks for a polar plot in matplotlib, you can manually draw tick marks at specified angular positions. This technique is useful when you need more granular control over tick positioning than the default matplotlib settings provide. Basic Approach The process involves creating radial lines at specific angles to simulate minor ticks. Here's how to implement it ? import numpy as np import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create radius and theta data points r = np.arange(0, 5, 0.1) theta = ...

Read More

How to plot an animated image matrix in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 2K+ Views

To plot an animated image matrix in matplotlib, we can use FuncAnimation to repeatedly update a matrix display. This creates smooth animated visualizations of changing data patterns. Steps Set the figure size and adjust the padding between and around the subplots. Create a figure and a set of subplots. Make an animation by repeatedly calling a function update. Inside the update method, create a 6×6 dimension of matrix and display the data as an image, i.e., on a 2D regular raster. Turn off the axes using set_axis_off(). To display the figure, use show() method. Basic ...

Read More

How to put xtick labels in a box matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 717 Views

To put xtick labels in a box in matplotlib, we use the set_bbox() method on tick label objects. This creates a visible box around each x-axis label with customizable styling. Steps Create a new figure or activate an existing figure Get the current axis of the figure Position the spines and ticks as needed Iterate through the x-tick labels using get_xticklabels() Apply set_bbox() method with desired box properties Display the figure using show() method Basic Example Here's how to add boxes around x-tick labels ? import matplotlib.pyplot as plt import numpy as ...

Read More

Program to find maximum score by splitting binary strings into two parts in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 412 Views

Suppose we have a binary string s. We need to split it into two non-empty substrings s1 and s2. The score of this split is the count of "0"s in s1 plus the count of "1"s in s2. We have to find the maximum score we can obtain. So, if the input is like s = "011001100111", then the output will be 8, because we can split the string like "01100" + "1100111". Then, the score is 3 + 5 = 8. Algorithm To solve this, we will follow these steps − ones := number ...

Read More

How to plot a time as an index value in a Pandas dataframe in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 2K+ Views

To plot a time as an index value in a Pandas DataFrame using Matplotlib, you need to set the time column as the DataFrame index. This allows the time values to appear on the x−axis automatically when plotting. Steps Create a DataFrame with time and numeric data columns Convert the time column to datetime format if needed Set the time column as the DataFrame index using set_index() Use the DataFrame's plot() method to create the visualization Basic Time Series Plot Here's how to create a simple time series plot with time as the index ...

Read More

Program to find matrix for which rows and columns holding sum of behind rows and columns in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 222 Views

Given a matrix, we need to find a new matrix where each element at position res[i, j] contains the sum of all elements from the original matrix where row r ≤ i and column c ≤ j. This is known as calculating the prefix sum matrix or cumulative sum matrix. Problem Understanding For each position (i, j) in the result matrix, we sum all elements in the rectangle from (0, 0) to (i, j) in the original matrix. If the input matrix is ? 8 2 7 4 Then ...

Read More

How to put a title for a curved line in Python Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 685 Views

To add a title to a curved line plot in Python Matplotlib, you use the plt.title() method after creating your plot. This is essential for making your visualizations clear and informative. Steps Set the figure size and adjust the padding between and around the subplots. Create x and y data points such that the line would be a curve. Plot the x and y data points using plt.plot(). Place a title for the curve plot using plt.title() method. Display the figure using ...

Read More

Program to find length of longest sublist containing repeated numbers by k operations in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 327 Views

Suppose we have a list called nums and a value k, now let us consider an operation by which we can update the value of any number in the list. We have to find the length of the longest sublist which contains repeated numbers after performing at most k operations. So, if the input is like nums = [8, 6, 6, 4, 3, 6, 6] k = 2, then the output will be 6, because we can change 4 and 3 to 6, to make this array [8, 6, 6, 6, 6, 6, 6], and the length of sublist ...

Read More

Program to find length of longest contiguous sublist with same first letter words in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 267 Views

Suppose we have a list of lowercase alphabet strings called words. We have to find the length of the longest contiguous sublist where each word has the same first letter. So, if the input is like words = ["she", "sells", "seashells", "on", "the", "sea", "shore"], then the output will be 3, because the longest contiguous sublist is ["she", "sells", "seashells"] where each word starts with 's'. Algorithm To solve this, we will follow these steps − Initialize cnt = 1 to track current sequence length Initialize maxcnt = 0 to track maximum length found Initialize ...

Read More

Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 3K+ Views

To plot the FFT (Fast Fourier Transform) of a signal with correct frequencies on the X-axis in matplotlib, we need to properly compute the frequency bins and visualize the power spectrum. Understanding FFT Frequency Calculation The key to plotting FFT with correct frequencies is using np.fft.fftfreq() which returns the discrete Fourier Transform sample frequencies. For real signals, we typically plot only the positive frequencies using np.fft.rfftfreq(). Basic FFT Plot with Normalized Frequencies Here's how to create a basic FFT plot with normalized frequencies ? import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] ...

Read More
Showing 3191–3200 of 8,546 articles
« Prev 1 318 319 320 321 322 855 Next »
Advertisements