mjspier
mjspier

Reputation: 6536

matplotlib plot has bad resolution in streamlit

In a streamlit app when trying to plot figures with a different format than the standard format I get very bad resolutions.

Example:

import pandas as pd
import numpy as np
import streamlit as st

index = pd.date_range(start='2019-01-01', periods=31)
data = np.random.randint(0,100,size=(len(index)))
df = pd.DataFrame(index=index, data=data)
df.plot(figsize=(25, 5), antialiased=True)
st.pyplot(dpi=100)

Results in a plot like this:

enter image description here

The same plot in a jupyter notebook with the same figsize does not have this issue. Why is this happening and how can I fix this issue?

Upvotes: 5

Views: 4287

Answers (1)

user62241
user62241

Reputation: 154

I'm copying and pasting in the response from the Streamlit Forum you received for this question below:

"Hey @mjspier - thanks for checking out Streamlit!

The figsize param values are in inches, so in this case you’re generating an image that’s intended to be 25 inches wide. Streamlit is then shrinking that image down to Streamlit’s maximum displayable width, which is about 1500 pixels, hence the blurriness.

An easy fix is to pass a small figsize. (Each inch in figsize is ~1000 pixels, so a figsize of (15, 3) will look less blurry.)

I’ve also opened a bug about this, so that we at least do something less unexpected than showing an ugly image when figsize is very large."

Hope this helps!

Upvotes: 5

Related Questions