如何阻止matplotlib在垂直渐近线处不正确地添加线条?

huangapple go评论54阅读模式
英文:

How to stop matplotlib from incorrectly adding lines at vertical asymptotes?

问题

以下是代码中要翻译的部分:

For functions that contain vertical asymptotes, y^2 = x^3/(2(3) - x) for example, matplotlib will add in [vertical lines][1]  at the asymptotes which majorly  throws off the [expected][2] graph. I am using plot_implicit() from sympy. I have tried setting the adaptive attribute to True which removes the lines but also removes the styling, and for some reason makes the plot line thickness vary and look really [warped][3]. I would like to keep adaptive=False. For reasons specific to my application I need to use plot_implicit() and can't use contour() or plot() or something else.

from matplotlib.colors import ListedColormap
from sympy import *
from sympy.parsing.sympy_parser import *
import spb

qVal = "y**2 = x**3/(2*(3) - x)"
x, y = symbols('x y')

cmap = ListedColormap(["teal"], "#ffffff00")

lhs, rhs = qVal.split('=')
lhs = eval(lhs)
rhs = eval(rhs)

plot = spb.plot_implicit(Eq(lhs, rhs), (x, -10, 10), (y, -5, 5), adaptive=False, show=True, rendering_kw={"cmap": cmap}, aspect='equal')

请注意,代码的翻译是针对代码部分的内容。

英文:

For functions that contain vertical asymptotes, y^2 = x^3/(2(3) - x) for example, matplotlib will add in vertical lines at the asymptotes which majorly throws off the expected graph. I am using plot_implicit() from sympy. I have tried setting the adaptive attribute to True which removes the lines but also removes the styling, and for some reason makes the plot line thickness vary and look really warped. I would like to keep adaptive=False. For reasons specific to my application I need to use plot_implicit() and can't use contour() or plot() or something else.

from matplotlib.colors import ListedColormap
from sympy import *
from sympy.parsing.sympy_parser import *
import spb


qVal = "y**2 = x**3/(2*(3) - x)"
x, y = symbols('x y')



cmap = ListedColormap(["teal"], "#ffffff00")

lhs,rhs = qVal.split('=')
lhs = eval(lhs)
rhs = eval(rhs)


plot = spb.plot_implicit(Eq(lhs, rhs),  (x, -10, 10), (
y,-5, 5), adaptive=False,show=True, rendering_kw= {"cmap": cmap}, aspect='equal')

答案1

得分: 0

plot_implicit 使用 adaptive=False 采用了一种简单的网格方法来评估函数,并使用了 matplotlib 的轮廓特性来绘制零水平轮廓线。这里的 "simple" 意味着它不应用任何后处理。你可以查看函数的 3D 绘图,它也采用了网格方法,以理解正在发生的事情:

expr = lhs - rhs
spb.plot3d(expr, (x, -10, 10), (y, -5, 5), backend=spb.PB, wireframe=True)

由于在评估的数据上没有后处理,绘图将连接渐近线的两侧。当你获取零水平轮廓时,你会得到那条垂直线。不幸的是,目前还没有为网格方法实现渐近线检测。

你可以安装模块的新版本,以使用 plot_implicit 和自定义样式,例如 adaptive=True

plot = spb.plot_implicit(Eq(lhs, rhs),  (x, -10, 10), (y, -5, 5), aspect='equal',
                         adaptive=True, rendering_kw={"color": "r"})

正如你注意到的,自适应算法倾向于创建不均匀的线条或区域。我无法解决这个问题。

一个更好的方法是对你的符号方程应用一些后处理。例如:

expr = lhs - rhs
sols = solve(expr, y)
spb.plot(*sols, (x, -10, 10), {"color": "b"},
         legend=False, aspect="equal", ylim=(-5, 5))
英文:

plot_implici with adaptive=False uses a simple meshgrid approach to evaluate a function, and it uses a matplotlib's contour feature to plot the zero-level contour. With "simple" I mean that it doesn't apply any post-processing. You can look at a 3D plot of your function, which also uses the meshgrid approach, to understand what's happening:

expr = lhs - rhs
spb.plot3d(expr, (x, -10, 10), (y,-5, 5), backend=spb.PB, wireframe=True)

如何阻止matplotlib在垂直渐近线处不正确地添加线条?

Because there is no post-processing on the evaluated data, the plot connects both sides of an asymptote. When you take the zero-level contour, you'll have that vertical line. Sadly, an asymptote detection is currently not implemented for a meshgrid approach.

You can install the newer release of the module to use plot_implicit with adaptive=True and custom styling:

plot = spb.plot_implicit(Eq(lhs, rhs),  (x, -10, 10), (y,-5, 5), aspect='equal',
                         adaptive=True, rendering_kw={"color": "r"})

如何阻止matplotlib在垂直渐近线处不正确地添加线条?

As you noticed, the adaptive algorithm tends to create uneven lines or regions. There is nothing I can do to fix it.

A better approach would be to apply some post-processing to your symbolic equation. For example:

expr = lhs - rhs
sols = solve(expr, y)
spb.plot(*sols, (x, -10, 10), {"color": "b"},
         legend=False, aspect="equal", ylim=(-5, 5))

如何阻止matplotlib在垂直渐近线处不正确地添加线条?

huangapple
  • 本文由 发表于 2023年3月31日 04:49:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定