Python、Axes3D、plotting:无法附加到我的3D散点图中的X、Y、Z值列表

Python、Axes3D、plotting:无法附加到我的3D散点图中的X、Y、Z值列表,python,axes,Python,Axes,(对python来说非常新)所以我制作了一个3D散点图,我想在其中添加不同“类型”的点。目前,我想将值附加到X1、Y1、Z1,以创建有问题的点。如果我手工输入,似乎没有任何问题。但是,当我尝试在类之外使用append函数时,点不会显示在绘图上。我希望能够在我的散点图上附加或添加点 import numpy as np import math from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt font

(对python来说非常新)所以我制作了一个3D散点图,我想在其中添加不同“类型”的点。目前,我想将值附加到X1、Y1、Z1,以创建有问题的点。如果我手工输入,似乎没有任何问题。但是,当我尝试在类之外使用append函数时,点不会显示在绘图上。我希望能够在我的散点图上附加或添加点

import numpy as np
import math
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

font = {'fontname': 'Franklin Gothic Demi'}
title = {'fontname': 'Bookman Old Style'}

diameter = 3500
width = 200


class world(object):


    def __init__(self):


        self.fig = plt.figure()
        self.fig.add_subplot(111, projection="3d")
        self.ax = plt.gca()

        self.t = 0

        self.X1 = []
        self.Y1 = []
        self.Z1 = []

        self.ax.scatter(self.X1, self.Y1, self.Z1, c="r", marker="1", depthshade=True)

        self.ax.set_xlabel("synaptic diameter (nm)", color = "navy", **font)
        self.ax.set_ylabel("synaptic diameter (nm)", color = "navy", **font)
        self.ax.set_zlabel("cleft width (nm)", color = "navy", **font)

        self.ax.autoscale(enable=True)

        self.ax.set_xlim(0, diameter)
        self.ax.set_ylim(0, diameter)
        self.ax.set_zlim(0, width)


        plt.gca().invert_xaxis()

        plt.title("Synapse", color = "black", size = 20, **title)


        self.Serotonin = []
        self.MAO = []
        self.immobileAgents = []
        #not yet relevant here

    def showPlot(self):

        self.showPlot = plt.show()
下面是我用来将值附加到列表中的函数之一

world = world()

tee = 0
while tee <= 100:

    world.X1.append(tee)
    world.Y1.append(tee)
    world.Z1.append(tee)

    tee = tee + 1

print (world.X1)
print (world.Y1)
print (world.Z1)
#just to be sure

world.showPlot()
world=world()
T=0

如果我没弄错的话,当你创建一个世界(例如,
earth=world()。
plt.show()。将所有绘图代码移动到
showPlot
方法,或将数据作为参数提供给
\uuuu init\uuuu

请注意,plt.show()、plt.title()等隐式使用“当前”轴;这可能是其他地方的另一个阴谋。例如使用
self.ax.set\u title

根据您对
world
所做的尝试,最好

def __init__(self, X, Y, Z):
    self.X, self.Y, self.Z = X, Y, Z

def createPlot(self):
    self.fig = plt.figure()
    ax = self.fig.add_subplot(111, projection="3d")
    ... # do the plotting
    return ax
并用于:

earth = world(X, Y, Z)
earth.createPlot()
plt.show()

如果我没弄错的话,你可以在创建世界时绘图(例如,
earth=world()
)。
plt.show()。将所有绘图代码移动到
showPlot
方法,或将数据作为参数提供给
\uuuu init\uuuu

请注意,plt.show()、plt.title()等隐式使用“当前”轴;这可能是其他地方的另一个阴谋。例如使用
self.ax.set\u title

根据您对
world
所做的尝试,最好

def __init__(self, X, Y, Z):
    self.X, self.Y, self.Z = X, Y, Z

def createPlot(self):
    self.fig = plt.figure()
    ax = self.fig.add_subplot(111, projection="3d")
    ... # do the plotting
    return ax
并用于:

earth = world(X, Y, Z)
earth.createPlot()
plt.show()

非常感谢!我修改了我的代码,在init部分包含了所有的X、Y、Z值,以及与特定定义函数相关的所有绘图,它确实工作了!非常感谢!我修改了我的代码,在init部分包含了所有的X、Y、Z值,以及与特定定义函数相关的所有绘图,它确实工作了!