當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Streamlit st.plotly_chart用法及代碼示例

顯示交互式 Plotly 圖表。

Plotly 是 Python 的圖表庫。該函數的參數緊跟 Plotly 的 plot() 函數的參數。您可以在 https://plot.ly/python 找到更多關於 Plotly 的信息。

要在 Streamlit 中顯示 Plotly 圖表,請在調用 Plotly 的 py.plotpy.iplot 的任何地方調用 st.plotly_chart

函數簽名

st.plotly_chart(figure_or_data, use_container_width=False, sharing="streamlit", **kwargs)
參數說明

figure_or_data (plotly.graph_objs.Figure, plotly.graph_objs.Data,)

dict/list of plotly.graph_objs.Figure/Data

有關圖形說明的示例,請參見https://plot.ly/python/

use_container_width (bool)

如果為 True,則將圖表寬度設置為列寬。這優先於圖窗的原生 width 值。

sharing ({'streamlit', 'private', 'secret', 'public'})

使用 'streamlit' 使用 plotly 的離線模式(默認)直接在 Streamlit 應用程序中插入繪圖及其所有依賴項。使用任何其他共享模式將圖表發送到需要帳戶的 Plotly 圖表工作室。有關詳細信息,請參閱https://plotly.com/chart-studio/

**kwargs (null)

Plotly 的 plot() 函數接受的任何參數。

示例

下麵的示例直接來自 https://plot.ly/python 的示例:

import streamlit as st
import plotly.figure_factory as ff
import numpy as np

# Add histogram data
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2

# Group data together
hist_data = [x1, x2, x3]

group_labels = ['Group 1', 'Group 2', 'Group 3']

# Create distplot with custom bin_size
fig = ff.create_distplot(
         hist_data, group_labels, bin_size=[.1, .25, .5])

# Plot!
st.plotly_chart(fig, use_container_width=True)

相關用法


注:本文由純淨天空篩選整理自streamlit.io大神的英文原創作品 st.plotly_chart。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。