Hi - I am hoping to create an animated scatterplot that plots two continuous variables over time, and uses a slider-animation similar to what is found in the tutorial at the bottom of this link to visualize the points moving across time. Link below:
The one addition that I am hoping to add is to include trailing lines for each datapoint as the animation progresses to better show the progress of each line. A good example of the trailing line can be found in this chart:
I thought that this could be accomplished by simply changing âmodeâ: âmarkersâ, to âmodeâ: âlines+markersâ, but this did not work and returns the same graph. Do you know the next best steps in order to change our script so that we can achieve the trailing line effect? Do we need to change our layout from filling in python dictionaries, to using the go.layout, go.scatter, and go.frame format?
Any help would be massively appreciated! Script for plotting the graph without generating trailing lines is pasted below.
make figure
figure = {
âdataâ: ,
âlayoutâ: {},
âframesâ:
}
config = {âscrollzoomâ: True}
fill in most of layout
figure[âlayoutâ][âtitleâ] = {âtextâ:âDaily Frequency of Key Covid-19 Hashtags plotted against cumulative total of tweets per hashtagâ}
figure[âlayoutâ][âxaxisâ] = {âtitleâ: âtotal tweetsâ, âtypeâ: âlogâ, ârangeâ: [0,5]}
figure[âlayoutâ][âyaxisâ] = {âtitleâ: âword countâ,âtypeâ: âlogâ, ârangeâ: [0,3]}
figure[âlayoutâ][âhovermodeâ] = âclosestâ
figure[âlayoutâ][âslidersâ] = {
âargsâ: [
âslider.valueâ, {
âdurationâ: 400,
âeaseâ: âcubic-in-outâ
}
],
âinitialValueâ: â20191001â,
âplotlycommandâ: âanimateâ,
âvaluesâ: years,
âvisibleâ: True
}
figure[âlayoutâ][âupdatemenusâ] = [
{
âbuttonsâ: [
{
âargsâ: [None, {âframeâ: {âdurationâ: 500, âredrawâ: False},
âfromcurrentâ: True, âtransitionâ: {âdurationâ: 300, âeasingâ: âquadratic-in-outâ}}],
âlabelâ: âPlayâ,
âmethodâ: âanimateâ
},
{
âargsâ: [[None], {âframeâ: {âdurationâ: 0, âredrawâ: False}, âmodeâ: âimmediateâ,
âtransitionâ: {âdurationâ: 0}}],
âlabelâ: âPauseâ,
âmethodâ: âanimateâ
}
],
âdirectionâ: âleftâ,
âpadâ: {ârâ: 10, âtâ: 87},
âshowactiveâ: False,
âtypeâ: âbuttonsâ,
âxâ: 0.1,
âxanchorâ: ârightâ,
âyâ: 0,
âyanchorâ: âtopâ
}
]
sliders_dict = {
âactiveâ: 0,
âyanchorâ: âtopâ,
âxanchorâ: âleftâ,
âcurrentvalueâ: {
âfontâ: {âsizeâ: 20},
âprefixâ: âYear:â,
âvisibleâ: True,
âxanchorâ: ârightâ
},
âtransitionâ: {âdurationâ: 300, âeasingâ: âcubic-in-outâ},
âpadâ: {âbâ: 10, âtâ: 50},
âlenâ: 0.9,
âxâ: 0.1,
âyâ: 0,
âstepsâ:
}
size = [1]
make data
year = 20200110
for word in words:
dataset_by_year = dataset[dataset[âdatestringâ] == year]
dataset_by_year_and_word=dataset_by_year[dataset_by_year[âwordâ] == word]
data_dict = {
'x': list(dataset_by_year_and_word['word_cumu_sum']),
'y': list(dataset_by_year_and_word['word_count']),
'mode': 'markers',
#'text': list(dataset_by_year_and_gov['cases']),
'marker': {
# 'sizemode': 'area',
# 'sizeref': 2.*max(size)/(40.**.5),
'size':20
},
'name': word,
'type': 'scatter',
'showlegend': True
}
figure['data'].append(data_dict)
make frames
for year in years:
frame = {âdataâ: , ânameâ: str(year)}
for word in words:
dataset_by_year = dataset[dataset[âdatestringâ] == int(year)]
dataset_by_year_and_word=dataset_by_year[dataset_by_year['word'] == word]
data_dict = {
'x': list(dataset_by_year_and_word['word_cumu_sum']),
'y': list(dataset_by_year_and_word['word_count']),
'mode': 'markers',
#'text': list(dataset_by_year_and_gov['cases']),
'marker': {
# 'sizemode': 'area',
# 'sizeref': 2.*max(size)/(40.**.5),
'size':20
},
'name': word,
'type': 'scatter',
'showlegend': True
}
frame['data'].append(data_dict)
figure['frames'].append(frame)
slider_step = {'args': [
[year],
{'frame': {'duration': 300, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 300}}
],
'label': year,
'method': 'animate'}
sliders_dict['steps'].append(slider_step)
from plotly.offline import init_notebook_mode, iplot
from IPython.display import display, HTML
import pandas as pd
init_notebook_mode(connected=True)
figure[âlayoutâ][âslidersâ] = [sliders_dict]
iplot(figure, config=config)