如何在sympy中的绘图上显示网格

问题描述 投票:0回答:3

我想使用 Sympy 显示带有绘图的网格:

import sympy
from sympy import sin
from sympy.abc import x
from math import pi
sympy.plot(sin(x),xlim=(0,2*pi))

使用 matplotlib 可以直接添加网格:

import matplotlib.pylab as plt
plt.grid(True)
plt.show()

如何使用 Sympy 执行此操作?

python python-3.x sympy
3个回答
4
投票

由于 SymPy 在底层使用 matplotlib(如果有的话), 你可以使用:

from matplotlib import style
style.use('ggplot')

这将为你的 sympy.plot() 设计风格

要打印所有可用样式,请使用:

import matplotlib as plt
print(plt.style.available)

这里有关于 matplotlib 的样式的链接


2
投票

请添加此内容。

import seaborn as sns
sns.set()
sns.set_style("whitegrid", {'grid.linestyle': '--'})

你可以得到这样的无花果:

import sympy
from sympy import sin
from sympy.abc import x
from math import pi
import seaborn as sns
sns.set()
sns.set_style("whitegrid", {'grid.linestyle': '--'})

sympy.plot(sin(x),xlim=(0,2*pi))

0
投票

我使用了与@mauricio777相同的解决方案。我发现显示/可视化样式的网站非常有用: https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html

我个人更喜欢使用:

from matplotlib import style
style.use('seaborn-v0_8-whitegrid')
© www.soinside.com 2019 - 2024. All rights reserved.