I have a subplot that I want to save in a PDF. I am trying to do this inside a Streamlit app and to create the PDF I am using FPDF library. The subplot is working without any problems. The problem is that I can't save it as PDF because an error is shown saying that the figure that I want to save is not defined. This is how I created the figure containing the subplot:
fig1, (ax1, ax2) = plt.subplots(1, 2)
hip_angle, _, _ = angle_calculation(merged)
hip_angle_2, _, _ = angle_calculation(merged_2)
sub_plotting(freq_left, hip_angle_2, 'Hip angle - Left leg', ax1)
sub_plotting(freq_right, hip_angle, 'Hip angle - Right leg', ax2)
ax1.set_xlabel('Gait phase %')
ax2.set_xlabel('Gait phase %')
ax1.set_ylabel('Angle (degrees)')
ax2.set_ylabel('Angle (degrees)')
ax1.set_xticks(np.arange(0, 110, step=10))
ax2.set_xticks(np.arange(0, 110, step=10))
ax1.set_ylim([-20, 50])
ax2.set_ylim([-20, 50])
ax1.set_aspect('auto')
ax2.set_aspect('auto')
fig1.set_figheight(8)
fig1.set_figwidth(18)
st.pyplot(fig1)
# Here I am just creating a button to download the figure
fig = 'Hip angle.png'
plt.savefig(fig)
with open(fig, "rb") as f:
btn = st.download_button(
label="Descarregar imagem",
data=f,
file_name=fig,
mime="image/png")
This is what I am using to try to save the figure in the PDF.
checkbox = st.checkbox('Name', value='')
if checkbox:
name = st.text_input('Nome do utente', value=' ')
st.download_button(
label="Descarregar relatório",
data=create_pdf(fig1),
file_name="Relatório.pdf",
mime="application/pdf",
)
As I mentioned, there's an error saying that the fig1
is not defined.
This is the function that I am using to create the PDF template.
def create_pdf(figure1):
pdf = FPDF()
pdf.add_page()
figure = io.BytesIO()
figure1.savefig(figure, format="png")
saved_fig = tempfile.NamedTemporaryFile()
with open(f"{saved_fig.name}.png", 'wb') as sf:
sf.write(figure.getvalue())
pdf.set_xy(30, 50)
pdf.image(figure1, w=140, h=110)
figure.close()
return bytes(pdf.output())
Here is a simplified code that works with pdf download. We create the pdf file first based on png file. Once created we use it for download.
Code
Output
Downloaded png output:
Downloaded pdf output