sympy+matplotlib 不绘制几何图元

JHo*_*and 5 python matplotlib sympy ipython

我正在尝试按照sympy 文档在 ipython(线、圆、椭圆)中绘制一些几何图元。

我在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
Run Code Online (Sandbox Code Playgroud)

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

In[2] = p
Out[2] = <sympy.plotting.plot.Plot at 0x7f2e64fe62e8>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

它显示和空的坐标轴集(没有圆圈)。此外,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'
Run Code Online (Sandbox Code Playgroud)

小智 3

这些示例来自“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