我想绘制一些3D +时间点。对于每个时间步,我有2组命名点(治疗前后),我想使用滑块(如here)及时移动。 如果可能的话,我想在每个点附近添加他们的名字(标签)以识别它们(我们不能对文本进行着色,所以我选择在点附近添加名称)。 我是一个初学者,对我来说并不简单,而且我确实阻止了这个问题。我不知道我的策略是否可以在python中使用3D +时间数据。
我有2个字典initialdata
和finaldata
,其中的键对应于时间步长。
我编写了一个名为plotting(initialdata, finaldata, t, plot_title = t, name, fuse)
的函数,其中t
是时间步长(整数),plot_title
是一个字符串,fuse
是一个布尔值(TRUE = initialdata和finaldata同样绘制) plot; 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()
如果我只是尝试绘制一个没有滑块的时间步长(仅通过调用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
我想在我的情节中使用滑块,但这条线不起作用:
>>>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)
但我有5个参数(ax
和time
已定义)所以我不明白它为什么不起作用!我搜索in the matplotlib.widgets documentation是否可以理解Slider类的参数,但我不明白:它们描述了其他参数(vline
?poly
?); label
定义了两次。
那我该怎么办?你能救我吗?