我試圖在Pycharm中使用Sympy(1.0)的繪圖模塊,但遇到如下所示的錯誤。我想這是由matplotlib(2.0.2)和Sympy(1.0)之間的版本不兼容造成的。有人有線索嗎?在此先感謝〜什麼是sympy 1.0的matplotlib的正確版本?
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))
您是否看到此問題? https://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something – parik
使用新的虛擬環境與python 3.5.3,matplotlib 2.0.2和sympy 1.0工作順利如從前。 'sympy.plotting.plot_parametric(sympy.cos(u),sympy.sin(u),(u,-5,5))'在我的屏幕上呈現一個很好的圓圈。也許你遇到了pycharm中的錯誤?我想發佈你的代碼的相關部分,重現你的錯誤將有所幫助。 – Hannebambel
感謝您的幫助。我剛剛添加了原始代碼。通過執行,問題位於文件sympy.plot.py的行:'self.matplotlib = import_module('matplotlib', __import__kwargs = {'fromlist':['pyplot','cm','collections ']}, min_module_version ='1.1.0',catch =(RuntimeError,))'。它無法導入模塊matplotlib。所以我明白爲什麼它會產生錯誤,但仍然不知道如何解決它。 – LEI1ZH