适用于sympy 1.0的matplotlib的正确版本是什么?

时间:2017-06-12 16:30:33

标签: python matplotlib sympy

我试图在Pycharm中使用Sympy(1.0)的绘图模块,但遇到如下所示的错误。我想这是由matplotlib(2.0.2)和Sympy(1.0)之间的版本不兼容引起的。有人有线索吗?提前谢谢〜

Traceback (most recent call last):
  File "/home/leizh/PycharmProjects/Learn_python/Smoothness_Bilinear_Quadrilateral_Elmt.py", line 49, in <module>
    plot_parametric(cos(u),sin(u),(u,-5,5))
  File "/home/leizh/.local/lib/python3.5/site-packages/sympy/plotting/plot.py", line 1415, in plot_parametric
    plots.show()
  File "/home/leizh/.local/lib/python3.5/site-packages/sympy/plotting/plot.py", line 184, in show
    self._backend = self.backend(self)
  File "/home/leizh/.local/lib/python3.5/site-packages/sympy/plotting/plot.py", line 1056, in __new__
    return MatplotlibBackend(parent)
  File "/home/leizh/.local/lib/python3.5/site-packages/sympy/plotting/plot.py", line 868, in __init__
    self.cm = self.matplotlib.cm
AttributeError: 'NoneType' object has no attribute 'cm'

该代码用于计算双线性四边形单元的映射。

from sympy import *
from sympy.plotting import *

xi = Symbol("xi")
eta = Symbol("eta")

#Shape functions in reference element
def Ni(xi,eta,i):
    references_vertices = {1:[-1,-1],2:[1,-1],3:[1,1],4:[-1,1]}
    xiv = references_vertices[i][0]
    etav = references_vertices[i][1]
    return Rational(1,4)*(1+xiv*xi)*(1+etav*eta)

#Give a specific element in physical space with an angle >= 180 degree
physical_vertices = {1:[-1,-1],2:[1,-1],3:[1,1],4:[0,0]}

#Interpolation for (x,y) in terms of (xi,eta)
def mapping(xi,eta,vertices):
    x = 0
    y = 0
    for i in vertices:
        xv = vertices[i][0]
        yv = vertices[i][1]
        x += Ni(xi,eta,i)*xv
        y += Ni(xi,eta,i)*yv
    return [x,y]

#mapping (xi, eta) -> (x, y)
xy = mapping(xi,eta,physical_vertices)
print("x and y")
print(factor(xy[0]))
print(factor(xy[1]))

#Jacobian
jac = []
jac.append([xy[0].diff(xi),xy[0].diff(eta)])
jac.append([xy[1].diff(xi),xy[1].diff(eta)])

print("Jacobian Matrix")
print(factor(jac))

#The determinant of Jacobian
det_jac = jac[0][0]*jac[1][1]-jac[0][1]*jac[1][0]
print(factor(det_jac))

#Plot
plot3d_parametric_surface(xy[0], xy[1], det_jac,(xi,-1,1),(eta,-1,1))
det_jac.subs([(xi,1),(eta,-1)])

#test
u = symbols('u')
plot(u**2,(u,-1,1))
plot_parametric(cos(u),sin(u),(u,-5,5))

1 个答案:

答案 0 :(得分:0)

我已经能够用matplotlib 2.0.2,sympy 1.0和python 3.4.6重现你的问题。但是使用matplotlib 2.0.2,sympy 1.0和python 3.5.3工作正常。请注意,我使用的是不同的计算机,但每次都使用新的虚拟环境。所以这里不应该有其他问题。我建议升级到python 3.5.x。

将来请提供一个“最小”的工作示例来重现您的错误,例如:

[DataMember]

编辑:两台计算机之间存在差异:一台使用qt4agg后端(不起作用),另一台使用tkagg(确实有效)。因此,对于使用sympy和matplotlib的后端,似乎存在一个问题。

相关问题