sympy + matplotlib不绘制几何图元

时间:2018-01-30 20:12:22

标签: python matplotlib ipython sympy

我正试图在sympy documentation之后在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

现在,当我在输入行输入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'

1 个答案:

答案 0 :(得分:1)

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