Articles on Trending Technologies

Technical articles with clear explanations and examples

How can I draw a scatter trend line using Matplotlib?

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

To draw a scatter trend line using Matplotlib, we can use polyfit() and poly1d() methods to calculate and plot the trend line over scattered data points. Basic Scatter Plot with Trend Line Here's how to create a scatter plot with a linear trend line − import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create sample data x = np.random.rand(100) y = np.random.rand(100) # Create scatter plot fig, ax = plt.subplots() ax.scatter(x, y, c=x, cmap='plasma', alpha=0.7) # Calculate trend line using ...

Read More

How to plot hexbin histogram in Matplotlib?

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

A hexbin histogram in Matplotlib displays the distribution of two-dimensional data using hexagonal bins. This visualization is particularly useful for large datasets where traditional scatter plots become cluttered with overlapping points. Basic Hexbin Plot The hexbin() method creates a hexagonal binning plot where the color intensity represents the density of data points in each hexagon ? import numpy as np import matplotlib.pyplot as plt # Generate sample data x = 2 * np.random.randn(5000) y = x + np.random.randn(5000) # Create hexbin plot fig, ax = plt.subplots(figsize=(8, 6)) hb = ax.hexbin(x[::10], y[::10], gridsize=20, cmap='plasma') ...

Read More

How to make joint bivariate distributions in Matplotlib?

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

Joint bivariate distributions visualize the relationship between two continuous variables. In Matplotlib, we can create these distributions using scatter plots with transparency to show density patterns. Basic Joint Bivariate Distribution Here's how to create a simple joint bivariate distribution using correlated data − import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [8, 6] plt.rcParams["figure.autolayout"] = True # Generate correlated data np.random.seed(42) x = 2 * np.random.randn(5000) y = x + np.random.randn(5000) # Create scatter plot fig, ax = plt.subplots() scatter = ax.scatter(x, y, alpha=0.08, c=x, cmap="viridis") ...

Read More

How to align the bar and line in Matplotlib two Y-axes chart?

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

To align bar and line plots in matplotlib with two Y-axes, we use the twinx() method to create a twin axis that shares the X-axis but has an independent Y-axis. This allows overlaying different chart types on the same plot. Creating DataFrame and Basic Setup First, let's create sample data and set up the figure parameters ? import matplotlib.pyplot as plt import pandas as pd # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create sample DataFrame df = pd.DataFrame({"col1": [1, 3, 5, 7, 1], "col2": [1, 5, 7, ...

Read More

Draw a border around subplots in Matplotlib

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

To draw a border around subplots in Matplotlib, we can use a Rectangle patch that creates a visible border around the subplot area. This technique is useful for highlighting specific subplots or creating visual separation in multi−subplot figures. Basic Border Around Single Subplot Here's how to add a simple border around one subplot ? import matplotlib.pyplot as plt import matplotlib.patches as patches # Create figure and subplot fig, ax = plt.subplots(1, 1, figsize=(6, 4)) # Plot some sample data ax.plot([1, 2, 3, 4], [1, 4, 2, 3], 'b-o') ax.set_title("Subplot with Border") # Get ...

Read More

Plotting a cumulative graph of Python datetimes in Matplotlib

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

To plot a cumulative graph of Python datetimes in Matplotlib, you can combine Pandas datetime functionality with Matplotlib's plotting capabilities. This is useful for visualizing data trends over time, such as daily sales, website visits, or any time-series data that accumulates. Basic Setup First, let's create a dataset with datetime values and plot a cumulative sum ? import pandas as pd import matplotlib.pyplot as plt from datetime import datetime, timedelta import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [10, 6] plt.rcParams["figure.autolayout"] = True # Create sample datetime data start_date = datetime(2024, 1, ...

Read More

How can I programmatically select a specific subplot in Matplotlib?

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

To programmatically select a specific subplot in Matplotlib, you can use several approaches including add_subplot(), subplots(), or subplot() methods. This allows you to modify individual subplots after creation. Method 1: Using add_subplot() Create subplots in a loop and then select a specific one by calling add_subplot() again with the same position ? import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [10, 4] plt.rcParams["figure.autolayout"] = True fig = plt.figure() # Create 4 subplots for index in [1, 2, 3, 4]: ax = fig.add_subplot(1, 4, index) ...

Read More

How to get smooth interpolation when using pcolormesh (Matplotlib)?

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

Pcolormesh in Matplotlib creates pseudocolor plots with rectangular grids. By default, it produces blocky, pixelated output. To achieve smooth interpolation and eliminate harsh edges, you can use the shading="gouraud" parameter. Default Pcolormesh vs Smooth Interpolation Let's first see the difference between default shading and Gouraud shading ? import matplotlib.pyplot as plt import numpy as np # Create sample data data = np.random.random((5, 5)) x = np.arange(0, 5, 1) y = np.arange(0, 5, 1) x, y = np.meshgrid(x, y) # Create subplots for comparison fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5)) # Default ...

Read More

How to change axes background color in Matplotlib?

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

To change the axes background color in Matplotlib, we can use the set_facecolor() method. This method allows us to customize the background color of the plotting area to make our visualizations more appealing or to match specific design requirements. Using set_facecolor() Method The most straightforward approach is to get the current axes and apply set_facecolor() ? import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Get current axes and set background color ax = plt.gca() ax.set_facecolor("orange") # Create data points x = ...

Read More

Populating Matplotlib subplots through a loop and a function

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

Matplotlib subplots can be efficiently populated using loops and functions to create organized grid layouts. This approach is especially useful when you need to display multiple related plots with similar formatting. Creating the Subplot Grid First, let's set up a 3×2 grid of subplots and define our plotting function ? import numpy as np import matplotlib.pyplot as plt # Set figure size and layout plt.rcParams["figure.figsize"] = [10, 8] plt.rcParams["figure.autolayout"] = True # Create 3 rows and 2 columns of subplots fig, axes = plt.subplots(3, 2) def iterate_columns(cols, x, color='red'): ...

Read More
Showing 4451–4460 of 61,297 articles
« Prev 1 444 445 446 447 448 6130 Next »
Advertisements