How to Create Discontinuous Axes in Matplotlib?
Creating Discontinuous Axes in Matplotlib
Introduction:
When creating plots using Matplotlib, a continuous x-axis is typically used. However, there may be instances where a discontinuous axis is desired, where a gap or jump occurs in the x-axis values. This can be useful for displaying data with missing or sparsely distributed values.
Using Subplots:
One approach to creating a discontinuous axis is to use subplots. Each subplot can be assigned a different range of x-axis values, resulting in a gap between the subplots. Here's a simple example:
import matplotlib.pyplot as plt x1 = np.linspace(0, 5, 100) y1 = np.sin(x1) x2 = np.linspace(10, 15, 100) y2 = np.cos(x2) plt.subplot(1, 2, 1) plt.plot(x1, y1) plt.subplot(1, 2, 2) plt.plot(x2, y2) plt.show()
Custom Axis Transformation:
Another method for creating a discontinuous axis is to use a custom axis transformation. By defining a new transformation class, we can specify how the data is mapped to the axis. The following code demonstrates this approach:
import matplotlib.pyplot as plt from matplotlib.transforms import Transform from matplotlib.ticker import LogLocator class DiscontinuousTransform(Transform): def __init__(self, breaks): Transform.__init__(self) self.breaks = breaks def transform(self, values): new_values = values.copy() for break in self.breaks: new_values[values > break] += 1 return new_values def inverted(self): return InvertedDiscontinuousTransform(self.breaks) class InvertedDiscontinuousTransform(Transform): def __init__(self, breaks): Transform.__init__(self) self.breaks = breaks def transform(self, values): new_values = values.copy() for break in self.breaks: new_values[values >= break] -= 1 return new_values def inverted(self): return DiscontinuousTransform(self.breaks) x = np.linspace(0, 10, 100) y = np.sin(x) trans = DiscontinuousTransform([5]) locator = LogLocator(base=10) locator.set_params(minor_locator=None) plt.plot(x, y, transform=trans) plt.gca().xaxis.set_major_locator(locator) plt.gca().xaxis.set_major_formatter(plt.FormatStrFormatter("%0.0f\n(pert)")) plt.show()
Conclusion:
Creating a discontinuous axis in Matplotlib can be achieved using subplots or custom axis transformations. The custom transformation approach provides more flexibility and control over the axis behavior. Both methods can be effective for visualizing data with gaps or discontinuities.
The above is the detailed content of How to Create Discontinuous Axes in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
