gpt4 book ai didi

python - 在pygame中画图

转载 作者:行者123 更新时间:2023-12-05 07:33:33 25 4
gpt4 key购买 nike

我有一个数字字典(准确的价格)/每个月:

{'2018-05-26': Decimal('7334.1638'), '2018-05-27': Decimal('7344.9675'), '2018-05-28': Decimal('7105.6725')}

我可以使用 plotly 轻松绘制此图,该图如下所示: Plot

我希望此图在 pygame 中显示为绘图。我得到了几个关于如何在 pygame 中显示 matplotlib 数据的例子,但那些只是复制/粘贴 plotly ,你不能与环境互动。我想要做的是让这个 plotly 成为 pygame 中的游戏环境。

假设我想让汽车像山一样在这个地 block 上行驶。另外,由于我的列表是动态的,所以我需要它是实时的。关于如何实现这一目标的任何建议都会有所帮助。

最佳答案

我使用了 pygame wiki 中的这种方法 - http://www.pygame.org/wiki/MatplotlibPygame它将 matplotlib 中的 graphgh 渲染为位图,然后将其转换为字符串,然后使用该字符串在 pygame 中创建带有图像的表面。

import matplotlib
matplotlib.use("Agg")

import matplotlib.backends.backend_agg as agg


import pylab

fig = pylab.figure(figsize=[4, 4], # Inches
dpi=100, # 100 dots per inch, so the resulting buffer is 400x400 pixels
)
ax = fig.gca()
ax.plot([1, 2, 4])

canvas = agg.FigureCanvasAgg(fig)
canvas.draw()
renderer = canvas.get_renderer()
raw_data = renderer.tostring_rgb()

import pygame
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((600, 400), DOUBLEBUF)
screen = pygame.display.get_surface()

size = canvas.get_width_height()

surf = pygame.image.fromstring(raw_data, size, "RGB")
screen.blit(surf, (0,0))
pygame.display.flip()

crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True

关于python - 在pygame中画图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50584736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com