Chart

  • Introducció

    Streamlit supports several different charting libraries, and our goal is to continually add support for more. Right now, the most basic library is Matplotlib.

    Then there are also interactive charting libraries like Vega Lite (2D charts) and deck.gl (maps and 3D charts).

    And finally there a few chart types that are “native” to Streamlit, like st.line_chart and st.area_chart.

    Comencem

    Crea un projecte amb el nom streamlit-chart:

    uv init streamlit-chart

    API Reference

    Matplotlib

    uv add matplotlib

    st.pyplot

    Matplotlib doesn’t work well 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 from the threading module.

    Crea el fitxer pages/Matplotlib.py:

    import matplotlib.pyplot as plt
    import streamlit as st
    from numpy.random import default_rng as rng
    from threading import RLock
    
    _lock = RLock()
    
    arr = rng(0).normal(1, 1, size=100)
    
    with _lock:
        fig, ax = plt.subplots()
        ax.hist(arr, bins=20)
        st.pyplot(fig)