Анимация Scatterplot - PullRequest
       5

Анимация Scatterplot

0 голосов
/ 27 марта 2020

Я спрашивал об этом раньше, но с тех пор я добился определенного прогресса.

У меня есть набор координат для ячеек в массиве, которые я хотел бы построить и анимировать изменение цвета с течением времени, используя некоторый образец данные, которые я покажу вам, что я сделал до сих пор:

%matplotlib inline
import numpy as np
import pandas as pd 
import seaborn as sns

##original data, cell positions
cells = ['test','Cluster00424','Cluster00132','Cluster00177','Cluster00541','Cluster00457','Cluster00520','Cluster00390','Cluster00180']
xpos = [70, 0.666887652,0.691425629,0.910386764,1.481886372,1.555952031,2.043722713,2.440762609,2.50638909]
ypos = [70, 42.31267232,51.32300711,1.944227894,61.48178309,51.55425106,60.60706093,59.08879992,62.08540864]

positions = pd.DataFrame(list(zip(cells,xpos,ypos)),columns=['Name','X position', 'Y position'])

## times when the cells are active 
##starting times
test_start=list(range(0,170))
Cluster00424_start=[1, 3, 21, 31, 38, 55, 56, 61, 64, 66, 71, 72, 76, 86, 88, 90, 91, 104, 110, 111, 136, 140, 145, 147, 148]
Cluster00132_start=[10, 12, 35, 45, 47, 51, 55, 57, 66, 72, 77, 84, 90, 95, 96, 126, 140, 157, 169]
Cluster00177_start=[16, 21, 22, 38, 39, 41, 47, 64, 70, 71, 72, 83, 97, 111, 119, 124, 128, 136, 143, 148, 149, 151, 153, 165, 169]
Cluster00541_start=[13, 25, 28, 30, 48, 51, 53, 54, 56, 57, 58, 59, 78, 93, 94, 102, 104, 117, 125, 142, 151, 153, 160, 164, 166]
Cluster00457_start=[16, 21, 22, 38, 39, 41, 47, 64, 70, 71, 72, 83, 97, 111, 119, 124, 128, 136, 143, 148, 149, 151, 153, 165, 169]
Cluster00520_start=[2, 5, 11, 17, 25, 47, 51, 53, 58, 60, 65, 77, 78, 83, 87, 92, 97, 102, 108, 117, 133, 144, 155, 162, 164]
Cluster00390_start=[3, 7, 21, 24, 28, 40, 48, 53, 57, 63, 66, 67, 77, 79, 83, 91, 92, 114, 119, 122, 124, 126, 128, 139, 169]
Cluster00180_start=[8, 13, 16, 17, 18, 40, 56, 57, 64, 68, 78, 81, 82, 91, 92, 102, 116, 136, 143, 147, 148, 152, 154, 161, 163]

##end times
test_end=[x+0.7 for x in test_start]
Cluster00424_end=[x+0.7 for x in Cluster00424_start]
Cluster00132_end=[x+0.7 for x in Cluster00132_start]
Cluster00177_end=[x+0.7 for x in Cluster00177_start]
Cluster00541_end=[x+0.7 for x in Cluster00541_start]
Cluster00457_end=[x+0.7 for x in Cluster00457_start]
Cluster00520_end=[x+0.7 for x in Cluster00520_start]
Cluster00390_end=[x+0.7 for x in Cluster00390_start]
Cluster00180_end=[x+0.7 for x in Cluster00180_start]



      Name  X position  Y position
0          test   70.000000   70.000000
1  Cluster00424    0.666888   42.312672
2  Cluster00132    0.691426   51.323007
3  Cluster00177    0.910387    1.944228
4  Cluster00541    1.481886   61.481783
5  Cluster00457    1.555952   51.554251
6  Cluster00520    2.043723   60.607061
7  Cluster00390    2.440763   59.088800
8  Cluster00180    2.506389   62.085409

Мне удалось составить таблицу, показывающую цвета для каждой данной временной отметки (я, вероятно, сделал это слишком сложно, но это лучшее, что я мог сделать делать), я бы скорее нуждался в другом способе, когда временные метки используют одну и ту же систему десятичных знаков (до 4 десятичных знаков) и разметка от начала до конца, но сейчас я могу жить с этим)

## here I made a table that had a k (black) for non active times and b (blue) for active times
for cell_index in range(len(cells)):
    command = ''
    command = str(cells[cell_index])+'_dur =' + str(cells[cell_index]) +'_start+'+ str(cells[cell_index]) + '_end'
    exec(command)
    command2 = ''
    command2 = str(cells[cell_index])+'_dur = sorted(' + str(cells[cell_index]) +'_dur)'
    exec(command2)
    command3 = ''
    command3 = str(cells[cell_index]) +' = []'
    exec(command3)
    command4 = ''
    command4 = 'for i in range(len(' + str(cells[cell_index]) + '_dur)):' + str(cells[cell_index])+ '.append("k")'
    exec(command4)
    command5 = ''
    command5 = str(cells[cell_index]) + '_reddur = pd.DataFrame(' + str(cells[cell_index]) + ', index='+ str(cells[cell_index]) + '_dur)'
    exec(command5)

cells_reddur = [x + "_reddur , " for x in cells]
cells_reddur_str = ' '.join([str(elem) for elem in cells_reddur]) 
command4 = 'reddur = pd.concat([' +str(cells_reddur_str)+'],axis=1)'
exec(command4)

reddur.columns = cells
reddur = reddur.replace(np.nan, 'b', regex=True)

test Cluster00424 Cluster00132 Cluster00177 Cluster00541 Cluster00457  \
0.0      k            b            b            b            b            b   
0.7      k            b            b            b            b            b   
1.0      k            k            b            b            b            b   
1.7      k            k            b            b            b            b   
2.0      k            b            b            b            b            b   
...    ...          ...          ...          ...          ...          ...   
167.7    k            b            b            b            b            b   
168.0    k            b            b            b            b            b   
168.7    k            b            b            b            b            b   
169.0    k            b            k            k            b            k   
169.7    k            b            k            k            b            k   

      Cluster00520 Cluster00390 Cluster00180  
0.0              b            b            b  
0.7              b            b            b  
1.0              b            b            b  
1.7              b            b            b  
2.0              k            b            b  
...            ...          ...          ...  
167.7            b            b            b  
168.0            b            b            b  
168.7            b            b            b  
169.0            b            k            b  
169.7            b            k            b  

[340 rows x 9 columns]

Итак, вот как это выглядит, когда я строю значения первой отметки времени:

import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
col = reddur.iloc[0]
datatoplot = pd.DataFrame(list(zip(cells,xpos,ypos,col)),columns=['Name','X position', 'Y position', 'Color'])

ax = sns.scatterplot(x="X position", y="Y position", hue='Color', data= datatoplot)

scatterplot Я обнаружил, что должен построить все точки времени отдельно и сохранить их как изображение, а затем свяжите их вместе в GIF.

Есть ли более простой способ сделать это?

Большое спасибо,

Гергей

...