コンテンツにスキップ

Matplotlibを使う

データ分析の現場では、より高度なカスタマイズが可能なライブラリ(Matplotlib, Plotly, Altairなど)がよく使われます。 Streamlitはこれら全てのライブラリに対応しています。

Pythonでグラフといえば Matplotlib です。 使い方は簡単で、Matplotlibで作った fig(Figureオブジェクト)を st.pyplot(fig) に渡すだけです。

chart_matplotlib.py
import streamlit as st
import matplotlib.pyplot as plt
import 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)