python - 动画散点图和等高线图

标签 python pandas matplotlib animation plot

我正在尝试生成 animated scatter plot在功能内animated contour plot 。我可以让两者单独工作,但不能一起工作。

下面的代码生成 contour来自坐标ABdf 。我试图包含一个单独的动画 scatter样本内plot使用C坐标。此尝试目前已被注释掉。

所以我基本上想包括另一个 animated scatter使用C_XC_Y 。我尝试将它们应用于 line_c

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as sts
import matplotlib.animation as animation
import matplotlib.transforms as transforms

''' Section 1 '''

DATA_LIMITS = [-85, 85]

def datalimits(*data):
    return DATA_LIMITS  # dmin - spad, dmax + spad

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)
    PDF = sts.multivariate_normal([x, y]).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)):
        X, Y, PDF = mvpdf(x, y, xlim, ylim)
        PDFs.append(PDF)

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

''' Animate Plot '''

fig, ax = plt.subplots(figsize = (10,6))
ax.set_xlim(DATA_LIMITS)
ax.set_ylim(DATA_LIMITS)

#Animated coordinates for group A,B
line_a, = ax.plot([], [], '.', c='red', alpha = 0.5, markersize=5, animated=True)
line_b, = ax.plot([], [], '.', c='blue', alpha = 0.5, markersize=5, animated=True)

#Attempt to incorporate scatter for C 
line_c, = ax.plot([], [], '.', c='white', alpha = 0.5, markersize=2.5, animated=True)

cfs = None

def plotmvs(tdf, xlim=None, ylim=None, fig=fig, ax=ax):
    global cfs  
    if cfs:
        for tp in cfs.collections:
            # Remove the existing contours
            tp.remove()

    # Get the data frame for time t
    df = tdf[1]

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

    PDFs = []

    for (group, gdf), group_line in zip(df.groupby('group'), (line_a, line_b)):
        group_line.set_data(*gdf[['X','Y']].values.T)
        X, Y, PDF = mvpdfs(gdf['X'].values, gdf['Y'].values, xlim, ylim)
        PDFs.append(PDF)

    normPDF = PDF - PDF.min()
    normPDF = normPDF / normPDF.max()
    cfs = ax.contourf(X, Y, normPDF, cmap='viridis', alpha = 1, levels=np.linspace(-1,1,10))

#Create offset scatter for Group C
#    for (group, g2df), group_line in zip(df.groupby('group'), (line_c)):    
#        group_line.set_data(*g2df[['XX','YY']].values.T)

#        offset = lambda p: transforms.ScaledTranslation(p/82.,0, plt.gcf().dpi_scale_trans)
#        trans = plt.gca().transData
#        ax.scatter(line_c,transform=trans+offset(+2))


    return cfs.collections + [line_a, line_b]#, line_c] 

n = 10
time = range(n)  

d = ({
     'A1_X' :    [13.3,13.16,12.99,12.9,12.79,12.56,12.32,12.15,11.93,11.72],
     'A1_Y' :    [26.12,26.44,26.81,27.18,27.48,27.82,28.13,28.37,28.63,28.93],
     'A2_X' :    [6.97,6.96,7.03,6.98,6.86,6.76,6.55,6.26,6.09,5.9],
     'A2_Y' :    [10.92,10.83,10.71,10.52,10.22,10.02,9.86,9.7,9.54,9.37],
     'B1_X' :    [38.35,38.1,37.78,37.55,37.36,37.02,36.78,36.46,36.21,35.79],
     'B1_Y' :    [12.55,12.58,12.58,12.55,12.5,12.47,12.43,12.48,12.44,12.44],
     'B2_X' :    [14.6,14.38,14.16,13.8,13.45,13.11,12.71,12.3,12.06,11.61],
     'B2_Y' :    [4.66,4.44,4.24,4.1,4.01,3.84,3.67,3.56,3.44,3.47],
#    'C_X' :    [10,15,18,20,30,33,35,42,34,20],
#    'C_Y' :    [10,16,20,10,20,13,15,12,14,10],                 
     })

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']

