一张图看懂python-Python 如何将不同数量级的数据画在同一张图上?

本文介绍了如何使用Python的matplotlib库,在同一张图表上同时展示具有不同数量级的数据,通过创建两个子图并调整它们的视图限制,确保细节清晰可见。通过添加对角线来增强'切掉'轴的效果,使得数据分布更加直观。这种方法允许数据的变化范围在调整子图间距时仍保持清晰可见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# If we were to simply plot pts, we'd lose most of the interesting

# details due to the outliers. So let's 'break' or 'cut-out' the y-axis

# into two portions - use the top (ax) for the outliers, and the bottom

# (ax2) for the details of the majority of our data

f, (ax, ax2) = plt.subplots(2, 1, sharex=True)

# plot the same data on both axes

ax.plot(pts)

ax2.plot(pts)

# zoom-in / limit the view to different portions of the data

ax.set_ylim(.78, 1.) # outliers only

ax2.set_ylim(0, .22) # most of the data

# hide the spines between ax and ax2

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

ax2.spines['top'].set_visible(False)

ax.xaxis.tick_top()

ax.tick_params(labeltop='off') # don't put tick labels at the top

ax2.xaxis.tick_bottom()

# 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 import

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值