简体   繁体   English

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

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

(Very new to python) So I've made a 3D scatterplot to which I would like to add dots of different "types". (对python来说很新)所以我制作了一个3D散点图,我想添加不同“类型”的点。 At the moment I would like to append values to X1, Y1, Z1 to create the points in question. 目前我想将值附加到X1,Y1,Z1以创建有问题的点。 If I type those in by hand, there doesn't seem to be any issue. 如果我手动输入,那似乎没有任何问题。 However, when I try to use the append function outside of the class, the dots do not show on my plot. 但是,当我尝试在类之外使用append函数时,点不会显示在我的绘图上。 I would like to be able to externally append or add points on my scatterplot. 我希望能够在我的散点图上外部添加或添加点。

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

Here is (one of) the function(s) I used to append values to the lists 这是我用于将值附加到列表的函数(之一)

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

Note that in this case, the prupose is merely to be able to see the dots on my plot. 请注意,在这种情况下,目的只是为了能够看到我的情节上的点。 Their placement doesn't matter at all yet. 他们的位置根本没有关系。

If I'm not mistaken, you plot when you create a world (eg. earth=world() ). 如果我没有弄错的话,你会在创造一个世界时绘图(例如, earth=world() )。 The plt.show() doesn't look use the lists at all. plt.show()看起来根本不使用列表。 Move all the plot code to the showPlot method, or provide the data as an argument to __init__ . 将所有绘图代码移动到showPlot方法,或将数据作为参数提供给__init__

Note that plt.show(), plt.title() etc. implicitely use the "current" axis; 请注意,plt.show(),plt.title()等隐含地使用“当前”轴; this could be a different plot made elsewhere. 这可能是其他地方不同的情节。 Use for example self.ax.set_title . 例如,使用self.ax.set_title

Depending on what you're trying to do with world , it may be best to 取决于你想要用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

and use with: 并用于:

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

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

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