[英]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.