Matplotlibを使う
データ分析の現場では、より高度なカスタマイズが可能なライブラリ(Matplotlib, Plotly, Altairなど)がよく使われます。 Streamlitはこれら全てのライブラリに対応しています。
Matplotlibの表示
Section titled “Matplotlibの表示”Pythonでグラフといえば Matplotlib です。
使い方は簡単で、Matplotlibで作った fig(Figureオブジェクト)を st.pyplot(fig) に渡すだけです。
import streamlit as stimport matplotlib.pyplot as pltimport numpy as np
st.title("Matplotlibの表示")
st.write("Python標準のグラフライブラリ `matplotlib` で作ったグラフも表示できます。")
# 1. データの準備x = np.linspace(0, 10, 100)y = np.sin(x)
# 2. グラフの作成 (fig オブジェクトを作る)fig, ax = plt.subplots()ax.plot(x, y, label='Sine wave', color='green')ax.set_title("Simple Plot")ax.set_xlabel("X axis")ax.set_ylabel("Y axis")ax.legend()ax.grid(True)
# 3. Streamlitで表示# st.pyplot() に fig オブジェクトを渡すだけ!st.pyplot(fig)Streamlit上でMatplotlibを使うときは、plt.show() ではなく st.pyplot(fig) を使うのがポイントです。