How to independently set horizontal and vertical, major and minor gridlines of a plot?

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:57:13

1K+ Views

Matplotlib allows you to independently control horizontal and vertical, major and minor gridlines using the grid() method along with axis-specific methods. This provides fine-grained control over plot appearance. Basic Syntax The grid() method can be applied to specific axes: ax.xaxis.grid(which="major", color='color', linestyle='style', linewidth=width) ax.yaxis.grid(which="minor", color='color', linestyle='style', linewidth=width) Parameters which − Specifies "major" or "minor" gridlines color − Sets the gridline color linestyle − Defines line style ('-', '--', '-.', ':') linewidth − Controls line thickness Complete Example Here's how to create independent horizontal and vertical gridlines with different styles ... Read More

Contour hatching in Matplotlib plot

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:56:50

881 Views

Contour hatching in Matplotlib allows you to add visual patterns to filled contour plots, making it easier to distinguish between different regions. This is particularly useful for creating publication-ready plots or when working with grayscale images. Basic Contour Hatching Here's how to create a contour plot with different hatch patterns ? 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 coordinate arrays x = np.linspace(-3, 5, 150) y = np.linspace(-3, 5, 120) X, Y = np.meshgrid(x, y) # Generate sample data ... Read More

How can I move a tick label without moving corresponding tick in Matplotlib?

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:56:23

648 Views

In Matplotlib, you can move tick labels independently from their corresponding ticks using the set_xticklabels() or set_yticklabels() methods, or by creating custom annotations. This is useful when you need to adjust label positioning for better readability without affecting the tick marks themselves. Method 1: Using set_xticklabels() with Custom Positions The most straightforward approach is to get existing tick positions and create custom labels ? import matplotlib.pyplot as plt import numpy as np # Create sample data x = np.linspace(0, 10, 100) y = np.sin(x) plt.figure(figsize=(10, 6)) plt.plot(x, y) # Get current tick positions ... Read More

How to access axis label object in Matplotlib?

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:55:58

2K+ Views

To access axis label objects in Matplotlib, you can use the get_label() method on the axis object, which returns the label text object. This is useful when you need to retrieve or manipulate axis labels programmatically. Basic Syntax The main methods for accessing axis labels are ? # Get X-axis label text x_label_text = ax.xaxis.get_label().get_text() # Get Y-axis label text y_label_text = ax.yaxis.get_label().get_text() # Get the label object itself x_label_obj = ax.xaxis.get_label() y_label_obj = ax.yaxis.get_label() Example: Accessing Axis Label Objects Here's a complete example showing how to set and ... Read More

Adjust one subplot's height in absolute way (not relative) in Matplotlib

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:55:37

397 Views

When creating subplots in Matplotlib, you sometimes need precise control over their positioning and dimensions. The Axes() class allows you to specify absolute positions and sizes instead of using relative grid layouts. Understanding Axes Parameters The Axes() class takes parameters [left, bottom, width, height] where all values are in figure coordinates (0 to 1) ? left − horizontal position of the left edge bottom − vertical position of the bottom edge width − width of the subplot height − height of the subplot Example Here's how to create two subplots with different absolute ... Read More

Calculate the curl of a vector field in Python and plot it with Matplotlib

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:55:19

2K+ Views

To calculate the curl of a vector field in Python and plot it with Matplotlib, we can use the quiver() method to visualize the vector field and its curl components in 3D space. What is Curl? The curl of a vector field F = (u, v, w) measures the rotation or circulation of the field at each point. For a 3D vector field, the curl is calculated as: curl F = (∂w/∂y - ∂v/∂z, ∂u/∂z - ∂w/∂x, ∂v/∂x - ∂u/∂y) Example: Calculating and Plotting Curl Let's create a vector field and visualize its curl using ... Read More

How to assign specific colors to specific cells in a Matplotlib table?

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:54:56

2K+ Views

Matplotlib allows you to assign specific colors to individual cells in a table using the cellColours parameter. This is useful for highlighting data, creating color-coded reports, or improving table readability. Basic Syntax The ax.table() method accepts a cellColours parameter that takes a 2D list where each element corresponds to a cell color ? ax.table(cellText=data, cellColours=colors, colLabels=columns, loc='center') Example Let's create a table with employee data and assign specific colors to each cell ? import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # ... Read More

How to plot with different scales in Matplotlib?

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:54:37

5K+ Views

When working with data that has vastly different ranges, plotting multiple datasets on the same axes can make one dataset barely visible. Matplotlib provides twinx() and twiny() methods to create dual-axis plots with different scales. Basic Dual Y-Axis Plot Here's how to create a plot with two different y-axis scales using twinx() ? import numpy as np import matplotlib.pyplot as plt # Create sample data with different ranges t = np.arange(0.01, 10.0, 0.01) data1 = np.exp(t) # Exponential growth (large values) data2 = np.sin(2 * np.pi * t) # Sine wave (-1 to ... Read More

How can you clear a Matplotlib textbox that was previously drawn?

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:54:12

3K+ Views

To clear a Matplotlib textbox that was previously drawn, you can use the remove() method on the text object. This is useful when you need to dynamically update or clear text elements from your plots. Basic Text Removal When you create text in Matplotlib, it returns a text artist object that you can later remove using the remove() method. 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 plot fig, ax = plt.subplots() x = np.linspace(-10, 10, 100) y = np.sin(x) ax.plot(x, ... Read More

Horizontal stacked bar chart in Matplotlib

Rishikesh Kumar Rishi
Updated on 25-Mar-2026 22:53:57

11K+ Views

A horizontal stacked bar chart displays data as horizontal bars where multiple data series are stacked on top of each other. Matplotlib's barh() method makes it easy to create these charts by using the left parameter to stack bars horizontally. Syntax plt.barh(y, width, left=None, height=0.8, color=None) Parameters y − The y coordinates of the bars width − The width of the bars left − The x coordinates of the left sides of the bars (for stacking) height − The heights of the bars color − The colors of the bars Example ... Read More

Advertisements