interval_ms = 200
delay_ms = 1000
ani = animation.FuncAnimation(fig, plotmvs, frames=df.groupby('time'),
            blit=True, interval=interval_ms, repeat_delay=delay_ms)

plt.show()

最佳答案

好吧,所以我必须改变一些事情:

  • 最重要的是,我关闭了 blit=True(主要是因为我使用的是 Mac,这并不完全支持,因此您的情况可能会有所不同)和 animated=True line_aline_b 的定义中。
  • 您在代码注释中创建的组“C”缺少一个整数,无法让 tuples=[…] 代码正常工作(它期望找到一个 int(k. split('_')[0][1:])
  • 散点图应该是 .scatter(),因为您没有在 FuncAnimation 中使用 init_func=init 调用,我们可以简单地在动画函数本身内“即时”创建它。
  • 我添加了第二个 for 循环,其中包含 if group=='C' 情况 - 尽管您可以更优雅地解决这个问题 - 创建散点图 分散请注意,我包含了转换,尽管我不确定这最终会为您实现什么
  • 我将 ax.contourf() 中的级别设置为区间 [0,1],但这在我看来完全是迂腐的;-)
  • 我在所有绘图项中添加了 zorder= ,以便控制在哪个 z 平面上绘制它们(可选,但很有帮助),并稍微调整了绘制线的外观(可选) ,仅用于强调)
  • 最后,您需要确保返回一个可迭代列表,因此可能“看起来很奇怪”返回 cfs.collections + [scat] + [line_a,line_b ]

绘图结果:

Complete animation

完整代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as sts
import matplotlib.animation as animation
import matplotlib.transforms as transforms
from matplotlib.lines import Line2D

''' Section 1 '''

DATA_LIMITS = [-85, 85]

def datalimits(*data):
    return DATA_LIMITS  # dmin - spad, dmax + spad

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)
    PDF = sts.multivariate_normal([x, y]).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)):
        X, Y, PDF = mvpdf(x, y, xlim, ylim)
        PDFs.append(PDF)

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

''' Animate Plot '''

fig, ax = plt.subplots(figsize = (10,6))
ax.set_xlim(DATA_LIMITS)
ax.set_ylim(DATA_LIMITS)

#Animated coordinates for group A,B
line_a, = ax.plot([], [], '-o', c='red', alpha = 0.5, markersize=5,zorder=3)
line_b, = ax.plot([], [], '-o', c='blue', alpha = 0.5, markersize=5,zorder=3)

cfs = None

def plotmvs(tdf, xlim=None, ylim=None, fig=fig, ax=ax):    
    global cfs  
    if cfs:
        for tp in cfs.collections:
            # Remove the existing contours
            tp.remove()

    # Get the data frame for time t
    df = tdf[1]

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

    PDFs = []

    for (group, gdf), group_line in zip(df.groupby('group'), (line_a, line_b)):
        group_line.set_data(*gdf[['X','Y']].values.T)
        X, Y, PDF = mvpdfs(gdf['X'].values, gdf['Y'].values, xlim, ylim)
        PDFs.append(PDF)

    for (group, gdf) in df.groupby('group'):
        if group=='C':
            offset = lambda p: transforms.ScaledTranslation(p/82.,0, plt.gcf().dpi_scale_trans)
            trans = plt.gca().transData
            scat=ax.scatter(gdf['X'].values, gdf['Y'].values, 
                    marker='o', c='white', alpha = 0.5,zorder=3,
                    transform=trans+offset(+2) )#markersize=2,zorder=3)


    normPDF = PDF - PDF.min()
    normPDF = normPDF / normPDF.max()

    cfs = ax.contourf(X, Y, normPDF, cmap='viridis', alpha = 1, levels=np.linspace(0,1,10),zorder=1)

    return  cfs.collections + [scat] + [line_a,line_b] # make sure that these are iterable!

