I have this code, and I want to upload the output PNG figure to the S3 bucket:
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
N = 100
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sz = np.random.rand(N) * 30
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y,
mode="markers",
marker=go.scatter.Marker(
size=sz,
color=colors,
opacity=0.6,
colorscale="Viridis"
)
))
fig.write_image("fig1.png")
How can I do that? FYI
fig.write_image("fig1.png")
will convert the Plotly figure to a PNG image
CodePudding user response:
You can do that by using the .put_object()
method.
# generate an S3 key for the resulting plot
Bucket_Name = 'your_S3_Bucket_name'
image_name = 'TestImage.png'
# Write the image on the S3
img_data = BytesIO()
plot.write_image(img_data, format='png')
img_data.seek(0)
# put plot in S3 bucket
bucket = boto3.resource('s3').Bucket(Bucket_Name)
bucket.put_object(Body=img_data, ContentType='image/png', Key=image_name)