gpt4 book ai didi

python - sympy+matplotlib 不绘制几何图元

转载 作者:太空宇宙 更新时间:2023-11-03 11:18:03 25 4
gpt4 key购买 nike

我正在尝试在 ipython 中绘制一些几何基元(线、圆、椭圆),遵循 sympy documentation .

我在 CoCalc 上使用 Sympy 1.1.1 版和 python 3.5系统,但可以在我自己本地安装的 ipython 和 sympy(2.7 和 0.7.6)上重现相同的结果。这是我的代码:

from sympy import Point, Circle
from sympy.plotting.plot import Plot
p = Plot(axes='label_axes=True')
c = Circle(Point(0,0), 1)
p[0] = c

现在,当我在输入行输入 p 时,ipython 只返回对象但不绘制它:

In[2] = p
Out[2] = <sympy.plotting.plot.Plot at 0x7f2e64fe62e8>

plot 命令按预期工作,因此 matplotlib 后端正常运行。

当我输入 p.show() 时,出现以下错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-02f33f5f7a8f> in <module>()
----> 1 p.show()

/ext/anaconda3/lib/python3.5/site-packages/sympy/plotting/plot.py in show(self)
194 self._backend.close()
195 self._backend = self.backend(self)
--> 196 self._backend.show()
197
198 def save(self, path):
/ext/anaconda3/lib/python3.5/site-packages/sympy/plotting/plot.py in show(self)
1027
1028 def show(self):
-> 1029 self.process_series()
1030 #TODO after fixing https://github.com/ipython/ipython/issues/1255
1031 # you can uncomment the next line and remove the pyplot.show() call
/ext/anaconda3/lib/python3.5/site-packages/sympy/plotting/plot.py in process_series(self)
989 starts = [s.start for s in parent._series]
990 ends = [s.end for s in parent._series]
--> 991 self.ax.set_xlim(min(starts), max(ends))
992 if parent.ylim:
993 self.ax.set_ylim(parent.ylim)
ValueError: min() arg is an empty sequence

它显示空的坐标轴集(没有圆圈)。此外,p.save("foo.png") 会产生类似的错误。

我也尝试过 p = Plot(Circle((0,0),1)(虽然这不是文档建议的),但再次没有显示任何内容,p.show() 产生另一个长错误:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-f1f858abbdb7> in <module>()
1 p = Plot(Circle((0,0),1))
----> 2 p.show()

/ext/anaconda3/lib/python3.5/site-packages/sympy/plotting/plot.py in show(self)
193 if hasattr(self, '_backend'):
194 self._backend.close()
--> 195 self._backend = self.backend(self)
196 self._backend.show()
197
/ext/anaconda3/lib/python3.5/site-packages/sympy/plotting/plot.py in __new__(cls, parent)
1065 matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,))
1066 if matplotlib:
-> 1067 return MatplotlibBackend(parent)
1068 else:
1069 return TextBackend(parent)
/ext/anaconda3/lib/python3.5/site-packages/sympy/plotting/plot.py in __init__(self, parent)
872 def __init__(self, parent):
873 super(MatplotlibBackend, self).__init__(parent)
--> 874 are_3D = [s.is_3D for s in self.parent._series]
875 self.matplotlib = import_module('matplotlib',
876 __import__kwargs={'fromlist': ['pyplot', 'cm', 'collections']},
/ext/anaconda3/lib/python3.5/site-packages/sympy/plotting/plot.py in <listcomp>(.0)
872 def __init__(self, parent):
873 super(MatplotlibBackend, self).__init__(parent)
--> 874 are_3D = [s.is_3D for s in self.parent._series]
875 self.matplotlib = import_module('matplotlib',
876 __import__kwargs={'fromlist': ['pyplot', 'cm', 'collections']},
AttributeError: 'Circle' object has no attribute 'is_3D'

最佳答案

这些示例来自“pygletplot”模块,该模块基本上不起作用(当然不在 Python 3 下),并且多年未经过测试或开发。 SymPy 可以绘制什么:

  • 图表 y=f(x),带有 plot
  • 参数化曲线,带有plot_parametric
  • 隐式曲线,带有 plot_implicit
  • 3D 图形 z=f(x, y),使用 plot3d
  • 3D 参数曲线,使用 plot3d_parametric_line
  • 参数化曲面,带有 plot3d_parametric_surface

关于python - sympy+matplotlib 不绘制几何图元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com