Download figure as PDF with color?

I am quite new to streamlit. I am running my app locally. I have created a figure using plotly, and I have added a “download as PDF”-button just below. The figure appears in color, but when downloaded, the PDF is in black and white. Is it possible to do this? How would I adjust the code?

Here is my code:
fig = px.bar(combined_data, x=‘year’, y=‘publication’, color=‘Type’, barmode=‘group’,
title=‘Publications Over Time’,
labels={‘year’: ‘Year’, ‘publication’: ‘Publications’})
fig.update_layout(
xaxis=dict(
tickmode=‘linear’,
tick0=min(data[‘year’].min(), data2[‘year’].min()),
dtick=1
),
legend=dict(
orientation=‘h’, # Horizontal legend
yanchor=‘top’, # Anchor the legend at the top
y=-0.2, # Position the legend below the graph
xanchor=‘center’, # Center the legend horizontally
x=0.5 # Align the legend at the center of the x-axis
)
)

    # Display the figure in Streamlit
    st.plotly_chart(fig)


# Save the figure to a PDF buffer
pdf_buffer = io.BytesIO()
fig.write_image(pdf_buffer, format='pdf')

# Reset the buffer position to the beginning
pdf_buffer.seek(0)

# Add a button to download the figure as a PDF
st.download_button(
    label="Download as PDF",
    data=pdf_buffer,
    file_name="vetu_figure.pdf",
    mime="application/pdf"
)

Thank you for any help!

That’s a shortcoming of the theme that streamlit enforces as the default. Passing an actual Plotly theme to plotly.express can help with that issue:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.