Matplotlib Articles

Page 24 of 91

How to show a figure that has been closed in Matplotlib?

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

When you close a figure in Matplotlib, it's removed from memory and cannot be displayed again using the standard plt.show(). However, you can restore a closed figure by creating a new canvas manager and transferring the figure data. Understanding the Problem Once plt.close() is called on a figure, the canvas connection is broken. To display it again, we need to create a new canvas and reassign the figure to it. Example: Restoring a Closed Figure Here's how to show a figure that has been closed ? import numpy as np import matplotlib.pyplot as plt ...

Read More

How to set the Y-axis in radians in a Python plot?

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

To set the Y-axis in radians in a Python plot, we need to customize the axis ticks and labels to display radian values like π/2, π/4, etc. This is commonly needed when plotting trigonometric functions. Steps to Set Y-axis in Radians Create data points using NumPy Plot the data using matplotlib Define custom tick positions in radian units Set custom tick labels using LaTeX formatting for fractions Apply the ticks and labels using set_yticks() and set_yticklabels() Example Let's plot the arctangent function and set its Y-axis in radians ? import matplotlib.pyplot as ...

Read More

How can box plot be overlaid on top of swarm plot in Seaborn?

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

Overlaying a box plot on top of a swarm plot in Seaborn creates an effective visualization that combines individual data points with summary statistics. The swarm plot shows each data point while the box plot provides quartile information and outliers. Basic Overlay Example Here's how to create a box plot overlaid on a swarm plot using sample data − import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [8, 5] plt.rcParams["figure.autolayout"] = True # Create sample data np.random.seed(42) data = pd.DataFrame({ ...

Read More

Adjust the width of box in boxplot in Python Matplotlib

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

In Python Matplotlib, you can adjust the width of boxes in a boxplot using the widths parameter in the boxplot() method. This allows you to create boxes of different sizes for better visualization and comparison. Steps Set the figure size and adjust the padding between and around the subplots Create sample data using Pandas DataFrame Use the boxplot() method with the widths parameter to adjust box dimensions Display the plot using the show() method Example Here's how to create a boxplot with different box widths ? import pandas as pd import numpy ...

Read More

How to draw a heart with pylab?

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

Drawing a heart shape with pylab/matplotlib can be achieved using mathematical equations and the fill_between() method. This creates a beautiful heart visualization using parametric equations. Mathematical Approach The heart shape is created using two mathematical functions: Upper part: y1 = sqrt(1 - (abs(x) - 1)²) Lower part: y2 = -3 * sqrt(1 - (abs(x) / 2)^0.5) Steps Set the figure size and adjust the padding between and around the subplots. Create x, y1 and y2 data points using numpy. Fill the area between (x, y1) and (x, y2) using fill_between() method. Place ...

Read More

How do I show the same Matplotlib figure several times in a single IPython notebook?

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

To show the same Matplotlib figure several times in a single Jupyter notebook, you can use the fig.show() method or plt.show(). This is useful when you want to display the same plot multiple times at different points in your notebook. Basic Approach Using fig.show() Create a figure once and display it multiple times using the figure's show method ? import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create figure and plot data fig, ax = plt.subplots() ax.plot([2, 4, 7, 5, 4, 1]) ax.set_title('Sample Line Plot') ...

Read More

How to plot sine curve on polar axes using Matplotlib?

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

To plot a sine curve on polar axes using Matplotlib, we need to create a polar coordinate system and plot angular data. Polar plots are useful for representing periodic data and circular patterns. Steps to Create a Polar Sine Plot Set the figure size and adjust the padding between and around the subplots Create a new figure using figure() method Add polar axes using add_subplot(projection='polar') Generate angular data points (theta) and radius values using numpy Plot the data using plot() method on polar axes Display the figure using show() method Example import numpy ...

Read More

How do I find the intersection of two line segments in Matplotlib?

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

To find the intersection of two line segments in Matplotlib, we calculate where two lines meet using their slopes and intercepts, then draw horizontal and vertical lines through that point. Mathematical Formula For two lines with equations y = m1*x + c1 and y = m2*x + c2, the intersection point is: x_intersection = (c1 - c2) / (m2 - m1) y_intersection = m1 * x_intersection + c1 Example Here's how to find and visualize the intersection point ? import matplotlib.pyplot as plt import numpy as np ...

Read More

How to show minor tick labels on a log-scale with Matplotlib?

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

In Matplotlib, displaying minor tick labels on a log-scale plot requires special formatting since log scales typically only show major ticks by default. We can achieve this by using the tick_params() method and FormatStrFormatter to control minor tick appearance. Basic Log-Scale Plot with Minor Ticks Here's how to create a log-scale plot and display minor tick labels ? import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FormatStrFormatter # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points x = np.linspace(-2, 2, 10) y = np.exp(x) ...

Read More

How to fill the area under a step curve using pyplot? (Matplotlib)

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

To fill the area under a step curve using pyplot, you can use the fill_between() method with the step parameter. This creates filled regions beneath step plots, which are useful for displaying discrete data or histogram-like visualizations. Basic Step Curve with Fill Here's how to create a simple step curve with filled area ? import matplotlib.pyplot as plt import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points x = np.linspace(-10, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Fill area under step ...

Read More
Showing 231–240 of 902 articles
« Prev 1 22 23 24 25 26 91 Next »
Advertisements