python-matplotlib在子图中创建断轴
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-matplotlib在子图中创建断轴,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3307字,纯文字阅读大概需要5分钟。
内容图文
![python-matplotlib在子图中创建断轴](/upload/InfoBanner/zyjiaocheng/690/018510995e324ccb8418b201488b4952.jpg)
我有一个有问题的子图,其中包含两个数据规模.我不想使用对数刻度,而是要断开轴,以便子图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()
我尝试遵循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')
解决方法:
我可能建议仅使用两个子图,一个在顶部,一个在底部.然后,通过mpl_toolkits.axes_grid1.make_axes_locatable将上面的一分为二.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
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, axes = plt.subplots(nrows=2, sharex=True)
ax = axes[0]
divider = make_axes_locatable(ax)
ax2 = divider.new_vertical(size="100%", pad=0.1)
fig.add_axes(ax2)
ax.scatter(x, y)
ax.set_ylim(0, 1)
ax.spines['top'].set_visible(False)
ax2.scatter(x, y)
ax2.set_ylim(10, 100)
ax2.tick_params(bottom="off", labelbottom='off')
ax2.spines['bottom'].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=ax2.transAxes, color='k', clip_on=False)
ax2.plot((-d, +d), (-d, +d), **kwargs) # top-left diagonal
ax2.plot((1 - d, 1 + d), (-d, +d), **kwargs) # top-right diagonal
kwargs.update(transform=ax.transAxes) # switch to the bottom axes
ax.plot((-d, +d), (1 - d, 1 + d), **kwargs) # bottom-left diagonal
ax.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs) # bottom-right diagonal
#create bottom subplot as usual
axes[1].scatter(x, y2)
plt.show()
内容总结
以上是互联网集市为您收集整理的python-matplotlib在子图中创建断轴全部内容,希望文章能够帮你解决python-matplotlib在子图中创建断轴所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。