如何处理渐近线/不连续点
当绘制具有不连续性/渐近线/奇异性/任何东西的图形时,是否有任何自动方法可以防止 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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过使用屏蔽数组,您可以避免绘制曲线的选定区域。
要删除 x=2 处的奇点:
这条简单的曲线可能会更清楚地显示哪些点被排除:
< img src="https://i.sstatic.net/DvJZy.png" alt="在此处输入图像描述">
By using masked arrays you can avoid plotting selected regions of a curve.
To remove the singularity at x=2:
This simple curve might be a bit more clear on which points are excluded:
这可能不是您正在寻找的优雅解决方案,但如果只想要大多数情况下的结果,您可以将绘制数据的大值和小值“剪辑”为
+∞
和-∞分别。 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.不,我认为没有内置方法可以告诉 matplotlib 忽略这些
点。毕竟它只是连接点,对函数一无所知
或者两点之间发生了什么。
但是,您可以使用 sympy 来查找极点,然后将函数的连续部分修补在一起。这里有一些公认的丑陋代码,正是这样做的:
No, I think there is no built-in way to tell
matplotlib
to ignore thesepoints. After all, it just connects points and knows nothing about functions
or what happens in between the points.
However, you can use
sympy
to find the poles, and then patch the continuous pieces of your function together. Here some admittedly ugly code that does exactly that:有同样的问题。
我的解决方案是将 X 分成两个不同的区间:一个在奇点之前,另一个在奇点之后。在同一图上绘制单独的曲线。
Had the same issue.
The solution for me was to separate X into two different intervals: one before and the other after the singularity. The plot separate curves on the same plot.