2016-12-16 114 views
0
从一组这样的图片添加图例

我做了一个动画(10个快照):enter image description here为(美术家)的动画matplotlib

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import Circle 
import time 

infile = open ('out.txt') 

frame_counter = 0 

N_p = 100 
N_step = 10 
N_line = N_p*N_step 

for s in xrange(N_step): 
     x, y = [], [] 
     for i in xrange(N_p): 
       data = infile.readline() 
       raw = data.split() 
       x.append(float(raw[0])) 
       y.append(float(raw[1])) 

     xnp = np.array(x) 
     ynp = np.array(y) 

     fig = plt.figure(0) 
     ax = fig.add_subplot(111, aspect='equal') 
     for x, y in zip(xnp, ynp): 

       cir = Circle(xy = (x, y), radius = 1) 
       cir.set_facecolor('red') 
       ax.add_artist(cir) 
       cir.set_clip_box(ax.bbox) 

       ax.set_xlim(-10, 150) 
       ax.set_ylim(-10, 150) 
     fig.savefig("step.%04d.png" % frame_counter) 
     ax.remove() 
     frame_counter +=1 

现在我想一个传说添加到每个图像显示时间步骤。 为了做到这一点,我必须为这10个图像中的每一个设置图例。问题是,我已经测试了不同的东西像ax.set_labelcir.set_label,... 我得到这样的错误:

UserWarning: No labelled objects found. Use label='...' kwarg on individual plots 

根据这个错误,我必须加贴到我的个人情节,但由于这是一个艺术家的情节,我不知道我怎么能做到这一点。

+0

当只有一种类型的点被显示时,为什么你需要一个图例? – ImportanceOfBeingErnest

+0

我想在动画中展示时间。 – user252935

+0

让我细化一下:为什么只有一种类型的点被显示时,您需要一个图例来显示动画中的时间? – ImportanceOfBeingErnest

回答

1

如果无论出于何种原因需要legend,您可以将Circle显示为句柄并使用一些文本作为标签。

ax.legend(handles=[cir], labels=["{}".format(frame_counter)]) 

如果你并不真的需要一个传说,你可以使用一些text到轴内放置。

ax.text(.8,.8, "{}".format(frame_counter), transform=ax.transAxes)