Python Articles

Page 145 of 855

How to center labels in a Matplotlib histogram plot?

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

To place the labels at the center in a histogram plot, we can calculate the mid-point of each patch and place the ticklabels accordingly using xticks() method. This technique provides better visual alignment between the labels and their corresponding histogram bars. Steps Set the figure size and adjust the padding between and around the subplots. Create a random standard sample data, x. Initialize a variable for number of bins. Use hist() method to make a histogram plot. ...

Read More

Show tick labels when sharing an axis in Matplotlib

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

When creating subplots that share an axis in Matplotlib, you might want to show tick labels on all subplots instead of hiding them on shared axes. By default, Matplotlib hides tick labels on shared axes to avoid redundancy, but you can control this behavior. Default Behavior with Shared Y-Axis When using sharey parameter, Matplotlib automatically hides y-axis tick labels on the right subplot to avoid duplication ? import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [10, 4] plt.rcParams["figure.autolayout"] = True # Create first subplot ax1 = plt.subplot(1, 2, 1) ax1.plot([1, 4, 9]) ax1.set_title('Left Plot') # ...

Read More

How to extract data from a Matplotlib plot?

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

To extract data from a plot in matplotlib, we can use get_xdata() and get_ydata() methods. This is useful when you need to retrieve the underlying data points from an existing plot object. Basic Data Extraction The following example demonstrates how to extract x and y coordinates from a matplotlib plot ? import numpy as np from matplotlib import pyplot as plt # Configure plot appearance plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create sample data y = np.array([1, 3, 2, 5, 2, 3, 1]) # Create plot and store line object curve, ...

Read More

How to plot complex numbers (Argand Diagram) using Matplotlib?

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

Complex numbers can be visualized using an Argand diagram, where the real part is plotted on the x-axis and the imaginary part on the y-axis. Matplotlib provides an excellent way to create these plots using scatter plots. Basic Argand Diagram Let's start by plotting a simple set of complex numbers ? import numpy as np import matplotlib.pyplot as plt # Create complex numbers complex_numbers = [1+2j, 3+1j, 2-1j, -1+3j, -2-2j] # Extract real and imaginary parts real_parts = [z.real for z in complex_numbers] imag_parts = [z.imag for z in complex_numbers] # Create the ...

Read More

Plotting multiple line graphs using Pandas and Matplotlib

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

To plot multiple line graphs using Pandas and Matplotlib, we can create a DataFrame with different datasets and use the plot() method to visualize multiple lines on the same graph. This approach is useful for comparing trends across different data series. Basic Setup First, let's set up the required libraries and configure the plot settings ? import pandas as pd import matplotlib.pyplot as plt # Set figure size for better visualization plt.rcParams["figure.figsize"] = [10, 6] plt.rcParams["figure.autolayout"] = True Creating Sample Data We'll create a DataFrame with data for three different mathematical functions ...

Read More

Program to change the root of a binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 980 Views

Suppose we are given a binary tree and need to change its root to a specific leaf node. This transformation involves restructuring the tree by following specific rules to maintain the binary tree structure while making the leaf node the new root. Transformation Rules To change the root of a binary tree, we follow these rules − If a node has a left child, it becomes the right child. A node's parent becomes its left child. In this process, the parent node's link to that node becomes null, so it will have only one child. ...

Read More

Program to fix a erroneous binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 288 Views

When a binary tree has an erroneous connection where a node's right child pointer incorrectly points to another node at the same level, we need to identify and fix this error. The solution involves detecting the problematic node and removing it along with its descendants, except for the node it erroneously points to. Consider this example where node 4's right child incorrectly points to node 6: 5 3 7 ...

Read More

Program to find out the lowest common ancestor of a binary tree using parent pointers using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 249 Views

The Lowest Common Ancestor (LCA) of two nodes in a binary tree is the lowest node that has both nodes as descendants. When parent pointers are available, we can efficiently find the LCA by traversing upward from one node and checking if we encounter the other node's ancestors. Node Structure The tree node contains parent pointers for upward traversal ? TreeNode: data: left: right: parent: Algorithm The approach follows these steps ? ...

Read More

Program to find out the lowest common ancestor of a binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 217 Views

The Lowest Common Ancestor (LCA) of two nodes in a binary tree is the deepest node that has both nodes as descendants. A node can be considered a descendant of itself. This problem is commonly solved using recursive traversal. So, if the input is like: 5 3 7 2 ...

Read More

Program to add two polynomials given as linked lists using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

Suppose we are given two polynomials represented as linked lists and we need to find their addition. Each polynomial is stored as a linked list where each node contains a coefficient, power, and pointer to the next node. We need to return a new linked list representing the sum of the two polynomials. For example, if we have polynomials 1x^1 + 1x^2 and 2x^1 + 3x^0, then the output will be 3x^1 + 1x^2 + 3x^0. Algorithm To solve this, we will follow these steps − Create dummy and node pointers ...

Read More
Showing 1441–1450 of 8,549 articles
« Prev 1 143 144 145 146 147 855 Next »
Advertisements