【发布时间】:2018-07-23 06:49:45
【问题描述】:
(这是this问题的扩展)
你好
我正在尝试使用新数据更新 lmplot,但无法找到将其连接到现有图形/轴的方法,因为它们有自己的。到目前为止,我已经尝试过这样的:
%matplotlib notebook
import matplotlib.animation as animation
import numpy as np
#fig, ax = plt.subplots(1,1,figsize=(5,4))
df = get_data()
g = sns.lmplot( x='Mean', y='Variance', data=df, fit_reg=False, hue='Size', legend=False, palette=cmap)
def get_data():
takeRandomSample(population, popSize, popVar)
current_elem = len(sampleStats)-1
current_size = sampleStats[current_elem][0]
current_mean = sampleStats[current_elem][1]
current_var = sampleStats[current_elem][2]
data = {'Size' : current_size, 'Mean' : current_mean, 'Variance' : current_var}
df = pd.DataFrame(data, index=[0])
return df
def prep_axes(g):
g.set(xlim=(0, 20), ylim=(0, 100), xticks=range(0,21))
ax = g.axes[0,0]
ax.axvline(x=popMean, color='#8c8ca0', ls='dashed')
ax.axhline(y=popVar, color='#8c8ca0', ls='dashed')
ax.set_title('Sample Statistics :{}'.format(i))
ax.set_facecolor(backgroundColour)
def animate(i):
df = get_data()
g = sns.lmplot( x='Mean', y='Variance', data=df, fit_reg=False, hue='Size', legend=False, palette=cmap)
prep_axes(g, i)
# initialize samples
sampleStats = []
plt.tight_layout()
ani = animation.FuncAnimation(g.fig, animate, frames = np.arange(1,100), interval=100)
问题:
1. 这只会产生一个静态图,因为我找不到更新现有 g 的方法,并在同一个 g 的图形 lmsplot 上重新绘制。 animate 函数正在创建新的 g
2. 我不得不进行一次不必要的初始化以让 g 对象将 g.fig 传递给 funcanimation,但由于第 1 点,这也无济于事。
我们如何使用 lmplot 制作动画?由于色调功能,我想使用它而不是常规的 matplotlib。
我也尝试直接使用 facetgrid(并在 g.map 中传递 lmplot),但这也无济于事。
【问题讨论】:
-
将
funcanimation与lmplot结合使用会很困难,而且可能会适得其反。但是,您似乎没有使用 lmplot 的任何功能(无回归,无方面)仅hue?自己分离色调类别会容易得多,或者考虑使用新的seaborn.scatterplot()。如果您需要更多帮助,您需要提供Minimal, Complete, and Verifiable example -
请找到它here 它是所有sn-ps的笔记本。我被困在最后一部分。现在我得到单独的静态图,而不是动画,因为每次都有单独的
g更新。请帮忙
标签: python matplotlib plotly seaborn