python散点图animation_python-动画轮廓和散点图

我正在尝试从一组xy坐标中设置散点和双变量高斯分布的动画.我将记录特定的代码,该代码首先调用分散和分布,然后再记录如何测量分布.

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import scipy.stats as sts

import matplotlib.animation as animation

''' Below is a section of the script that generates the scatter and contour '''

fig, ax = plt.subplots(figsize = (10,4))

def plotmvs(df, xlim=None, ylim=None, fig=fig, ax=ax):

if xlim is None: xlim = datalimits(df['X'])

if ylim is None: ylim = datalimits(df['Y'])

PDFs = []

for (group,gdf),color in zip(df.groupby('group'), ('red', 'blue')):

ax.plot(*gdf[['X','Y']].values.T, '.', c=color, alpha = 0.5)

kwargs = {

'xlim': xlim,

'ylim': ylim

}

X, Y, PDF = mvpdfs(gdf['X'].values, gdf['Y'].values, **kwargs)

PDFs.append(PDF)

PDF = PDFs[0] - PDFs[1]

normPDF = PDF - PDF.min()

normPDF = normPDF/normPDF.max()

cfs = ax.contourf(X, Y, normPDF, levels=100, cmap='jet')

return fig, ax

n = 10

time = [1]

d = ({

'A1_Y' : [10,20,15,20,25,40,50,60,61,65],

'A1_X' : [15,10,15,20,25,25,30,40,60,61],

'A2_Y' : [10,13,17,10,20,24,29,30,33,40],

'A2_X' : [10,13,15,17,18,19,20,21,26,30],

'A3_Y' : [11,12,15,17,19,20,22,25,27,30],

'A3_X' : [15,18,20,21,22,28,30,32,35,40],

'A4_Y' : [15,20,15,20,25,40,50,60,61,65],

'A4_X' : [16,20,15,30,45,30,40,10,11,15],

'B1_Y' : [18,10,11,13,18,10,30,40,31,45],

'B1_X' : [17,20,15,10,25,20,10,12,14,25],

'B2_Y' : [13,10,14,20,21,12,30,20,11,35],

'B2_X' : [12,20,16,22,15,20,10,20,16,15],

'B3_Y' : [15,20,15,20,25,10,20,10,15,25],

'B3_X' : [18,15,13,20,21,10,20,10,11,15],

'B4_Y' : [19,12,15,18,14,19,13,12,11,18],

'B4_X' : [20,10,12,18,17,15,13,14,19,13],

})

tuples = [((t, k.split('_')[0][0], int(k.split('_')[0][1:]), k.split('_')[1]), v[i]) for k,v in d.items() for i,t in enumerate(time)]

df = pd.Series(dict(tuples)).unstack(-1)

df.index.names = ['time', 'group', 'id']

for time,tdf in df.groupby('time'):

plotmvs(tdf)

'''MY ATTEMPT AT ANIMATING THE PLOT '''

def animate(i) :

tdf.set_offsets([[tdf.iloc[0:,1][0+i][0], tdf.iloc[0:,0][0+i][0]], [tdf.iloc[0:,1][0+i][1], tdf.iloc[0:,0][0+i][1]], [tdf.iloc[0:,1][0+i][2], tdf.iloc[0:,0][0+i][2]], [tdf.iloc[0:,1][0+i][3], tdf.iloc[0:,0][0+i][3]], [tdf.iloc[0:,1][0+i][4], tdf.iloc[0:,0][0+i][4]]])

normPDF = n[i,:,0,:].T

cfs.set_data(X, Y, normPDF)

ani = animation.FuncAnimation(fig, animate, np.arange(0,10),# init_func = init,

interval = 10, blit = False)

有关如何使用单个框架生成和绘制分布的完整工作代码

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import scipy.stats as sts

import matplotlib.animation as animation

def datalimits(*data, pad=.15):

dmin,dmax = min(d.min() for d in data), max(d.max() for d in data)

