# st.pyplot

Source: https://docs.streamlit.io/develop/api-reference/charts/st.pyplot

* Function signature:

   ```python
   st.pyplot(fig, clear_figure=False, *, width="stretch", use_container_width=None, alt=None, **kwargs)
   ```

* Parameters:

   | name | type | default | description |
   |---|---|---|---|
   | fig | Matplotlib Figure |  | The Matplotlib Figure object to render. See https://matplotlib.org/stable/gallery/index.html for examples. Pandas' DataFrame.plot() returns an Axes rather than a Figure, so pass its .get_figure() result. |
   | clear_figure | bool |  | Whether to clear the figure after rendering it. If this is False (default), the figure is left as-is. If True, Streamlit calls fig.clf() after the figure is displayed. |
   | width | "stretch", "content", or int | centered | The width of the chart element. This can be one of the following:  "stretch" (default): The width of the element matches the width of the parent container. "content": The width of the element matches the width of its content, but doesn't exceed the width of the parent container. An integer specifying the width in pixels: The element has a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container. Streamlit shrinks a wider image to exactly that many pixels, so it can look blurry on high-resolution screens. With "stretch" or "content", Streamlit keeps up to 1,460 pixels, so the figure stays sharp in the default centered layout. |
   | use_container_width | bool |  | Whether to override the figure's native width with the width of the parent container. If use_container_width is True (default), Streamlit sets the width of the figure to match the width of the parent container. If use_container_width is False, Streamlit sets the width of the chart to fit its contents according to the plotting library, up to the width of the parent container. |
   | alt | str or None |  | A description of the figure image for screen readers and other assistive technologies. If this is None (default), Streamlit does not provide an accessible name for the figure. An empty string ("") marks the image as decorative. Whitespace- only values are treated as None and logged. Prefer naming the chart's takeaway rather than pasting axis tick labels. |
   | **kwargs | any |  | Arguments to pass to Matplotlib's savefig function. |

> **Note**
>
> Always pass a Matplotlib `Figure` to `st.pyplot`. Calling `st.pyplot()` with no arguments is no longer supported. Passing Matplotlib `savefig` keyword arguments is deprecated; save the figure yourself and display it with [`st.image`](https://docs.streamlit.io/develop/api-reference/media/st.image).

> **Warning**
>
> Matplotlib [doesn't work well with threads](https://matplotlib.org/3.3.2/faq/howto_faq.html#working-with-threads). So if you're using Matplotlib you should wrap your code with locks. This Matplotlib bug is more prominent when you deploy and share your apps because you're more likely to get concurrent users then. The following example uses [`Rlock`](https://docs.python.org/3/library/threading.html#rlock-objects) from the `threading` module.
>
> ```python
> import streamlit as st
> import matplotlib.pyplot as plt
> import numpy as np
> from threading import RLock
>
> _lock = RLock()
>
> x = np.random.normal(1, 1, 100)
> y = np.random.normal(1, 1, 100)
>
> with _lock:
>     fig, ax = plt.subplots()
>     ax.scatter(x, y)
>     st.pyplot(fig)
> ```
