首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >matplotlib 0.99 Mplot3d AttributeError:'list‘对象没有属性'ndim’

matplotlib 0.99 Mplot3d AttributeError:'list‘对象没有属性'ndim’
EN

Stack Overflow用户
提问于 2018-05-15 16:29:03
回答 1查看 9.5K关注 0票数 3

我试着在这里绘制一个像这样的3d图:

使用此代码并接收一个错误,该错误如下:

代码语言:javascript
运行
复制
line 1621, in plot_surface
if Z.ndim != 2:
AttributeError: 'list' object has no attribute 'ndim'
代码语言:javascript
运行
复制
import cobra
import os
from os.path import join
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
data_dir ='/Users/stephenchapman/Documents/research/FBA_algae_digesate/COBRApy/iCZ843/iCZ843_models'
model = cobra.io.read_sbml_model(join(data_dir, "iCZ843_hetero.xml"))
model.reactions[15].upper_bound = -0 #ammonia
model.reactions[15].lower_bound = -98.3
model.reactions[27].upper_bound = -0 #acetate
model.reactions[27].lower_bound = -3.3
model.reactions[14].upper_bound = -0 #phosphate
model.reactions[14].lower_bound = -10
model.reactions[16].upper_bound = -0 #nitrate
model.reactions[16].lower_bound = -30.3
model.reactions[20].upper_bound = -0 #magnesium
model.reactions[20].lower_bound = -0.56
model.reactions[18].upper_bound = -0 #iron
model.reactions[18].lower_bound = -2.16
model.objective = model.reactions[63]
solution = model.optimize()
model.summary()

ac_uptake = []
NH4_uptake = []
growth_rate = []
for i in range(0,85,5):
    model.reactions[27].lower_bound = -i #acetate uptake
    model.reactions[27].upper_bound = -i
    for j in (0,80,5):
        model.reactions[15].lower_bound = -i #NH4 uptake
        model.reactions[15].upper_bound = -i
        print(solution.f)
        growth_rate.append(solution.f)
        ac_uptake.append(model.reactions[27].lower_bound)
        NH4_uptake.append(model.reactions[15].lower_bound)
        plot_ac_uptake = list(range(0,85,5))
        plot_NH4_uptake = list(range(0,85,5))

X = (plot_ac_uptake)
Y = (plot_NH4_uptake)
X, Y = np.meshgrid(X, Y)
Z = (growth_rate)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.viridis)
plt.show()

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2018-05-15 17:01:08

错误信息

代码语言:javascript
运行
复制
Z.ndim != 2: 
AttributeError: 'list' object has no attribute 'ndim'

表示列表Z没有属性ndim。然而,ndim是一个NumPy数组的属性。因此,将Z转换为NumPy数组很可能会解决这个问题(或者至少让您通过AttributeError):

代码语言:javascript
运行
复制
Z = np.array(Z)

例如,

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

X = list(np.linspace(-4, 4, 100))
Y = list(np.linspace(-4, 4, 100))
X, Y = np.meshgrid(X, Y)
Z = np.sin((X**2 + Y**2)/4)
# Z = (np.sin((X**2 + Y**2)/4)).tolist()  
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.viridis)
plt.show()

工作正常,但如果取消注释行

代码语言:javascript
运行
复制
Z = (np.sin((X**2 + Y**2)/4)).tolist()  

这使Z成为一个列表列表,然后得到错误消息。

代码语言:javascript
运行
复制
Z.ndim != 2: 
AttributeError: 'list' object has no attribute 'ndim'
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50355133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档