python - 使用 matplotlib 动画上的子图绘制带有图例的多条曲线
问题描述
我使用 matplotlib 生成带有子图的动画,但对于某些子图,我想包含多条带有图例的曲线。我无法让它工作。我的代码如下所示:
load = np.rand(1000,24)
index = np.arange(24)
reward1 = np.rand(1000)
reward2 = np.rand(1000)
reward3 = np.rand(1000)
Z = np.arange(1000)
reward1 = np.vstack((Z,reward1))
reward2 = np.vstack((Z,reward2))
reward3 = np.vstack((Z,reward3))
fig, axes = plt.subplots(2,2,figsize=(15,9))
fig.tight_layout()
lines = []
for nd, ax in enumerate(axes.flatten()):
if nd == 0:
l, = ax.plot(load[0,:],index)
if nd == 1:
l, = ax.plot(reward1[0], reward1[0])
if nd == 2:
l, = ax.plot(reward2[0], reward2[0])
if nd == 3:
l, = ax.plot(reward3[0], reward3[0])
lines.append(l)
def run(it):
for nd, line in enumerate(lines):
if nd == 0:
line.set_data(index,load[it,:])
if nd == 1:
line.set_data(reward1[..., :it])
if nd == 2:
line.set_data(reward2[..., :it])
if nd == 3:
line.set_data(reward3[..., :it])
ani = animation.FuncAnimation(fig, run, frames=1000, interval=30, blit=True)
ani.save('figure.mp4')
plt.show()
这将创建一个带有 4 个子图的动画。我希望奖励曲线与相应的图例位于同一子图上。当然,负载和奖励不是随机的,但我添加了它以准确显示它们是什么:numpy 数组。
为了生成图例,我使用了以下代码:
plt.figure()
na1, = plt.plot(reward1)
na2, = plt.plot(reward2)
na3, = plt.plot(reward3)
plt.legend((na1,na2,na3),('Reward 1','Reward2','Reward 3'))
plt.show()
我试图将这两段代码整合在一起,但我一直没有成功。那么,有没有一种简单的方法可以做我想做的事?
解决方案
原始答案
添加以下行怎么样(例如,在您绘制线条的循环之后):
axes.flatten()[0].legend(handles=lines, labels=["index", "reward1", "reward2", "reward3"])
您需要将线实例作为句柄传递给图例函数,以包含来自其他子图的线。
编辑
以下代码适用于我。请注意,我稍微修改了您的示例以使其运行。您可能还应该查看图例指南,了解有关如何放置和修改图例的更多详细信息。我在您的绘图循环下方插入了图例代码。有两种选择,可以为所有行绘制一个图例,也可以为每行绘制一个图例。
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
# Dummy data
load = np.random.randn(1000, 24)
index = np.arange(24)
reward1 = np.random.randn(1000)
reward2 = np.random.randn(1000)
reward3 = np.random.randn(1000)
Z = np.arange(1000)
reward1 = np.vstack((Z, reward1))
reward2 = np.vstack((Z, reward2))
reward3 = np.vstack((Z, reward3))
fig, axes = plt.subplots(2, 2, figsize=(15, 9))
fig.tight_layout()
axes = axes.flatten()
lines = []
for nd, ax in enumerate(axes):
if nd == 0:
l, = ax.plot(index, load[0, :], label="index")
if nd == 1:
l, = ax.plot(reward1[0], reward1[1], label="reward1")
if nd == 2:
l, = ax.plot(reward2[0], reward2[1], label="reward2")
if nd == 3:
l, = ax.plot(reward3[0], reward3[1], label="reward3")
lines.append(l)
# Legend
# Have one legend on axes[0] for all lines
axes[0].legend(handles=lines, loc=1)
# Legend alternative
# Have one legend per axes for one line each
# for ax in axes:
# ax.legend()
def init():
return lines
def run(it):
for nd, line in enumerate(lines):
if nd == 0:
line.set_data(index, load[it, :])
if nd == 1:
line.set_data(reward1[0, :it], reward1[1, :it])
if nd == 2:
line.set_data(reward2[0, :it], reward2[1, :it])
if nd == 3:
line.set_data(reward3[0, :it], reward3[1, :it])
return lines
ani = animation.FuncAnimation(
fig, run, frames=np.arange(1000), interval=30, blit=True,
init_func=init
)
ani.save('figure.mp4')
plt.show()
推荐阅读
- node.js - 未找到模块:无法解析 pg/lib 中的“dns”
- java - 使用 Powermockito 模拟最终类的本地对象创建
- python - 如何在画布上从上到下绘制段落?
- javascript - BIRT - Javascript Math.abs 函数不起作用
- .net - 为什么我的连接字符串失败?
- spring-integration - Spring Integration 将有效负载转换为服务激活器中的对象
- python-3.x - Python - 尝试向 Codeforces 发送请求
- java - ArrayList 类在离开 whileloop 后丢失变量
- asp.net - 如何在我的 ASP.Net Core 2.0 项目中实现 Active Directory?
- file - SharePoint Online 选择性脱机同步