Bokeh 是一個有趣的 Python 資料視覺化資料工具

王林
發布: 2024-09-08 16:30:04
原創
738 人瀏覽過

資料視覺化在解釋大量資訊方面發揮關鍵作用。 Bokeh 等工具已成為建立互動式儀表板和報告的熱門解決方案。每個工具都具有獨特的優勢,具體取決於您專案的複雜性和您首選的程式語言。在本文中,我們將深入研究每個工具,然後專注於 Bokeh,包括實踐範例和雲端中的部署。

所以...

什麼是散景?
Bokeh 是一個互動式視覺化函式庫,針對現代 Web 瀏覽器進行示範。它提供優雅簡潔的圖形,使開發人員能夠建立具有高級互動性的儀表板。 Bokeh 特別適合使用 Python 的資料科學家和開發人員,提供高階介面和對繪圖的精細控制。

如何使用這個工具?

  • 安裝依賴項:

pip 安裝散景
pip 安裝gunicorn

  • 創建情節: 在這種情況下,我在主頁中開發了兩個圖,然後我稱之為「app.py」

Bokeh an interesting data tool in python for data visualization

from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure, curdoc
import numpy as np

# Sample data for line plot
line_data = {
    'x': [1, 2, 3, 4, 5],
    'y1': [6, 7, 2, 4, 7],
    'y2': [1, 4, 8, 6, 9]
}

# Data for scatter plot
N = 4000
x_scatter = np.random.random(size=N) * 100
y_scatter = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = np.array([(r, g, 150) for r, g in zip(50 + 2 * x_scatter, 30 + 2 * y_scatter)], dtype="uint8")

# Create ColumnDataSource for line plot
source = ColumnDataSource(data={'x': line_data['x'], 'y': line_data['y1']})

# Create a figure for line plot
plot_line = figure(title="Interactive Line Plot", x_axis_label='X', y_axis_label='Y')
line1 = plot_line.line('x', 'y', source=source, line_width=3, color='blue', legend_label='y1')
line2 = plot_line.line('x', 'y2', source=source, line_width=3, color='red', legend_label='y2', line_alpha=0.5)

# Create a figure for scatter plot
plot_scatter = figure(title="Scatter Plot", tools="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,examine,help")
plot_scatter.circle(x_scatter, y_scatter, radius=radii,
                    fill_color=colors, fill_alpha=0.6,
                    line_color=None)

# Dropdown widget to select data for line plot
select = Select(title="Y-axis data", value='y1', options=['y1', 'y2'])

# Update function to change data based on selection
def update(attr, old, new):
    selected_y = select.value
    source.data = {'x': line_data['x'], 'y': line_data[selected_y]}
    # Update line colors based on selection
    line1.visible = (selected_y == 'y1')
    line2.visible = (selected_y == 'y2')
    plot_line.title.text = f"Interactive Line Plot - Showing {selected_y}"

select.on_change('value', update)

# Arrange plots and widgets in a layout
layout = column(select, plot_line, plot_scatter)

# Add layout to current document
curdoc().add_root(layout)
`
登入後複製

在 Heroku 中建立您的頁面並執行後續步驟。

  • 建立 Procfile:

Bokeh an interesting data tool in python for data visualization

在此文件中以我的情況為例進行聲明。

網路:散景服務 --port=$PORT --address=0.0.0.0 --allow-websocket-origin=juancioelpapi-325d94c2c6c7.herokuapp.com app.py

  • 創建需求: 在專案中建立requirements.txt並寫入並儲存

Bokeh an interesting data tool in python for data visualization

背景虛化

  • 推送您的專案:

在 git 中推送項目時情況類似,但在這種情況下,最終的主推送是在 heroku

git 初始化
git add .
git commit -m「使用 Gunicorn 部署 Bokeh 應用程式」
git push heroku master

  • 最後...

您可以看到有散景圖的頁面。

Bokeh an interesting data tool in python for data visualization

Bokeh an interesting data tool in python for data visualization

  • 結論

Bokeh 的真正強大之處在於它能夠在 Web 環境中提供互動式儀表板,使其成為即時資料監控和大型資料集的理想選擇。透過使用 Gunicorn 在 Heroku 等雲端服務上部署 Bokeh 應用程序,您可以建立可擴展、可用於生產且易於維護和更新的儀表板。

以上是Bokeh 是一個有趣的 Python 資料視覺化資料工具的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!