Matplotlib Articles

Page 60 of 91

How to position and align a Matplotlib figure legend?

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

To position and align a Matplotlib figure legend, you can control both its location and alignment using various parameters. The bbox_to_anchor parameter provides precise positioning control, while ncol and loc handle alignment and layout. Basic Legend Positioning First, let's create a simple plot with two lines and position the legend ? import matplotlib.pyplot as plt # Create sample data x = [1, 2, 3, 4] y1 = [1, 5, 1, 7] y2 = [5, 1, 7, 1] # Plot two lines line1, = plt.plot(x, y1, linewidth=2, label='Line 1') line2, = plt.plot(x, y2, linewidth=2, label='Line ...

Read More

How can I convert numbers to a color scale in Matplotlib?

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

To convert numbers to a color scale in Matplotlib, you can map numerical values to colors using colormaps and normalization. This technique is commonly used in scatter plots, heatmaps, and other visualizations where color represents data magnitude. Basic Color Mapping with Scatter Plot Here's how to create a scatter plot where colors represent numerical values − import matplotlib.pyplot as plt import numpy as np import pandas as pd # Create sample data x = np.arange(12) y = np.random.rand(len(x)) * 20 c = np.random.rand(len(x)) * 3 + 1.5 # Create DataFrame df = pd.DataFrame({"x": x, ...

Read More

Exporting an svg file from a Matplotlib figure

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

To export an SVG file from a matplotlib figure, we can use the savefig() method with the .svg format. SVG (Scalable Vector Graphics) files are ideal for plots as they maintain quality at any zoom level and are perfect for web publishing. Steps to Export SVG File Set the figure size and adjust the padding between and around the subplots. Create a figure and a set of subplots. Create random x and y data points using numpy. Plot x and y data points using plot() method. Save the .svg format file using savefig() method. Example ...

Read More

Plot numpy datetime64 with Matplotlib

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

To plot a time series in Python using matplotlib with numpy datetime64, we need to create datetime arrays and plot them against numerical values. This is commonly used for visualizing time-based data like stock prices, sensor readings, or any data that changes over time. Basic Steps Create x points using numpy datetime64 arrays Create corresponding y points with numerical data Plot the datetime x-axis against y values using plot() method Display the figure using show() method Example Let's create a simple time series plot with hourly data for a single day ? ...

Read More

How to give sns.clustermap a precomputed distance matrix in Matplotlib?

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

To use sns.clustermap with a precomputed distance matrix in Matplotlib, you need to pass your distance matrix to the row_linkage and col_linkage parameters. This allows you to provide custom clustering results instead of letting seaborn compute distances automatically. Creating a Basic Clustermap with Default Distance First, let's see how clustermap() works with default distance calculation ? import matplotlib.pyplot as plt import seaborn as sns import numpy as np from scipy.cluster.hierarchy import linkage from scipy.spatial.distance import pdist plt.rcParams["figure.figsize"] = [10, 6] sns.set_theme(color_codes=True) # Load example dataset iris = sns.load_dataset("iris") species = iris.pop("species") # Create ...

Read More

How to sharex when using subplot2grid?

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

When using subplot2grid, you can share the x-axis between subplots by passing the sharex parameter. This creates linked subplots where zooming or panning one plot affects the other. Steps to Share X-axis Create random data using numpy Create a figure using figure() method Create the first subplot with subplot2grid() Create the second subplot with sharex=ax1 parameter Plot data on both subplots Use tight_layout() to adjust spacing Display the figure with show() Example Here's how to create subplots with shared x-axis using subplot2grid ? import numpy as np import matplotlib.pyplot as plt ...

Read More

How to add a text into a Rectangle in Matplotlib?

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

To add text into a rectangle in Matplotlib, you can use the annotate() method to place text at the center point of the rectangle. This technique is useful for creating labeled diagrams, flowcharts, or annotated visualizations. Steps Create a figure using figure() method Add a subplot to the current figure Create a rectangle using patches.Rectangle() class Add the rectangle patch to the plot Calculate the center coordinates of the rectangle Use annotate() method to place text at the center Set axis limits and display the plot Example Here's how to create a rectangle with ...

Read More

How to get the center of a set of points using Python?

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

To get the center of a set of points, we calculate the centroid by finding the average of all x-coordinates and y-coordinates. This gives us the geometric center of the point cloud. Method: Calculate Arithmetic Mean The center (centroid) is calculated as ? Center X = sum(all x coordinates) / number of points Center Y = sum(all y coordinates) / number of points Example Let's create a scatter plot and mark the center point ? import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ...

Read More

How do I set color to Rectangle in Matplotlib?

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

In Matplotlib, you can set colors to rectangles using the patches.Rectangle class. This allows you to customize both the edge color and fill color of rectangular shapes in your plots. Basic Rectangle with Colors Here's how to create a colored rectangle using edgecolor and facecolor parameters − import matplotlib.pyplot as plt import matplotlib.patches as patches # Create figure and axis fig, ax = plt.subplots(figsize=(7, 4)) # Create a rectangle with custom colors rectangle = patches.Rectangle((1, 1), 3, 2, ...

Read More

How to scale axes in Mplot3d?

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

To scale axes in Matplotlib's 3D plotting, you need to control the range of each axis using specific methods. This allows you to zoom into particular regions or adjust the proportions of your 3D visualization. Steps to Scale 3D Axes Create a figure using figure() method Create a 3D axes instance using Axes3D() class Use set_xlim3d() to scale the X-axis range Use set_ylim3d() to scale the Y-axis range Use set_zlim3d() to scale the Z-axis range Display the plot using show() method Basic Example Here's how to create a 3D plot with scaled axes ? ...

Read More
Showing 591–600 of 902 articles
« Prev 1 58 59 60 61 62 91 Next »
Advertisements