我怎样才能画更多的线?(Python交响乐团)

2024-04-24 11:05:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当我打字的时候

from sympy import *
from sympy.plotting import *
from sympy.plotting.plot import *


x, y = symbols('x y')
f = Function('f')
g = Function('g')

f = 1/((x+0.3)**2 + y**2) - 1/((x-0.3)**2 + y**2 )
g = (x+0.3)/sqrt((x+0.3)**2 + y**2) - (x-0.3)/sqrt((x-0.3)**2 + y**2)


p0 = Plot(ContourSeries(f,(x,-1.5,1.5),(y,-1.5,1.5)))
p1 = Plot(ContourSeries(g,(x,-1.5,1.5),(y,-1.5,1.5)))

p0.show()
p1.show()

enter image description here

enter image description here

p0如第一张图所示。线路数量少。在

我想画更多的线像第二幅画。在

解决办法是什么?在


Tags: fromimport数量plotshowfunctionsqrtplotting
1条回答
网友
1楼 · 发布于 2024-04-24 11:05:17

ContourSeries类没有公开正确的信息来实现这一点,但它很容易扩展。我调用了参数levels,直接传递给matplotlib。在

class MyContourSeries(ContourSeries):

    def __init__(self, expr, var_start_end_x, var_start_end_y, **kwargs):
        super(MyContourSeries, self).__init__(expr, var_start_end_x, var_start_end_y)
        self.nb_of_points_x = kwargs.get('nb_of_points_x', 50)
        self.nb_of_points_y = kwargs.get('nb_of_points_y', 50)
        self.levels = kwargs.get('levels', 5)

    def get_meshes(self):
        mesh_x, mesh_y, f = super().get_meshes()
        return (mesh_x, mesh_y, f, self.levels)

第1.1.1条和第3.1条至少应该适用。Sympy plotting正在为此系列使用matplotlibs countour(https://github.com/sympy/sympy/blob/master/sympy/plotting/plot.py#L909

^{pr2}$

哪个签名是matplotlib.pyplot.contour([X, Y,] Z, [levels], **kwargs)

与matplotlib contour函数一样,可以使用固定数量的级别

p0 = Plot(MyContourSeries(f, (x, -1.5, 1.5), (y, -1.5, 1.5),
                      nb_of_points_x=50, nb_of_points_y=50, levels=100))
p0.show()

number of levels

或者直接过关

p0 = Plot(MyContourSeries(f, (x, -1.5, 1.5), (y, -1.5, 1.5),
                      nb_of_points_x=50, nb_of_points_y=50, levels=np.arange(-50,50)))
p0.show()

passing a range

相关问题 更多 >