Streamlit: Matplotlib plots are blurry when using a large figsize

Created on 4 Dec 2019  路  3Comments  路  Source: streamlit/streamlit

For example:

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

np.random.seed(0)
df = pd.DataFrame(np.random.normal(1, 1, size=100))
df.plot(figsize=(25, 5))
st.pyplot()

Results in this unreadable image:

download

We currently clamp st.pyplot images to Streamlit's MAXIMUM_CONTENT_WIDTH; images bigger than this get downscaled.

See pyplot.marshall():

image_proto.marshall_images(
        image, None, -2, new_element_proto.imgs, False, channels="RGB", format="PNG"
    )

That -2 value is a magic number that causes marshall_images to do the clamping/downscaling. We could consider either passing a value of 0 (which would not do any downscaling), or we could warn the user if their image is much bigger than MAXIMUM_CONTENT_WIDTH.

Also, we should definitely turn these magic width values into constants:

ORIGINAL_WIDTH = 0
CLAMP_TO_MAX_CONTENT_WIDTH = -1

(or whatever.)

This issue was originally raised in the Streamlit forums, here: https://discuss.streamlit.io/t/matplotlib-plots-are-blurry/1224

bug charts uui

Most helpful comment

I've found that in my plots I can change the resolution by increasing the figsize, but the fig always spans the entire width of the page. Is there a way to turn off the auto scaling for pyplot?

All 3 comments

I think we'd better give most control of displayed plots to users, what we need to do is only displaying the plot as is. Expose some constants is one possible way, but follow the way that st.Image() did is simpler and more consistent. Can I fix this one?

Per https://github.com/streamlit/streamlit/issues/1350, this also makes the pyplot dpi parameter essentially meaningless.

I've found that in my plots I can change the resolution by increasing the figsize, but the fig always spans the entire width of the page. Is there a way to turn off the auto scaling for pyplot?

Was this page helpful?
0 / 5 - 0 ratings