如何解决从Numpy数组生成和渲染条形图的最快方法
背景:我有2个数据数组:x
和y
,我想将它们转换成小节并在游戏过程中渲染每一帧。我目前正在使用matplotlib创建条形图,然后将条形图转换为像素数组并使用cv2显示。
问题:这种方法有效,但速度很慢,而且我的帧率降低到了2 fps。
问题:是否有一种更快的方式来创建条形图和渲染条形图,以便获得合理的帧速率?
目前,这是我用来从2个NumPy数组生成和渲染条形图的代码:
import numpy as np
import matplotlib.pyplot as plt
import cv2
x = np.arange(10)
y = np.ones([6,10])
def render_barplot():
plt.close() # Remove the previous figure
# Create new figure
fig,ax = plt.subplots()
ax.set_ylim([0,1])
for i in range(6):
ax.bar(x,ys[i],label=str(i))
ax.legend()
fig.canvas.draw()
# convert canvas to image
img = np.frombuffer(fig.canvas.tostring_rgb(),dtype=np.uint8)
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
# img is rgb,convert to opencv's default bgr
img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
cv2.imshow("barplot",img)
cv2.waitKey(1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。