简体   繁体   中英

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". At the moment I would like to append values to X1, Y1, Z1 to create the points in question. 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. 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() ). The plt.show() doesn't look use the lists at all. Move all the plot code to the showPlot method, or provide the data as an argument to __init__ .

Note that plt.show(), plt.title() etc. implicitely use the "current" axis; this could be a different plot made elsewhere. Use for example self.ax.set_title .

Depending on what you're trying to do with world , it may be best to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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