spad = pad*(dmax - dmin)

return dmin - spad, dmax + spad

def rot(theta):

theta = np.deg2rad(theta)

return np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

def getcov(radius=1, scale=1, theta=0):

cov = np.array([

[radius*(scale + 1), 0],

[0, radius/(scale + 1)]

])

r = rot(theta)

return r @ cov @ r.T

def mvpdf(x, y, xlim, ylim, radius=1, velocity=0, scale=0, theta=0):

X,Y = np.meshgrid(np.linspace(*xlim), np.linspace(*ylim))

XY = np.stack([X, Y], 2)

x,y = rot(theta) @ (velocity/2, 0) + (x, y)

cov = getcov(radius=radius, scale=scale, theta=theta)

PDF = sts.multivariate_normal([x, y], cov).pdf(XY)

return X, Y, PDF

def mvpdfs(xs, ys, xlim, ylim, radius=None, velocity=None, scale=None, theta=None):

PDFs = []

for i,(x,y) in enumerate(zip(xs,ys)):

kwargs = {

'xlim': xlim,

'ylim': ylim

}

X, Y, PDF = mvpdf(x, y,**kwargs)

PDFs.append(PDF)

return X, Y, np.sum(PDFs, axis=0)

fig, ax = plt.subplots(figsize = (10,4))

def plotmvs(df, xlim=None, ylim=None, fig=fig, ax=ax):

if xlim is None: xlim = datalimits(df['X'])

if ylim is None: ylim = datalimits(df['Y'])

PDFs = []

for (group,gdf),color in zip(df.groupby('group'), ('red', 'blue')):

#Animate this scatter

ax.plot(*gdf[['X','Y']].values.T, '.', c=color, alpha = 0.5)

kwargs = {

'xlim': xlim,

'ylim': ylim

}

X, Y, PDF = mvpdfs(gdf['X'].values, gdf['Y'].values, **kwargs)

PDFs.append(PDF)

PDF = PDFs[0] - PDFs[1]

normPDF = PDF - PDF.min()

normPDF = normPDF/normPDF.max()

#Animate this contour

cfs = ax.contourf(X, Y, normPDF, levels=100, cmap='jet')

return fig, ax

n = 10

time = [1]

d = ({

'A1_Y' : [10,20,15,20,25,40,50,60,61,65],

'A1_X' : [15,10,15,20,25,25,30,40,60,61],

'A2_Y' : [10,13,17,10,20,24,29,30,33,40],

'A2_X' : [10,13,15,17,18,19,20,21,26,30],

'A3_Y' : [11,12,15,17,19,20,22,25,27,30],

'A3_X' : [15,18,20,21,22,28,30,32,35,40],

'A4_Y' : [15,20,15,20,25,40,50,60,61,65],

'A4_X' : [16,20,15,30,45,30,40,10,11,15],

'B1_Y' : [18,10,11,13,18,10,30,40,31,45],

'B1_X' : [17,20,15,10,25,20,10,12,14,25],

'B2_Y' : [13,10,14,20,21,12,30,20,11,35],

'B2_X' : [12,20,16,22,15,20,10,20,16,15],

'B3_Y' : [15,20,15,20,25,10,20,10,15,25],

'B3_X' : [18,15,13,20,21,10,20,10,11,15],

'B4_Y' : [19,12,15,18,14,19,13,12,11,18],

'B4_X' : [20,10,12,18,17,15,13,14,19,13],

})

tuples = [((t, k.split('_')[0][0], int(k.split('_')[0][1:]), k.split('_')[1]), v[i]) for k,v in d.items() for i,t in enumerate(time)]

df = pd.Series(dict(tuples)).unstack(-1)

df.index.names = ['time', 'group', 'id']

for time,tdf in df.groupby('time'):

plotmvs(tdf)

我本质上是想通过遍历xy坐标的每一行来动画化此代码.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值