我正在尝试制作Seaborn的动画(FuncAnimation
)。动画效果很好,但是hue
值(应由数据帧中的“ Anomalous”列控制)没有更新,并且会产生错误的结果。
我该如何解决此问题,以同时用temp和湿度列更新异常列?
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.animation as animation
import matplotlib
import seaborn as sns
sensor_data_path='full_results.csv'
sns.set(rc={'figure.figsize':(8,8)})
sns.set(style='ticks')
def read_sensor_data():
# I read the sensor data here
def modify_file(csv_file):
# I modify the sensor data to return the required chunk of the full file
def animate(i):
for c in scatters:
results_file=read_sensor_data()
results_file_copy=modify_file(results_file)
results_file_copy=results_file_copy.reset_index(drop=True)
xy=results_file_copy.loc[:,["temp_C","humid_%"]]
c.set_offsets(xy)
txt.set_text('frame={:d}'.format(i))
return scatters+[txt]
x="temp_C"
y="humid_%"
csv_file=read_sensor_data()
g = sns.lmplot(x=x, y=y,data=csv_file,hue="Anomalous",
markers=["o", "x"],legend_out = True, aspect=1.61, fit_reg=False)
fig = g.fig
ax = g.axes[0,0]
scatters = [c for c in ax.collections if isinstance(c, matplotlib.collections.PathCollection)]
txt = ax.text(0.1,0.9,'frame=0', transform=ax.transAxes)
ani=animation.FuncAnimation(fig, animate, interval=5000, save_count=100,
repeat=True, blit=False)
plt.show()