溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Django中使用Bokeh實(shí)現(xiàn)數(shù)據(jù)可視化

發(fā)布時間:2021-03-24 15:59:43 來源:億速云 閱讀:219 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Django中使用Bokeh實(shí)現(xiàn)數(shù)據(jù)可視化,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1. 波形圖

這里繪制一個包含了數(shù)千個數(shù)據(jù)點(diǎn)的信號波形圖,繪制方法和 Matlab 如出一轍。學(xué)習(xí)成本為零。

import pandas as pd
from bokeh.plotting import figure
from bokeh.io import output_file, show

csv_file = 'points.csv'
data = pd.read_csv(csv_file)
TOOLS = 'hover,crosshair,pan,wheel_zoom,box_zoom,reset,save,box_select'
picture = figure(width=1000, height=400, tools=TOOLS)
picture.line(data['order'], data['value'], color='blue', alpha=0.5)
output_file('waveform.html', title='waveform')
show(picture)

points.csv 中包含了 2048 個點(diǎn)。上面這段腳本是直接生成了一個 html 文件,show(picture)語句打開了這個 html 文件。效果如下:

怎么在Django中使用Bokeh實(shí)現(xiàn)數(shù)據(jù)可視化

右側(cè)的工具欄是通過TOOLS = 'hover,crosshair,pan,wheel_zoom,box_zoom,reset,save,box_select'設(shè)置的。包含了常見的一些功能,包括縮放,保存,重置等等。由于簡書的 markdown 不支持直接插入 div 塊和 js 腳本,所以只能截取一個圖放在這里,不能體驗(yàn)到右側(cè)的工具欄的使用感受。

2. 集成到 Django 中

上面的例子是直接生成了一個 html 文件,但在正常的使用中,只應(yīng)該生成對應(yīng)的 div 和 js 就行了。
在 Django 的 view.py 中,定義一個 view。

def waveform(request):
  csv_file = 'your file'
  data = pd.read_csv(csv_file) 
  TOOLS = "hover,crosshair,pan,wheel_zoom,box_zoom,reset,save,box_select"
  picture = figure(width=1200, height=400, tools=TOOLS) 
  picture.line(data['order'], data['value'], color='blue', alpha=0.5)
  script, div = components(picture, CDN)
  return render(request, 'waveform.html', {'script': script, 'div': div})

這樣就把對應(yīng)的 template 的 waveform.html 中:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Experiment with Bokeh</title>
  <link href="{% static 'bokeh-0.12.4.min.css' %}" rel="stylesheet" type="text/css">
  <link href="{% static 'bokeh-widgets-0.12.4.min.css' %}" rel="stylesheet" type="text/css">
  <script src="{% static 'bokeh-0.12.4.min.js' %}"></script>
  <script src="{% static 'bokeh-widgets-0.12.4.min.js' %}"></script>
  {{ script |safe }}
</head>
<body>
{{ div |safe }}
</body>
</html>

這里有一個不太好的地方,把 script 放到了 head 里面。

然而要是放在底部。就不能正確畫出圖了。(求大神解答)

3. 時頻圖

在經(jīng)過短時傅里葉變換輸出的結(jié)果,可以用 image 來顯示時頻分布圖。與 Matlab 畫出來的也是如出一轍。

import numpy as np
import pandas as pd
from bokeh.io import output_file, show
from bokeh.plotting import figure
data = pd.read_csv('tf_stft.csv')
value = np.array(data['value'])
d = np.reshape(value, (338, 124))
d = np.transpose(d)
TOOLS = "hover,crosshair,pan,wheel_zoom,box_zoom,reset,save,box_select"
p = figure(x_range=(0, 62), y_range=(0, 169), tools=TOOLS)
p.image(image=[d], x=0, y=0, dw=62, dh=169, palette="Viridis256")
output_file("image.html", title="image.py example")
show(p)

結(jié)果如下:

怎么在Django中使用Bokeh實(shí)現(xiàn)數(shù)據(jù)可視化

上述就是小編為大家分享的怎么在Django中使用Bokeh實(shí)現(xiàn)數(shù)據(jù)可視化了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI