matplotlib 如何处理渐近线/不连续

rhfm7lfc  于 2023-10-24  发布在  其他
关注(0)|答案(4)|浏览(118)

当绘制一个具有不连续性/渐近线/奇点/任何东西的图形时,是否有任何自动方法可以防止Matplotlib在“中断”处“连接点”?(请参见下面的代码/图像)。
我读到Sage有一个看起来不错的[detect_poles]工具,但我真的希望它能与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()

h79rfbju

h79rfbju1#

通过使用masked arrays,可以避免绘制曲线的选定区域。
要消除x=2处的奇点:

import matplotlib.numerix.ma as M    # for older versions, prior to .98
#import numpy.ma as M                # for newer versions of matplotlib
from pylab import *

figure()

xx = np.arange(-0.5,5.5,0.01) 
vals = 1/(xx-2)        
vals = M.array(vals)
mvals = M.masked_where(xx==2, vals)

subplot(121)
plot(xx, mvals, linewidth=3, color='red') 
xlim(-1,6)
ylim(-5,5)

这条简单的曲线可能会更清楚地说明哪些点被排除在外:

xx = np.arange(0,6,.2) 
vals = M.array(xx)
mvals = M.masked_where(vals%2==0, vals)
subplot(122)
plot(xx, mvals, color='b', linewidth=3)
plot(xx, vals, 'rx')
show()

rt4zxlrg

rt4zxlrg2#

这可能不是你正在寻找的优雅的解决方案,但是如果你只是想在大多数情况下得到结果,你可以将你绘制的数据的大值和小值分别“剪切”到+∞-∞。Matplotlib不会绘制这些。当然,你必须小心不要让你的分辨率太低或者你的剪切阈值太高。

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')
blpfk2vs

blpfk2vs3#

不,我认为没有内置的方法来告诉matplotlib忽略这些点。毕竟,它只是连接点,对函数或点之间发生的事情一无所知。
然而,你可以使用sympy来找到极点,然后将函数的连续部分拼接在一起。下面是一些公认的丑陋代码,它确实做到了这一点:

from pylab import *
from sympy import solve
from sympy.abc import x
from sympy.functions.elementary.complexes import im

xmin = -0.5
xmax = 5.5
xstep = 0.01

# solve for 1/f(x)=0 -- we will have poles there
discontinuities = sort(solve(1/(1/(x-2)),x))

# pieces from xmin to last discontinuity
last_b = xmin
for b in discontinuities:
    # check that this discontinuity is inside our range, also make sure it's real
    if b<last_b or b>xmax or im(b):
      continue
    xi = np.arange(last_b, b, xstep)
    plot(xi, 1./(xi-2),'r-')
    last_b = b

# from last discontinuity to xmax
xi = np.arange(last_b, xmax, xstep)
plot(xi, 1./(xi-2),'r-')

xlim(xmin, xmax)
ylim(-4,4)
show()

qni6mghb

qni6mghb4#

我也有同样的问题,我的解决方案是把X分成两个不同的区间:一个在奇点之前,另一个在奇点之后,这张图把同一张图上的曲线分开。

相关问题