带有滑块和文本的 3D 绘图(python 交互式 matplotlib)

Eau*_*che 5 python label slider matplotlib

我想绘制一些 3D+时间点。对于每个时间步长,我有 2 组命名点(治疗前后),我想使用滑块(如此)及时移动。如果可能的话,我想在每个点附近添加他们的名字(标签)以识别它们(我们无法为文本着色,所以我选择在点附近添加名字)。我是初学者,这对我来说并不简单,而且我真的被这个问题所困扰。我不知道我的策略是否适用于 python 中的 3D+时间数据。

我有 2 个字典initialdatafinaldata其中键对应于时间步长。我编写了一个名为plotting(initialdata, finaldata, t, plot_title = t, name, fuse)wheret是时间步长(整数)的函数,plot_title是一个字符串,fuse是一个布尔值(TRUE=initialdata 和 finaldata 绘制在同一个图中;FALSE=它们绘制在 2 个不同的子图中)。name是一个字典(键对应时间),每个点都有对应的名字。这是我的代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.widgets import Slider, Button, RadioButtons

def plotting(before, after, time, title = '', name=[] , fuse = False, verbose = False) :
    if verbose :
        print "before : ", before
        print "after : ", after
    xb = before[time][0]
    yb = before[time][1]
    zb = before[time][2]
    xa = after[time][0]
    ya = after[time][1]
    za = after[time][2]
    titb = "before",title
    tita = "after",title
    fig = plt.figure(figsize=plt.figaspect(0.5))
    if fuse:
        fig.subplots_adjust(hspace=0.1)
        ax = fig.add_subplot(1, 2, 1, projection = '3d')
        ax.scatter(xb, yb, zb, s=len(before), c = 'r', marker = 'o')
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.set_zlabel('Z')
        ax2 = fig.add_subplot(1, 2, 2, projection = '3d')
        ax2.scatter(xa, ya, za, s = len(after),c = 'b', marker = '^')
        ax.set_ylim([0,1000])
        ax2.set_xlabel('X')
        ax2.set_ylabel('Y')
        ax2.set_zlabel('Z')
        ax2.relim()
        ax2.autoscale_view(True,True,True)
        if len(name)>0:
            ax.text(xb, yb, zb, name[time])
            ax2.text(xa, ya, za, name[time])
    else:
        ax = fig.add_subplot(1, 2, 1, projection = '3d')
        ax.scatter(xb, yb, zb, s=len(before), c = 'r', marker = 'o')
        ax.scatter(xa, ya, za, s = len(after),c = 'b', marker = '^')
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.set_zlabel('Z')
        if len(name)>0:
            ax.text(xb, yb, zb, name[time])
            ax.text(xa, ya, za, name[time])
        ax.set_autoscaley_on(False)
        ax.set_ylim([0,1000])
    plt.show()
    return 0

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d') #ax =fig.gca(projection='3d')

initialdata={1: [(1, 1, 1), (1, 2, 3), (2, 3, 4), (2, 2, 2), (2, 3, 4), (3, 4, 5)], 2: [(2, 2, 2), (2, 3, 4), (3, 4, 5), (3, 3, 3), (3, 4, 5)]}
finaldata={1: [(2, 2, 2), (2, 3, 4), (3, 4, 5), (3, 3, 3), (3, 4, 5), (4, 5, 6)], 2: [(3, 3, 3), (3, 4, 5), (4, 5, 6), (4, 4, 4), (4, 5, 6)]}
corresp = {1: ["a", "b", "c", "d", "e", "f"], 2: ["a", "c", "d", "e", "f", "g"]}
time=1
stime = Slider(ax, 'Time', 1, 60, valinit=time)
plotting(initialdata, finaldata, time, title=time, name=corresp)
stime.on_changed(update)
def update(val):
    time = stime.val
    plotting(initialdata, finaldata, time, title=time, name=corresp)
draw()
Run Code Online (Sandbox Code Playgroud)

如果我只是尝试在没有滑块的情况下绘制一个时间步长(plotting()仅通过调用),它会绘制出绘图(初始 我认为没关系......但它返回:

>>> plotting(initialdata, finaldata, 1, title=1, name=corresp)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 293, in draw
    Axes.draw(self, renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2324, in draw
     a.draw(renderer)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/mplot3d/art3d.py", line 85, in draw
     if dx==0. and dy==0.:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
0
Run Code Online (Sandbox Code Playgroud)

我想在我的情节中使用滑块,但这条线不起作用:

>>>stime = Slider(ax, 'Time', 1, 60, valinit=time)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/widgets.py", line 376, in __init__ horizontalalignment='right')
TypeError: text() takes at least 5 arguments (4 given)
Run Code Online (Sandbox Code Playgroud)

但我有 5 个参数(ax并且time已定义)所以我不明白为什么它不起作用!我搜索了matplotlib.widgets文档中我是否可以理解Slider类的参数,但我不明白:(他们所描述的其它参数vlinepoly?); label定义了两次。

那我该怎么办?你能帮助我吗 ?