0

I set my web app in streamlit to Wide Mode. As a result, the plotted graph fills the entire page, making it very large. Is there any way to plot it as if it were out of Wide Mode, or any way to decrease it? I would like it to be in a size that looks good on both the computer and mobile.

            fig1 = plt.figure()
            plt.subplot(1, 1, 1)
            plt.axhline(0, linewidth=0.3, color='white')
            plt.plot(1.477*r, v(u, l), color="white")
            plt.plot([1.477/umin,1.477/umax],[vmin,vmax],'bo', color="gold")
            plt.xlabel("r [km]")
            plt.axis([0, 1.477*rmax, -0.5, vlim + 0.1])
            ax = plt.gca()
            ax.spines['bottom'].set_color('white')
            ax.tick_params(axis='x', colors='white')
            ax.tick_params(axis='y', colors='white')
            ax.spines['top'].set_color('white') 
            ax.spines['right'].set_color('white')
            ax.spines['left'].set_color('white')
            ax.xaxis.label.set_color('white')
            ax.yaxis.label.set_color('white')
            fig1.patch.set_facecolor('#0E1117')
            ax.set_facecolor("black")
            plt.show()
            st.pyplot(fig1)

enter image description here

User8563
  • 123
  • 1
  • 2
  • 13

2 Answers2

1

We are not allowed to control the width of the figure in streamlit.
Although there is a work around:
Step 1: plot a fig while passing plt.subplot(figsize=(width,height)) argument.
Step 2: Save the fig fig1.savefig("figure_name.png")
Step 3: Show the image using st.image

from PIL import Image
image = Image.open('figure_name.png')
st.image(image)
micro5
  • 415
  • 3
  • 6
1

You can find a minimal example based on a matplotlib tutorial to vary the values of the width and height parameters in figsize using sliders from the Cannot change matplotlib figure size

import streamlit as st
import matplotlib.pyplot as plt

cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
activity = ["combing", "drinking", "feeding", "napping", "playing", "washing"]

width = st.sidebar.slider("plot width", 1, 25, 3)
height = st.sidebar.slider("plot height", 1, 25, 1)

fig, ax = plt.subplots(figsize=(width, height))
ax.plot(activity, dog, label="dog")
ax.plot(activity, cat, label="cat")
ax.legend()

st.pyplot(fig)

or use st.columns

To Insert containers laid out as side-by-side columns. Inserts a number of multi-element containers laid out side-by-side and returns a list of container objects.

Examples

Ailurophile
  • 2,552
  • 7
  • 21
  • 46