question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Matplotlib plots are blurry when using a large figsize

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
tvstcommented, Feb 23, 2021

Since this was posted 1.5 years ago (😱), we added some arguments to st.image to give developers more control. Taking that work as inspiration, I think the best solution here would be to give developers the same level of control for st.pyplot as we provide for st.image. In fact, we should support the exact same resolution-related parameters for pyplot as for image.

This means st.pyplot would take the following parameters:

  • width: None or int
  • use_column_width: ‘auto’ | ‘always’ | ‘never’. And also accept None | True | False for historical reasons.
  • output_format: ‘JPEG’ | ‘PNG’ | ‘auto’. Where ‘auto’ is the same as ‘PNG’ in this case. And we could also add support for ‘SVG’, since Matplotlib supports that. NOTE: I realize this one is not related to the bug reported here. But might as well add it to the mix, since there are good usecases for this

In addition, we should explicitly document the fact that you can pass dpi=[some number] to st.pyplot() to change the pixel density. (This is automatically supported today because st.plot() forwards its kwargs to Matplotlib’s Figure.savefig() (see docs)

IMPORTANT: When doing this we should make sure to not change the default behavior of st.pyplot!

3reactions
kurt-rheecommented, Oct 1, 2020

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

2 ways to improve the default resolution of matplotlib plots ...
Solution 1. Change the default dpi settings in matplotlib. By default the plots rendered in our notebooks are png format with a relatively...
Read more >
Matplotlib plots are blurry - Using Streamlit
An easy fix is to pass a small figsize. (Each inch in figsize is ~1000 pixels, so a figsize of (15, 3) will...
Read more >
inline images have low quality - matplotlib - Stack Overflow
I've tried using matplotlib's plt. imshow() as well, which has the exact same result. I'm starting the notebook with ipython notebook --pylab ...
Read more >
How do you improve Matplotlib image quality? - Tutorialspoint
Set the figure size and adjust the padding between and around the subplots. · Make a 2D data raster using a np.array. ·...
Read more >
How to Change Plot and Figure Size in Matplotlib - Datagy
One of the simplest and most expressive ways of changing the plot size in Matplotlib is to use the figsize= argument. As the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found