n = 10
time = range(n)  

d = ({
    'A1_X' :    [13.3,13.16,12.99,12.9,12.79,12.56,12.32,12.15,11.93,11.72],
    'A1_Y' :    [26.12,26.44,26.81,27.18,27.48,27.82,28.13,28.37,28.63,28.93],
    'A2_X' :    [6.97,6.96,7.03,6.98,6.86,6.76,6.55,6.26,6.09,5.9],
    'A2_Y' :    [10.92,10.83,10.71,10.52,10.22,10.02,9.86,9.7,9.54,9.37],
    'B1_X' :    [38.35,38.1,37.78,37.55,37.36,37.02,36.78,36.46,36.21,35.79],
    'B1_Y' :    [12.55,12.58,12.58,12.55,12.5,12.47,12.43,12.48,12.44,12.44],
    'B2_X' :    [14.6,14.38,14.16,13.8,13.45,13.11,12.71,12.3,12.06,11.61],
    'B2_Y' :    [4.66,4.44,4.24,4.1,4.01,3.84,3.67,3.56,3.44,3.47],
    'C1_X' :    [10.,15.,18.,20.,30.,33.,35.,42.,34.,20.],## name contains an int so that tuples=... list works!
    'C1_Y' :    [10.,16.,20.,10.,20.,13.,15.,12.,14.,10.],
})

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']

interval_ms = 200
delay_ms = 1000
ani = animation.FuncAnimation(fig, plotmvs,  frames=df.groupby('time'), interval=interval_ms, repeat_delay=delay_ms,)

plt.show()

更新

如果您希望有一个单个移动点,同时最小化/简化代码行,您还可以使用以下方式启动绘图元素:

line_a, = ax.plot([], [], '-o', c='red', alpha = 0.5, markersize=5,zorder=3)
line_b, = ax.plot([], [], '-o', c='blue', alpha = 0.5, markersize=5,zorder=3)
lines=[line_a,line_b] ## this is iterable!

offset = lambda p: transforms.ScaledTranslation(p/82.,0, plt.gcf().dpi_scale_trans)
trans = plt.gca().transData

scat = ax.scatter([], [], s=5**2,marker='o', c='white', alpha = 0.5,zorder=3,transform=trans+offset(+2) )
scats=[scat] ## this is iterable, too!

然后在plotmvs()中:

for (group, gdf), group_line in zip(df.groupby('group'), lines+scats):
        if group in ['A','B']:
            group_line.set_data(*gdf[['X','Y']].values.T)
            X, Y, PDF = mvpdfs(gdf['X'].values, gdf['Y'].values, xlim, ylim)
            PDFs.append(PDF)
        elif group in ['C']:
            x,y=(gdf['X'].values, gdf['Y'].values)
            scat.set_offsets( gdf[['X','Y']].values )

请注意,更新散点图和线图使用不同的函数和坐标列表!

最后更新返回值:

return  cfs.collections + scats + lines # make sure that these are iterable!

这会产生以下动画:

Secondary plot option

关于python - 动画散点图和等高线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55624400/

相关文章:

python - Pandas Dataframe 过滤器不工作,但 str.match() 正在工作

python - 移动脊柱后有效缓存和恢复 matplotlib 轴参数

python - 在 Pandas 数据框中对子组中的行进行排名的更快方法

python - 如何减少拼接图像之间的颜色差异以实现平滑的颜色过渡?

python - 我在 GEKKO 中遇到循环约束问题

Python 3.6 : New line command '/n' is ignored when writing to a file on AWS Ubuntu

python - 在最小值之前的每列值中找到最大值

python - 不能使用 matplotlib.use ('Agg' ),图形总是显示在屏幕上

grid - 通过网格在Tkinter中显示Matplotlib导航工具栏

python - 在 while 循环中调用时 Beautifulsoup 返回相同的结果