python 创建子图_python-matplotlib在子图中创建断轴

这篇博客介绍了如何在Python的Matplotlib库中创建具有断轴效果的子图。作者展示了如何通过设置子图的限界和使用spines来实现这一效果,特别是在一个包含两个不同数据规模的子图中,使得一半的y轴从0到10,另一半从10到100。
摘要由CSDN通过智能技术生成

我有一个有问题的子图,其中包含两个数据规模.我不想使用对数刻度,而是要断开轴,以便子图y轴的一半从0到10,另一半从10到100.

import numpy as np

import matplotlib.pyplot as plt

x = np.random.uniform(0, 10, 40)

y = np.concatenate([np.random.uniform(0, 1, 30), np.random.uniform(0, 100, 10)])

y2 = np.random.uniform(0, 1, 40)

fig, ax = plt.subplots(2, sharex=True)

ax[0].scatter(x, y) # problematic subplot

ax[1].scatter(x, y2)

plt.show()

GS5fc.png

我尝试遵循pyplot的断轴演示,尽管这似乎是错误的.为此,我是否需要创建总共四个子图?这只是一个虚拟的例子,我真正的问题有几个子图,其中许多都需要这些断轴.

import numpy as np

import matplotlib.pyplot as plt

x = np.random.uniform(0, 10, 40)

y = np.concatenate([np.random.uniform(0, 1, 30), np.random.uniform(0, 100, 10)])

y2 = np.random.uniform(0, 1, 40)

fig, ax = plt.subplots(4, sharex=True)

# Create broken axis with first two subplots

ax[0].scatter(x, y)

ax[1].scatter(x, y)

ax[0].set_ylim(1, 100)

ax[1].set_ylim(0, 1)

ax[0].spines['bottom'].set_visible(False)

ax[1].spines['top'].set_visible(False)

# From https://matplotlib.org/examples/pylab_examples/broken_axis.html

d = .015 # how big to make the diagonal lines in axes coordinates

# arguments to pass to plot, just so we don't keep repeating them

kwargs = dict(transform=ax[0].transAxes, color='k', clip_on=False)

ax[0].plot((-d, +d), (-d, +d), **kwargs) # top-left diagonal

ax[0].plot((1 - d, 1 + d), (-d, +d), **kwargs) # top-right diagonal

kwargs.update(transform=ax[1].transAxes) # switch to the bottom axes

ax[0].plot((-d, +d), (1 - d, 1 + d), **kwargs) # bottom-left diagonal

ax[0].plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs) # bottom-right diagonal

# Try my best to fix bottom two plots so they look like one plot

ax[2].scatter(x, y2)

ax[3].scatter(x, y2)

ax[2].set_ylim(.5, 1.0)

ax[3].set_ylim(0, .5)

ax[2].spines['bottom'].set_visible(False)

ax[3].spines['top'].set_visible(False)

plt.savefig('ex.pdf')

VSAax.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值