鱼C论坛

 找回密码
 立即注册
查看: 1956|回复: 5

[已解决]matplotlib绘制动画,如何在显示图中,直接通过文本框输入 数值给 变量y ?

[复制链接]
发表于 2021-9-29 14:55:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
matplotlib绘制动画,如何在显示图中,直接通过文本框输入 数值给 变量y   ?

而不是 代码中的一段函数?  y = np.sin(2 * np.pi * (x - 0.01 * i))

  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. from matplotlib import animation
  4. # First set up the figure, the axis, and the plot element we want to animate
  5. fig = plt.figure()
  6. ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
  7. line, = ax.plot([], [], lw=2)
  8. # initialization function: plot the background of each frame
  9. def init():
  10.   line.set_data([], [])
  11.   return line,
  12. # animation function. This is called sequentially
  13. # note: i is framenumber
  14. def animate(i):
  15.   x = np.linspace(0, 2, 1000)
  16.   y = np.sin(2 * np.pi * (x - 0.01 * i))
  17.   line.set_data(x, y)
  18.   return line,
  19. # call the animator. blit=True means only re-draw the parts that have changed.
  20. anim = animation.FuncAnimation(fig, animate, init_func=init,
  21.                 frames=200, interval=20, blit=True)
  22. #anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
  23. plt.show()
复制代码


最佳答案
2021-9-29 17:52:25
参考:https://stackoverflow.com/questions/21197728/embedding-a-matplotlib-animation-into-a-tkinter-frame
第二个人Victor的回答,可以解决你的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-29 15:26:36 | 显示全部楼层
input:
  1. y = float(input('Please input a float number:'))
复制代码

不过这样每次想显示一个帧都会需要你输入。不太确定你这样做的目的,还不如提前把数据准备好,让他按照顺序自动获取。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-29 15:32:34 | 显示全部楼层
suchocolate 发表于 2021-9-29 15:26
input:

不过这样每次想显示一个帧都会需要你输入。不太确定你这样做的目的,还不如提前把数据准备好, ...

谢谢,我是希望 在动画图中 有个文本框 可以给Y  赋值,这个该怎么实现呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-29 15:49:19 | 显示全部楼层
wideband 发表于 2021-9-29 15:32
谢谢,我是希望 在动画图中 有个文本框 可以给Y  赋值,这个该怎么实现呢?

mp有一个text_box,它支持交互输入,但同时它也会改变呈现的方式,就不能用动画了。
https://matplotlib.org/3.3.1/gal ... -widgets-buttons-py
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-29 17:52:25 | 显示全部楼层    本楼为最佳答案   
参考:https://stackoverflow.com/questions/21197728/embedding-a-matplotlib-animation-into-a-tkinter-frame
第二个人Victor的回答,可以解决你的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-1 17:10:27 | 显示全部楼层
阿奇_o 发表于 2021-9-29 17:52
参考:https://stackoverflow.com/questions/21197728/embedding-a-matplotlib-animation-into-a-tkinter-f ...

谢谢您的支持。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 01:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表