繁体   English   中英

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

[英]Python, Axes3D, plotting: Cannot append to List of X,Y,Z values in my 3D scatterplot

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

请注意,在这种情况下,目的只是为了能够看到我的情节上的点。 他们的位置根本没有关系。

如果我没有弄错的话,你会在创造一个世界时绘图(例如, earth=world() )。 plt.show()看起来根本不使用列表。 将所有绘图代码移动到showPlot方法,或将数据作为参数提供给__init__

请注意,plt.show(),plt.title()等隐含地使用“当前”轴; 这可能是其他地方不同的情节。 例如,使用self.ax.set_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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM