栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python / Matplotlib-有没有办法制作不连续的轴?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python / Matplotlib-有没有办法制作不连续的轴?

这只是此示例的简单修改,具有不连续的x轴而不是y轴。(这就是为什么我要将此帖子设为CW)

基本上,你只需要执行以下操作:

import matplotlib.pylab as pltimport numpy as np# If you're not familiar with np.r_, don't worry too much about this. It's just # a series with points from 0 to 1 spaced at 0.1, and 9 to 10 with the same spacing.x = np.r_[0:1:0.1, 9:10:0.1]y = np.sin(x)fig,(ax,ax2) = plt.subplots(1, 2, sharey=True)# plot the same data on both axesax.plot(x, y, 'bo')ax2.plot(x, y, 'bo')# zoom-in / limit the view to different portions of the dataax.set_xlim(0,1) # most of the dataax2.set_xlim(9,10) # outliers only# hide the spines between ax and ax2ax.spines['right'].set_visible(False)ax2.spines['left'].set_visible(False)ax.yaxis.tick_left()ax.tick_params(labeltop='off') # don't put tick labels at the topax2.yaxis.tick_right()# Make the spacing between the two axes a bit smallerplt.subplots_adjust(wspace=0.15)plt.show()

要添加折断的轴线//效果,我们可以这样做(同样,从Paul Ivanov的示例进行了修改):

import matplotlib.pylab as pltimport numpy as np# If you're not familiar with np.r_, don't worry too much about this. It's just # a series with points from 0 to 1 spaced at 0.1, and 9 to 10 with the same spacing.x = np.r_[0:1:0.1, 9:10:0.1]y = np.sin(x)fig,(ax,ax2) = plt.subplots(1, 2, sharey=True)# plot the same data on both axesax.plot(x, y, 'bo')ax2.plot(x, y, 'bo')# zoom-in / limit the view to different portions of the dataax.set_xlim(0,1) # most of the dataax2.set_xlim(9,10) # outliers only# hide the spines between ax and ax2ax.spines['right'].set_visible(False)ax2.spines['left'].set_visible(False)ax.yaxis.tick_left()ax.tick_params(labeltop='off') # don't put tick labels at the topax2.yaxis.tick_right()# Make the spacing between the two axes a bit smallerplt.subplots_adjust(wspace=0.15)# This looks pretty good, and was fairly painless, but you can get that# cut-out diagonal lines look with just a bit more work. The important# thing to know here is that in axes coordinates, which are always# between 0-1, spine endpoints are at these locations (0,0), (0,1),# (1,0), and (1,1). Thus, we just need to put the diagonals in the# appropriate corners of each of our axes, and so long as we use the# right transform and disable clipping.d = .015 # how big to make the diagonal lines in axes coordinates# arguments to pass plot, just so we don't keep repeating themkwargs = dict(transform=ax.transAxes, color='k', clip_on=False)ax.plot((1-d,1+d),(-d,+d), **kwargs) # top-left diagonalax.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-left diagonalkwargs.update(transform=ax2.transAxes) # switch to the bottom axesax2.plot((-d,d),(-d,+d), **kwargs) # top-right diagonalax2.plot((-d,d),(1-d,1+d), **kwargs) # bottom-right diagonal# What's cool about this is that now if we vary the distance between# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),# the diagonal lines will move accordingly, and stay right at the tips# of the spines they are 'breaking'plt.show()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414825.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号