Matplotlib 没有绘制任何东西
Matplotlib not plotting anything
我正在尝试为张量日志中的一些数据制作一个带有 3 个子图的图形。我已经制作了生成器和动画函数,它们似乎产生了正确的数据,但是当我这样做时 plt.show()
,绘图上没有任何显示,当我尝试另存为 mp4 时,生成的视频上没有绘制任何内容,并且它只迭代到第 100 个值(共 609 个)然后停止。
这是最小代码:
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
def gendat():
res = list(np.random.random_sample(size=609))
lens = list(np.random.random_sample(size=609))
loss = list(np.random.random_sample(size=609))
for i, (r, le, lo) in enumerate(zip(res,lens,loss)):
yield i, r, le , lo
figure, axis = plt.subplots(1,3, figsize=(15,5))
line1, = axis[0].plot([],[])
axis[0].set_xlabel("Episodes")
axis[0].set_ylabel("Average Reward")
axis[0].set_title("Reward")
line2, = axis[1].plot([],[])
axis[1].set_xlabel("Episodes")
axis[1].set_ylabel("Average Episode Length")
axis[1].set_title("Episode Length")
line3, = axis[2].plot([],[])
axis[2].set_xlabel("Episodes")
axis[2].set_ylabel("Average Loss")
axis[2].set_title("Loss")
line = [line1,line2,line3]
def init():
line[0].set_data([],[])
line[1].set_data([],[])
line[2].set_data([],[])
return line
figure.tight_layout()
def animate(dat):
i,r, le , lo = dat
print("got",i,r,le,lo)
line[0].set_data(i,r)
line[1].set_data(i,le)
line[2].set_data(i,lo)
return line
FFwriter = matplotlib.animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
ani = matplotlib.animation.FuncAnimation(figure, animate, init_func=init, frames=gendat, interval=20, repeat=False, blit=True)
plt.show()
ani.save("results.mp4",writer=FFwriter, dpi=500)
#plt.savefig("results.png",transparent=True, dpi=500)
需要解决两件事:
- 所有子图的轴限制。
set_data
需要坐标列表,而不是数字!
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
N = 609
def gendat():
res = list(np.random.random_sample(size=609))
lens = list(np.random.random_sample(size=609))
loss = list(np.random.random_sample(size=609))
for i, (r, le, lo) in enumerate(zip(res,lens,loss)):
yield i, r, le , lo
figure, axis = plt.subplots(1,3, figsize=(15,5))
line1, = axis[0].plot([],[])
axis[0].set_xlabel("Episodes")
axis[0].set_ylabel("Average Reward")
axis[0].set_title("Reward")
axis[0].set_xlim(0, N)
axis[0].set_ylim(-10, 10)
line2, = axis[1].plot([],[])
axis[1].set_xlabel("Episodes")
axis[1].set_ylabel("Average Episode Length")
axis[1].set_title("Episode Length")
axis[1].set_xlim(0, N)
axis[1].set_ylim(-10, 10)
line3, = axis[2].plot([],[])
axis[2].set_xlabel("Episodes")
axis[2].set_ylabel("Average Loss")
axis[2].set_title("Loss")
axis[2].set_xlim(0, N)
axis[2].set_ylim(-10, 10)
line = [line1,line2,line3]
def init():
line[0].set_data([],[])
line[1].set_data([],[])
line[2].set_data([],[])
return line
figure.tight_layout()
def append_to_line(line, x, y):
xd, yd = [list(t) for t in line.get_data()]
xd.append(x)
yd.append(y)
line.set_data(xd, yd)
print(xd)
def animate(dat):
i, r, le, lo = dat
append_to_line(line[0], i, r)
append_to_line(line[1], i, le)
append_to_line(line[2], i, lo)
# FFwriter = matplotlib.animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
ani = matplotlib.animation.FuncAnimation(figure, animate, frames=gendat, interval=20, repeat=False)
plt.show()
ani.save("results.mp4",writer=FFwriter, dpi=500)
我正在尝试为张量日志中的一些数据制作一个带有 3 个子图的图形。我已经制作了生成器和动画函数,它们似乎产生了正确的数据,但是当我这样做时 plt.show()
,绘图上没有任何显示,当我尝试另存为 mp4 时,生成的视频上没有绘制任何内容,并且它只迭代到第 100 个值(共 609 个)然后停止。
这是最小代码:
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
def gendat():
res = list(np.random.random_sample(size=609))
lens = list(np.random.random_sample(size=609))
loss = list(np.random.random_sample(size=609))
for i, (r, le, lo) in enumerate(zip(res,lens,loss)):
yield i, r, le , lo
figure, axis = plt.subplots(1,3, figsize=(15,5))
line1, = axis[0].plot([],[])
axis[0].set_xlabel("Episodes")
axis[0].set_ylabel("Average Reward")
axis[0].set_title("Reward")
line2, = axis[1].plot([],[])
axis[1].set_xlabel("Episodes")
axis[1].set_ylabel("Average Episode Length")
axis[1].set_title("Episode Length")
line3, = axis[2].plot([],[])
axis[2].set_xlabel("Episodes")
axis[2].set_ylabel("Average Loss")
axis[2].set_title("Loss")
line = [line1,line2,line3]
def init():
line[0].set_data([],[])
line[1].set_data([],[])
line[2].set_data([],[])
return line
figure.tight_layout()
def animate(dat):
i,r, le , lo = dat
print("got",i,r,le,lo)
line[0].set_data(i,r)
line[1].set_data(i,le)
line[2].set_data(i,lo)
return line
FFwriter = matplotlib.animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
ani = matplotlib.animation.FuncAnimation(figure, animate, init_func=init, frames=gendat, interval=20, repeat=False, blit=True)
plt.show()
ani.save("results.mp4",writer=FFwriter, dpi=500)
#plt.savefig("results.png",transparent=True, dpi=500)
需要解决两件事:
- 所有子图的轴限制。
set_data
需要坐标列表,而不是数字!
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
N = 609
def gendat():
res = list(np.random.random_sample(size=609))
lens = list(np.random.random_sample(size=609))
loss = list(np.random.random_sample(size=609))
for i, (r, le, lo) in enumerate(zip(res,lens,loss)):
yield i, r, le , lo
figure, axis = plt.subplots(1,3, figsize=(15,5))
line1, = axis[0].plot([],[])
axis[0].set_xlabel("Episodes")
axis[0].set_ylabel("Average Reward")
axis[0].set_title("Reward")
axis[0].set_xlim(0, N)
axis[0].set_ylim(-10, 10)
line2, = axis[1].plot([],[])
axis[1].set_xlabel("Episodes")
axis[1].set_ylabel("Average Episode Length")
axis[1].set_title("Episode Length")
axis[1].set_xlim(0, N)
axis[1].set_ylim(-10, 10)
line3, = axis[2].plot([],[])
axis[2].set_xlabel("Episodes")
axis[2].set_ylabel("Average Loss")
axis[2].set_title("Loss")
axis[2].set_xlim(0, N)
axis[2].set_ylim(-10, 10)
line = [line1,line2,line3]
def init():
line[0].set_data([],[])
line[1].set_data([],[])
line[2].set_data([],[])
return line
figure.tight_layout()
def append_to_line(line, x, y):
xd, yd = [list(t) for t in line.get_data()]
xd.append(x)
yd.append(y)
line.set_data(xd, yd)
print(xd)
def animate(dat):
i, r, le, lo = dat
append_to_line(line[0], i, r)
append_to_line(line[1], i, le)
append_to_line(line[2], i, lo)
# FFwriter = matplotlib.animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
ani = matplotlib.animation.FuncAnimation(figure, animate, frames=gendat, interval=20, repeat=False)
plt.show()
ani.save("results.mp4",writer=FFwriter, dpi=500)