使用Python的Matplotlib,如何将特定点的注释更新与使用FuncAnimation绘制的数据集对齐?

2024-09-24 22:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码,除了放置我的点注释和更新图例的时间与点出现在图形上的时间不一致外,它们的功能非常完美。如何使它们对齐

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()

这是一个工作replit


Tags: ofimportfielddatamatplotlibvaluelineorder
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()

enter image description here

请记住,当切片熊猫不包括最后一个值时,因此在这一行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

相关问题 更多 >