如何使用 Matplotlib 处理渐近线/不连续性 [英] how to handle an asymptote/discontinuity with Matplotlib

查看:47
本文介绍了如何使用 Matplotlib 处理渐近线/不连续性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在绘制具有不连续性/渐近线/奇异性/任何内容的图形时,是否有任何自动方法可以防止 Matplotlib 在中断"处加入点"?(请参阅下面的代码/图片).
我读到 Sage 有一个看起来不错的 [detect_poles] 工具,但我真的希望它与 Matplotlib 一起使用.

When plotting a graph with a discontinuity/asymptote/singularity/whatever, is there any automatic way to prevent Matplotlib from 'joining the dots' across the 'break'? (please see code/image below).
I read that Sage has a [detect_poles] facility that looked good, but I really want it to work with Matplotlib.

import matplotlib.pyplot as plt 
import numpy as np
from sympy import sympify, lambdify
from sympy.abc import x

fig = plt.figure(1) 
ax = fig.add_subplot(111) 

# set up axis 
ax.spines['left'].set_position('zero') 
ax.spines['right'].set_color('none') 
ax.spines['bottom'].set_position('zero') 
ax.spines['top'].set_color('none') 
ax.xaxis.set_ticks_position('bottom') 
ax.yaxis.set_ticks_position('left') 

# setup x and y ranges and precision
xx = np.arange(-0.5,5.5,0.01) 

# draw my curve 
myfunction=sympify(1/(x-2))
mylambdifiedfunction=lambdify(x,myfunction,'numpy')
ax.plot(xx, mylambdifiedfunction(xx),zorder=100,linewidth=3,color='red') 

#set bounds 
ax.set_xbound(-1,6)
ax.set_ybound(-4,4) 

plt.show()

推荐答案

这可能不是您正在寻找的优雅解决方案,但如果只想要大多数情况下的结果,您可以剪裁"绘制的大值和小值数据分别为+∞-∞.Matplotlib 不绘制这些.当然,您必须小心不要将分辨率设置得太低或剪辑阈值太高.

This may not be the elegant solution you are looking for, but if just want results for most cases, you can "clip" large and small values of your plotted data to +∞ and -∞ respectively. Matplotlib does not plot these. Of course you have to be careful not to make your resolution too low or your clipping threshold too high.

utol = 100.
ltol = -100.
yy = 1/(xx-2)
yy[yy>utol] = np.inf
yy[yy<ltol] = -np.inf

ax.plot(xx, yy, zorder=100, linewidth=3, color='red') 

这篇关于如何使用 Matplotlib 处理渐近线/不连续性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