Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export/save an animated bubble chart made with plotly?

How to export/save an animated bubble chart made with plotly? (For instance, the one was made following the link below) I would like to have it in a gift or some format with better resolution. Thank you in advance.

https://www.kaggle.com/aashita/guide-to-animated-bubble-charts-using-plotly

like image 368
Esperanza González Avatar asked Dec 24 '22 00:12

Esperanza González


2 Answers

There is no possibility to do this in plotly. Please use gifmaker and save every step in the animation as a single picture to later combine them to a gif. Follow this source. Further explanation on how to create animations is provided by plotly here.

The basic way to go would be to integrate this logic into the animation process of your plotly code:

 import ImageSequence
 import Image
 import gifmaker
 sequence = []

 im = Image.open(....)

 # im is your original image
 frames = [frame.copy() for frame in ImageSequence.Iterator(im)]

 # write GIF animation
 fp = open("out.gif", "wb")
 gifmaker.makedelta(fp, frames)
 fp.close()

If you could provide your actual code, it would be possible to provide a more detailled answer to your question. :)

like image 102
Mike_H Avatar answered Dec 27 '22 05:12

Mike_H


Another possibility is to use the gif library, which works with matplolib, altair and plotly, and is really straight forward. In this case you would not use a plotly animation. Instead, you define a function that returns a plotly fig and construct a list of figs to pass to gif as an argument.

Your code would look something like this:

import random
import plotly.graph_objects as go
import pandas as pd
import gif

# Pandas DataFrame with random data
df = pd.DataFrame({
    't': list(range(10)) * 10,
    'x': [random.randint(0, 100) for _ in range(100)],
    'y': [random.randint(0, 100) for _ in range(100)]
})

# Gif function definition
@gif.frame
def plot(i):
    d = df[df['t'] == i]
    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=d["x"],
        y=d["y"],
        mode="markers"
    ))
    fig.update_layout(width=500, height=300)
    return fig

# Construct list of frames
frames = []
for i in range(10):
    frame = plot(i)
    frames.append(frame)

# Save gif from frames with a specific duration for each frame in ms
gif.save(frames, 'example.gif', duration=100)
like image 41
Manuel Montoya Avatar answered Dec 27 '22 06:12

Manuel Montoya