4

I'm trying to plot a 3d graph like this one here:

enter image description here

Using this code and receiving an error that says:

line 1621, in plot_surface
if Z.ndim != 2:
AttributeError: 'list' object has no attribute 'ndim'

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()

Can anyone help me out please?

1 Answer 1

5

The error message

Z.ndim != 2: 
AttributeError: 'list' object has no attribute 'ndim'

is saying that the list Z has no attribute ndim. ndim is an attribute of NumPy arrays, however. So it is likely that converting Z to a NumPy array will fix the problem (or at least get you past the AttributeError):

Z = np.array(Z)

For example,

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()

works fine, but if you uncomment the line

Z = (np.sin((X**2 + Y**2)/4)).tolist()  

which makes Z a list-of-lists, then you get the error message

Z.ndim != 2: 
AttributeError: 'list' object has no attribute 'ndim'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.