【问题标题】:How can I debug my background image in matplotlib?如何在 matplotlib 中调试我的背景图像?
【发布时间】:2019-11-19 13:27:57
【问题描述】:

我正在尝试编写一个程序,在其中为数学函数设置动画。不过,我想使用背景图像。我试过了,程序启动得很好,但很快它就失去了对图像的跟踪,一切都变得一团糟。我该如何解决这个问题?

我尝试实时调整图像大小以及 x 和 y 限制,但它仍然变得混乱。我正在使用 matplotlib 和 numpy。

此图像是程序运行时的图像:

但是,在这个程序中,程序存在错误:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import numpy as np

#[...]

def animate(data): #refresh data shown
    x,y = data
    xdata.append(x)
    ydata.append(y)
    xmin,xmax = ax.get_xlim()

    if x >= xmax:
        ax.set_xlim(xmin,2*xmax)
        ax.figure.canvas.draw()


    line.set_data(xdata,ydata)
    return line


img = plt.imread("myimage.jpg")
ax.imshow(img, extent=[0, ax.get_xlim()[-1], ax.get_ylim()[0], ax.get_ylim()[-1]]) 

ani = animation.FuncAnimation(fig,animate,data_gen,blit=True,interval=10,repeat = False)
plt.show()

(我尝试将图片上传到这个问题,但直到现在我无法做到)

【问题讨论】:

  • 实际上在使用 blit=True 时应该可以工作。没有可运行的代码,人们无法找出为什么它不能。对于 blit=False,可以通过将图像添加到轴坐标而不是数据坐标中,或者将其放置在背景中的不同轴中来最好地解决问题。

标签: python python-2.7 matplotlib animation


【解决方案1】:

解决了!出于某种原因,当我更改 ylimits 时它起作用了。也许图像无法调整为不同的比例(?)。修改:

if x >= xmax:
    ax.set_xlim(xmin,2*xmax)
    ax.set_ylim(-1.5*xmax,1.5*xmax)
    ax.figure.canvas.draw()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多