這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Django中使用Bokeh實(shí)現(xiàn)數(shù)據(jù)可視化,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)公司是一家成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè),提供網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,按需求定制制作,網(wǎng)站開發(fā)公司,從2013年開始是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開發(fā),后臺程序制作以及后期項(xiàng)目運(yùn)營并提出專業(yè)建議和思路。1. 波形圖
這里繪制一個(gè)包含了數(shù)千個(gè)數(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 個(gè)點(diǎn)。上面這段腳本是直接生成了一個(gè) html 文件,show(picture)語句打開了這個(gè) html 文件。效果如下:
右側(cè)的工具欄是通過TOOLS = 'hover,crosshair,pan,wheel_zoom,box_zoom,reset,save,box_select'設(shè)置的。包含了常見的一些功能,包括縮放,保存,重置等等。由于簡書的 markdown 不支持直接插入 div 塊和 js 腳本,所以只能截取一個(gè)圖放在這里,不能體驗(yàn)到右側(cè)的工具欄的使用感受。
2. 集成到 Django 中
上面的例子是直接生成了一個(gè) html 文件,但在正常的使用中,只應(yīng)該生成對應(yīng)的 div 和 js 就行了。
在 Django 的 view.py 中,定義一個(gè) 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>
這里有一個(gè)不太好的地方,把 script 放到了 head 里面。
然而要是放在底部。就不能正確畫出圖了。(求大神解答)
3. 時(shí)頻圖
在經(jīng)過短時(shí)傅里葉變換輸出的結(jié)果,可以用 image 來顯示時(shí)頻分布圖。與 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ù)可視化了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:怎么在Django中使用Bokeh實(shí)現(xiàn)數(shù)據(jù)可視化-創(chuàng)新互聯(lián)
鏈接地址:http://aaarwkj.com/article38/icgsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、定制網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容