0

我有以下代码可以完美运行,但放置我的点注释和更新图例的时间与点出现在图表上的时间不一致。我如何让它们对齐?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import style
import matplotlib.patches as mpatches

style.use('ggplot')

x_data = [1,5,7,9,11,12,14,15,27,29,37,39,45,47,52,53,57,58,61,62,66,80,82,83,84,85,90,91,93,96,98,105,109,110,111,113,114,116,117,120,122,123,127,134,136,138,140,141,144,160,161,162,165,174,176,179,183,184,185,186,190,191,192,193,194,195,199,200,204]
y_data = [50,55,40,30,31,20,21,18,25,21,15,18,20,24,27,30,32,35,37,38,30,11,13,10,10,14,16,18,19,17,14,9,9,4,5,5,6,5,7,3,6,8,10,13,15,12,10,13,8,4,3,5,4,5,7,6,4,8,10,12,10,12,12,12,12,15,17,18,18]
annotations = ['','','Pasted Value','Cut Value','','Pasted Cut Value','','Abnormal Pause','','Abnormal Pause','Out of Order Field Interaction','','','','','','','','','','Window Exit','Window Entry','','Pasted Value','Out of Order Field Interaction','','','Irregular Typing Cadence','','Irregular Typing Cadence','Abnormal Pause','Irregular Typing Cadence','Out of Order Field Interaction','Value Manipulation','','Out of Order Field Interaction','','Value Manipulation','','Value Manipulation','','','','','','','','','Window Exit','Window Entry','Pasted Value','','Value Manipulation','','','Value Manipulation','Value Manipulation','','','','Copied Value','','Frustration - Repeat Paste','Frustration - Repeat Paste','Frustration - Repeat Paste','','','','']
print(len(x_data))
print(len(y_data))
print(len(annotations))

fig, ax = plt.subplots()
ax.set_xlim(0,205)
ax.set_ylim(0,100)
line, = ax.plot(0,50)

def init():
  line.set_data([], [])
  return line,

def animate(n):
  line, = plt.plot(x_data[:n], y_data[:n], color='b', marker='o')
  ax.annotate(annotations[n],(x_data[n],y_data[n]))
  updated_score = mpatches.Patch(color='b', label=y_data[n])
  plt.legend(handles=[updated_score])
  return line,

animation = FuncAnimation(fig, animate, frames=len(x_data), interval=500)

plt.title('A Cool Title')
plt.xlabel('Time in Seconds')
plt.ylabel('Value')

plt.show()

这是一个工作副本

4

2 回答 2

2

我认为 ax 和 plot 的混合是问题,我用对象样式重写了它并修复了代码。

fig, ax = plt.subplots()
ax.set_xlim(0,205)
ax.set_ylim(0,100)
line, = ax.plot(0,50)
ax.set_title('A Cool Title')
ax.set_xlabel('Time in Seconds')
ax.set_ylabel('Value')


def init():
    line.set_data([], [])
    return line,

def animate(n):
    line, = ax.plot(x_data[:n], y_data[:n], color='b', marker='o')
    ax.annotate(annotations[n],(x_data[n],y_data[n]))
    updated_score = mpatches.Patch(color='b', label=y_data[n])
    ax.legend(handles=[updated_score])
    return line,

animation = FuncAnimation(fig, animate, frames=len(x_data), interval=500, repeat=False)
# animation.save('line_legend_sync_ani.gif', writer='pillow')
plt.show()

在此处输入图像描述

于 2021-01-22T10:18:33.580 回答
1

请记住,当切片 Pandas 时不包含最后一个值,因此在这一行中plt.plot(x_data[:n+1], y_data[:n+1])您应该添加 +1 以包含第 n 个值。我认为这解决了时间不足的问题。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import style
import matplotlib.patches as mpatches

style.use('ggplot')

x_data = [1,5,7,9,11,12,14,15,27,29,37,39,45,47,52,53,57,58,61,62,66,80,82,83,84,85,90,91,93,96,98,105,109,110,111,113,114,116,117,120,122,123,127,134,136,138,140,141,144,160,161,162,165,174,176,179,183,184,185,186,190,191,192,193,194,195,199,200,204]
y_data = [50,55,40,30,31,20,21,18,25,21,15,18,20,24,27,30,32,35,37,38,30,11,13,10,10,14,16,18,19,17,14,9,9,4,5,5,6,5,7,3,6,8,10,13,15,12,10,13,8,4,3,5,4,5,7,6,4,8,10,12,10,12,12,12,12,15,17,18,18]
annotations = ['','','Pasted Value','Cut Value','','Pasted Cut Value','','Abnormal Pause','','Abnormal Pause','Out of Order Field Interaction','','','','','','','','','','Window Exit','Window Entry','','Pasted Value','Out of Order Field Interaction','','','Irregular Typing Cadence','','Irregular Typing Cadence','Abnormal Pause','Irregular Typing Cadence','Out of Order Field Interaction','Value Manipulation','','Out of Order Field Interaction','','Value Manipulation','','Value Manipulation','','','','','','','','','Window Exit','Window Entry','Pasted Value','','Value Manipulation','','','Value Manipulation','Value Manipulation','','','','Copied Value','','Frustration - Repeat Paste','Frustration - Repeat Paste','Frustration - Repeat Paste','','','','']
print(len(x_data))
print(len(y_data))
print(len(annotations))

fig, ax = plt.subplots()
ax.set_xlim(0,205)
ax.set_ylim(0,100)
line, = ax.plot(0,50)

def init():
  line.set_data([], [])
  return line,

def animate(n):
  line, = plt.plot(x_data[:n+1], y_data[:n+1], color='b', marker='o')
  ax.annotate(annotations[n],(x_data[n],y_data[n]))
  updated_score = mpatches.Patch(color='b', label=y_data[n])
  ax.legend(handles=[updated_score])
  return line,

animation = FuncAnimation(fig, animate, frames=len(x_data), interval=500)

plt.title('A Cool Title')
plt.xlabel('Time in Seconds')
plt.ylabel('Value')

plt.show()

在这里查看结果:https ://repl.it/@JuanJavier1/A-Cool-Title

于 2021-01-23T01:12:48.027 回答