【问题标题】:Python, Axes3D, plotting: Cannot append to List of X,Y,Z values in my 3D scatterplotPython,Axes3D,绘图:无法附加到我的 3D 散点图中的 X、Y、Z 值列表
【发布时间】:2017-05-26 22:12:09
【问题描述】:

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

请注意,在这种情况下,目的仅仅是为了能够看到我的情节上的点。他们的位置根本不重要。

【问题讨论】:

    标签: python axes


    【解决方案1】:

    如果我没记错的话,你是在创建世界时进行规划的(例如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()
    

    【讨论】:

    • 非常感谢您!我更改了代码以在 init 部分中包含所有 X、Y、Z 值以及与特定定义函数相关的所有绘图,并且确实有效!
    猜你喜欢
    • 1970-01-01
    • 2021-04-29
    • 2016-04-28
    • 2021-04-25
    • 1970-01-01
    • 2015-12-04
    • 2015-04-26
    • 2011-04-28
    相关资源
    最近更新 更多