- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从外部设备获取数据,并使用 matplotlib.animation.FuncAnimation 实时绘制数据。对于此测量,重要的是将底部 y 轴限制保持在 0,但保持上限自由。设备本身会为该测量返回两组数据,因此我同时为这两组数据制作动画,因此是子图。
网上搜索建议使用axes.set_ylim(bottom=0)
,都在this question中和 this one .但是他们的解决方案对我不起作用。
已经是代码一部分的自动缩放正在使用 axes.relim()
和 axes.autoscale_view(True, True, True)
,来自另一个问题的答案我已经忘记了。弄乱这些代码行似乎修复了查看窗口,但它不再随数据缩放。然后数据可以在“屏幕外”动画化。
我在采集(理想情况下)的本质下方重新创建了它,因为它比使用多个文件更容易。我假设问题在于 animate_corr(i)
的缩放。其余的代码只是获取和绘制数据。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#used to generate test data
import random
pi = np.pi
factor = 1
random.seed(123)
#procedural data generation for left plot
def get_data(data):
global mu
global b
global index
global factor
b=b*0.99
factor=factor*1.01
new_data = [factor*(((1-((1/(2*b))*np.exp(-1*(abs(i-mu))/b)))*random.random())+10) for i in index]
return new_data
#procedural data generation for right plot
def get_data_norm(data):
global mu
global b
global index
new_data = [((1-((1/(2*b))*np.exp(-1*(abs(i-mu))/b)))+10) for i in index]
return new_data
#animation function, assuming problem is here
def animate_corr(i):
global dat
global dat_norm
dat = get_data(dat)
dat_norm = get_data_norm(dat_norm)
#these two not working as expected
axs_corr[0].set_ylim((0, None), auto=True)
axs_corr[1].set_ylim(bottom=0, top=None, auto=True)
line_corr.set_ydata(dat)
line_corr_norm.set_ydata(dat_norm)
#rescales axes automatically
axs_corr[0].relim()
axs_corr[0].autoscale_view(True,True,True)
axs_corr[1].relim()
axs_corr[1].autoscale_view(True,True,True)
return line_corr, line_corr_norm,
#plots definitions
fig_corr, axs_corr = plt.subplots(1,2, sharex=True, figsize=(10,5))
fig_corr.suptitle('Animated Correlation')
#x is fixed
length = 1001
index = np.linspace(-10,10,length)
#laplacian distribution parameters
mu = 0
b = 2
#data
dat = [(1-((1/(2*b))*np.exp(-1*(abs(i-mu))/b)))+10 for i in index]
dat_norm = [(1-(1/(2*b))*np.exp(-(abs(i-mu))/b))+10 for i in index]
#initial plots
line_corr, = axs_corr[0].plot(index, dat)
line_corr_norm, = axs_corr[1].plot(index, dat_norm)
#titles
axs_corr[0].set_title('Random')
axs_corr[1].set_title('No Random')
#axes labels
fig_corr.text(0.51, 0.04, 'Time (ns)', ha='center')
fig_corr.text(0.04, 0.5, 'Coincidinces', va='center', rotation='vertical')
#animation call
ani_corr = animation.FuncAnimation(fig_corr, animate_corr, interval=10, blit=False, save_count=50)
plt.show()
我希望两个图的 y 轴限制都固定为 0。所以左边的图会不断增加其最大值,并在其比例中看到这一点。正确的图会使其倾角越来越尖锐,但一旦它小于 0,该图就不会再改变其比例(因为该图的值不会变大)。
最佳答案
@ivallesp 几乎成功了。删除 axs_corr[0].set_ylim((0, None), auto=True)
和 axs_corr[1].set_ylim((0, None), auto=True)
从 set_ydata
方法之前并将它们放在 autoscale_view
调用之后,对于这两个图,它也按照我想要的方式工作。
关于python - 使用 matplotlib.animation.FuncAnimation 将 y 轴下限固定为 0,同时不对两个子图施加上限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57510430/
我对此给出了答案thread ,谈论 matplotlib 上的褪色点。我对 ImportanceOfBeingErnest 的回答感到好奇。所以我尝试尝试他的代码。 首先,这是我的代码。 impor
我正在制作一个应该可视化传感器数据的应用程序。因此,我使用 python 和 tkinter 作为 gui 框架和 matplotlib 来可视化数据。该图应显示当前传感器值,因此它应该是动画的。但是
我对此给出了答案thread ,谈论 matplotlib 上的褪色点。我对 ImportanceOfBeingErnest 的回答感到好奇。所以我尝试尝试他的代码。 首先,这是我的代码。 impor
有谁知道停止 FuncAnimation 的首选方法吗?我用它来记录来自示波器的数据,并希望能够按需暂停和重新启动数据。有什么方法可以向它发送按钮点击事件吗? 谢谢,德里克 最佳答案 FuncAnim
下面的代码有效,但我认为每个重复周期都会过度绘制原始点。我希望它从原点开始,每个重复循环,有一个清晰的情节。在解决此问题的许多方法中,我尝试在 init 和 update 函数中插入 ax.clear
我试图从头开始模拟双星系统中行星的运动。为此,我需要能够在动画图中绘制点。在对整个事情进行编码之前,我正在学习使用 pyplot 为绘图设置动画。到目前为止,我还没有运气为移动点设置动画。在查看了几个
我正在尝试为机器人的链接设置动画但没有成功。 FuncAnimation 从不调用 animate 函数——从不执行打印语句。任何帮助将不胜感激。我的代码: def foo(): joints
我是 python 的新手,我正在尝试使用 matplotlibs FuncAnimation 制作动画并将其保存为某种视频格式。如果我运行以下代码: import numpy as np impor
我正在尝试用我计算的数据制作一部电影。我正在使用 ffmpeg 编写器。当我在 Spyder 中启动动画时,它工作正常并持续到最后,但当我尝试保存它时,它只持续动画的前 30%。我怎样才能保存整个动画
我想从不同的文件生成一系列动画线图的视频。下面是我的 python 代码的开始。我正在使用matplotlib.animation图书馆。 看起来像这样: 如何删除之前的绘图(一次显示一行),但保持相
我想构建一个带有某些实时图表的界面,显示一些实验的结果。为此,我决定结合使用glade(UI)、gtk、python 和 matplotlib。我正在学习一些基础知识,并且能够绘制一些实时图表。 现在
我正在尝试使用 FuncAnimation 模块制作动画,但我的代码只生成一帧然后停止。它似乎没有意识到它需要更新什么。你能帮我看看哪里出了问题吗? import numpy as np import
我使用 matplotlib 的 FuncAnimation 创建了表面绑定(bind)过程的简单动画。然而,结果非常缓慢。我怀疑这是因为我在每一帧重新绘制所有元素,但我还没有找到解决方法。任何帮助表
根据 matplotlib 的文档,FuncAnimation 重复调用一个函数来制作动画。 问题是,当没有更多图像数据可用时,我应该如何告诉 FuncAnimation() 停止? 最佳答案 如果您
如何将参数传递给animation.FuncAnimation()?我试过了,但没用。 animation.FuncAnimation() 的签名是 class matplotlib.animatio
浏览了一些关于 matplotlib 动画的教程并遇到了这个问题。我正在使用 matplotlib.animation funcanimation 如下: import matplotlib.anim
我正在尝试使用 Matplotlib 的 FuncAnimation 来动画显示每帧动画中的一个点。 # modules #--------------------------------------
我想弄清楚是否有任何方法可以更改现有 matplotlib FuncAnimation 的间隔。我希望能够根据用户输入调整动画的速度。 我发现了一个类似的问题 How do I change the
我在 Matplotlib 中遇到动画缓慢的问题。我正在对模拟结果进行动画处理,这是最简单的可视化方式,其中包含一系列随时间改变颜色的矩形。 遵循建议 here ,我使用 blitting 仅绘制每帧
我制作了一个脚本,它使用 matplotlib 的 FuncAnimation 函数来为抛物面函数的一系列等高线图设置动画。我想添加一个颜色条,其范围在整个动画过程中都不会改变。我真的不知道该怎么做。
我是一名优秀的程序员,十分优秀!