如何将在 main 中创建的实例与另一个模块中的 class 共享
How to share an instance created in main with a class in another module
我对如何在模块之间共享 class 实例有疑问。下面不是实际的代码,它是我正在尝试做的事情的简化表示。有问题的变量是 thePlot。如果我将其设为全局,则会出现 'global name not defined' 类型错误。如果我不使用全局,那么 shapes 模块中的 'draw' 方法找不到它。
调用 draw() 方法并尝试执行 'thePlot' 方法时出现错误 'plot'。
* Main module *****************
import matplotlib as plt
import plotter
import shapes
main():
thePlot = plotter.plotter()
cyl = shapes.cylinder(r, c, n, color)
cyl.draw()
plt.show()
* shapes module *******************
import main
import plotter
class shapes(self):
def __init__(self):
pass
def cylinder(r, c, n, color):
self.r = r
self.c = c
self.n = n
self.color = color
def draw(self):
self.x = calculate list of x coordinates
self.y = calculate list of y coordinates
self.z = calculate list of z coordinates
global thePlot
* This line causes the error
thePlot.plot(self.x, self.y, self.z, self.color)
* plotter module ******************
import matplotlib as plt
class plotter(self):
def __init__(self):
self.fig = plt.figure()
self.ax = fig.add_subplot(111, projection='3d')
def plot(self, x, y, z, color):
self.ax.plot_wireframe(x, y, z, color)
* Main module *****************
import matplotlib as plt
import plotter
import shapes
def main():
global thePlot
thePlot = plotter.plotter()
cyl = shapes.cylinder(r, c, n, color)
cyl.draw()
plt.show()
还有这个:
global thePlot
* This line causes the error
thePlot.plot(self.x, self.y, self.z, self.color)
应该变成:
main.thePlot.plot(self.x, self.y, self.z, self.color)
条件是之前调用了main.main()
我对matplotlib不熟悉,不过貌似也有类似的问题Add cylinder to plot。这个改编代码怎么样?
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
"""
def cyl():
x = None
y = None
z = None
return
"""
def cyl():
x=np.linspace(-1, 1, 100)
z=np.linspace(-2, 2, 100)
Xc, Zc=np.meshgrid(x, z)
Yc = np.sqrt(1-Xc**2)
return Xc, Yc, Zc
Xc, Yc, Zc = cyl()
#draw parameters
rstride = 20
cstride = 10
ax.plot_surface(Xc, Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)
ax.plot_surface(Xc, -Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()
我对如何在模块之间共享 class 实例有疑问。下面不是实际的代码,它是我正在尝试做的事情的简化表示。有问题的变量是 thePlot。如果我将其设为全局,则会出现 'global name not defined' 类型错误。如果我不使用全局,那么 shapes 模块中的 'draw' 方法找不到它。
调用 draw() 方法并尝试执行 'thePlot' 方法时出现错误 'plot'。
* Main module *****************
import matplotlib as plt
import plotter
import shapes
main():
thePlot = plotter.plotter()
cyl = shapes.cylinder(r, c, n, color)
cyl.draw()
plt.show()
* shapes module *******************
import main
import plotter
class shapes(self):
def __init__(self):
pass
def cylinder(r, c, n, color):
self.r = r
self.c = c
self.n = n
self.color = color
def draw(self):
self.x = calculate list of x coordinates
self.y = calculate list of y coordinates
self.z = calculate list of z coordinates
global thePlot
* This line causes the error
thePlot.plot(self.x, self.y, self.z, self.color)
* plotter module ******************
import matplotlib as plt
class plotter(self):
def __init__(self):
self.fig = plt.figure()
self.ax = fig.add_subplot(111, projection='3d')
def plot(self, x, y, z, color):
self.ax.plot_wireframe(x, y, z, color)
* Main module *****************
import matplotlib as plt
import plotter
import shapes
def main():
global thePlot
thePlot = plotter.plotter()
cyl = shapes.cylinder(r, c, n, color)
cyl.draw()
plt.show()
还有这个:
global thePlot
* This line causes the error
thePlot.plot(self.x, self.y, self.z, self.color)
应该变成:
main.thePlot.plot(self.x, self.y, self.z, self.color)
条件是之前调用了main.main()
我对matplotlib不熟悉,不过貌似也有类似的问题Add cylinder to plot。这个改编代码怎么样?
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
"""
def cyl():
x = None
y = None
z = None
return
"""
def cyl():
x=np.linspace(-1, 1, 100)
z=np.linspace(-2, 2, 100)
Xc, Zc=np.meshgrid(x, z)
Yc = np.sqrt(1-Xc**2)
return Xc, Yc, Zc
Xc, Yc, Zc = cyl()
#draw parameters
rstride = 20
cstride = 10
ax.plot_surface(Xc, Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)
ax.plot_surface(Xc, -Yc, Zc, alpha=0.2, rstride=rstride, cstride=cstride)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()