《CFDPython Learning Notes》Matplotlib: errors and solutions

When I learnt the CFD lesson ” 12 Steps to Navier-Stokes” published by Prof.Loren Barbara, I met some errors of Matplotlib, so I gathered them together. (Big thanks to Prof.Loren Barbara)


2018/6/1 AttributeError: Unknown property cstrid

In Step 6: Nonlinear convection in 2D, there are several lines of codes as below:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot, cm
import numpy
% matplotlib inline

### variable declarations
nx = 101
ny = 101
nt = 80
c = 1
dx = 2 / (nx - 1)
dy = 2 / (ny - 1)
sigma = .2
dt =sigma * dx

x = numpy.linspace(0, 2, nx)
y = numpy.linspace(0, 2, ny)

u = numpy.ones((ny, nx)) 
v = numpy.ones((ny, nx))
un = numpy.ones((ny, nx))
vn = numpy.ones((ny, nx))

###Assign initial conditions
###variable declarations
nx = 101
ny = 101
nt = 80
c = 1
dx = 2 / (nx - 1)
dy = 2 / (ny - 1)
sigma = .2
dt = sigma * dx

x = numpy.linspace(0, 2, nx)
y = numpy.linspace(0, 2, ny)

u = numpy.ones((ny, nx)) ##create a 1xn vector of 1's
v = numpy.ones((ny, nx))
un = numpy.ones((ny, nx))
vn = numpy.ones((ny, nx))

###Assign initial conditions
##set hat function I.C. : u(.5<=x<=1 && .5<=y<=1 ) is 2
u[int(.5 / dy):int(1 / dy + 1), int(.5 / dx):int(1 / dx + 1)] = 2
##set hat function I.C : u(.5<=x<=1 && .5<=y<=1 ) is 2
v[int(.5 / dy):int(1 /  dy + 1), int(.5 / dx):int(1 / dx + 1)] = 2

fig = pyplot.figure(figsize = (11, 7), dpi = 100)
ax  = fig.gca(projection = '3d')
X, Y = numpy.meshgrid(x, y)

ax.plot_surface(X, Y, u, cmap=cm.viridis, rstride=2, cstrid=2)
ax.set_xlabel('$x$')
ax.set_ylabel('$y$');

Then after I ran the jupyter notebook, the codes gave me the * AttributeError*:

AttributeError                            Traceback (most recent call last)
<ipython-input-10-40ce08930940> in <module>()
      3 X, Y = numpy.meshgrid(x, y)
      4 
----> 5 ax.plot_surface(X, Y, u, cmap=cm.viridis, rstride=2, cstrid=2)
      6 ax.set_xlabel('$x$')
      7 ax.set_ylabel('$y$');

D:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in plot_surface(self, X, Y, Z, *args, **kwargs)
   1720             normals = []
   1721 
-> 1722         polyc = art3d.Poly3DCollection(polys, *args, **kwargs)
   1723 
   1724         if fcolors is not None:

D:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\art3d.py in __init__(self, verts, *args, **kwargs)
    518         '''
    519         zsort = kwargs.pop('zsort', True)
--> 520         PolyCollection.__init__(self, verts, *args, **kwargs)
    521         self.set_zsort(zsort)
    522         self._codes3d = None

D:\Anaconda\lib\site-packages\matplotlib\collections.py in __init__(self, verts, sizes, closed, **kwargs)
    910         %(Collection)s
    911         """
--> 912         Collection.__init__(self, **kwargs)
    913         self.set_sizes(sizes)
    914         self.set_verts(verts, closed)

D:\Anaconda\lib\site-packages\matplotlib\collections.py in __init__(self, edgecolors, facecolors, linewidths, linestyles, antialiaseds, offsets, transOffset, norm, cmap, pickradius, hatch, urls, offset_position, zorder, **kwargs)
    158 
    159         self._path_effects = None
--> 160         self.update(kwargs)
    161         self._paths = None
    162 

D:\Anaconda\lib\site-packages\matplotlib\artist.py in update(self, props)
    900         try:
    901             ret = [_update_property(self, k, v)
--> 902                    for k, v in props.items()]
    903         finally:
    904             self.eventson = store

D:\Anaconda\lib\site-packages\matplotlib\artist.py in <listcomp>(.0)
    900         try:
    901             ret = [_update_property(self, k, v)
--> 902                    for k, v in props.items()]
    903         finally:
    904             self.eventson = store

D:\Anaconda\lib\site-packages\matplotlib\artist.py in _update_property(self, k, v)
    893                 func = getattr(self, 'set_' + k, None)
    894                 if not callable(func):
--> 895                     raise AttributeError('Unknown property %s' % k)
    896                 return func(v)
    897 

AttributeError: Unknown property cstrid

Finally, I found that I typed the “cstride” mistakenly. It should be “cstride” rather than “cstrid”.
Be careful when typing the codes.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值