Esp*_*lez 4 charts animation plotly
如何导出/保存用 plotly 制作的动画气泡图?(例如,一个是按照下面的链接制作的)我想把它做成礼物或一些分辨率更高的格式。先感谢您。
https://www.kaggle.com/aashita/guide-to-animated-bubble-charts-using-plotly
没有可能在情节中做到这一点。请使用givemaker动画中的每个步骤并将其保存为单个图片,以便稍后将它们组合为 gif。按照这个来源。plotly here提供了有关如何创建动画的进一步说明。
基本的方法是将此逻辑集成到情节代码的动画过程中:
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()
Run Code Online (Sandbox Code Playgroud)
如果您可以提供实际代码,则可以为您的问题提供更详细的答案。:)
另一种可能性是使用gif库,它与 matplolib、altair 和plotly 一起使用,而且非常简单。在这种情况下,您不会使用情节动画。相反,您定义一个返回绘图无花果的函数,并构造一个无花果列表以作为参数传递给 gif。
你的代码看起来像这样:
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)
Run Code Online (Sandbox Code Playgroud)
以已经提供的答案为基础。这会从具有帧的绘图图形生成动画 GIF (动画)
import plotly.express as px
import pandas as pd
import numpy as np
import io
import PIL
r = np.random.RandomState(42)
# sample data
df = pd.DataFrame(
{
"step": np.repeat(np.arange(0, 8), 10),
"x": np.tile(np.linspace(0, 9, 10), 8),
"y": r.uniform(0, 5, 80),
}
)
# smaple plotly animated figure
fig = px.bar(df, x="x", y="y", animation_frame="step")
# generate images for each step in animation
frames = []
for s, fr in enumerate(fig.frames):
# set main traces to appropriate traces within plotly frame
fig.update(data=fr.data)
# move slider to correct place
fig.layout.sliders[0].update(active=s)
# generate image of current state
frames.append(PIL.Image.open(io.BytesIO(fig.to_image(format="png"))))
# create animated GIF
frames[0].save(
"test.gif",
save_all=True,
append_images=frames[1:],
optimize=True,
duration=500,
loop=0,
)
Run Code Online (Sandbox Code Playgroud)