每当我尝试更改窗口尺寸时,matplotlib就会绘制整个图形

时间:2015-08-13 17:41:42

标签: python animation matplotlib

目标是在图表上输出实时数据,并能够更改其尺寸。我尝试使用从here获取的默认matplotlib示例。我已经改变了一点:

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

def data_gen():
    t = data_gen.t
    cnt = 0
    while cnt < 10000:
        cnt+=1
        t += 0.05
        yield t, np.sin(2*np.pi*t) * np.exp(-t/40.)
data_gen.t = 0

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
ax.grid()
xdata, ydata = [], []
def run(data):
    t,y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()
    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)
    return line,

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

在窗口尺寸发生变化之前,一切都很好。 Matplotlib通过整个图形绘制线到第一点,然后将另一条线绘制到它想象的位置。最后它看起来像this。所以这个数字搞砸了,我不知道该如何处理它。

我使用的是Ubuntu 15.04,python 2.7.9,matplotlib 1.4.3。在Windows 7和8.1上也是如此。

1 个答案:

答案 0 :(得分:0)

要完全关闭此问题,因为即使在matplotlib 2.0.0中也会出现此问题,我将自己回答这个问题。正如 @BrenBarn 指出的那样,我只需要<Style TargetType="Navigation:HtNavigationMenuCategoryItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem"> <Grid Margin="10,10,10,0"> <StackPanel Orientation="Vertical"> <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CategoryItems}"> <ItemsControl.ItemContainerStyle> <Style TargetType="Navigation:HtNavigationMenuCategoryItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Navigation:HtNavigationMenuCategoryItem"> <Controls:FooControl Text="test" Foreground="Yellow"></TextBlock> </ControlTemplate> </Setter.Value> </Setter> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> </StackPanel> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>