pb graph鼠标移上显示数据_Plotly数据可视化:离线版、微软vscode版的Python的基本作图...
1 介紹:
=====
1.1 Plotly 是一款用來做數據分析和可視化的在線平臺,功能非常強大。
1.2 Plotly是一個非常著名且強大的開源數據可視化框架,它通過構建基于瀏覽器顯示的web形式的可交互圖表來展示信息。
1.3 具有多種語言python、javascript、matlab、R、Jupyter、Excel等的API接口。
1.4 Plotly有在線和離線兩種模式。
1.5 優點:
1.5.1 可開發web版可視化界面。
1.5.2 相比matplotlib、R更加現代化。
1.5.3 支持3D可視化繪圖。
2 說明:
=====
2.1 本次先講解:離線版、微軟vscode版、python版代碼的常見作圖。(網上多是以jupyter notebook為開發工具的在線版或者離線版,隨著微軟代碼編輯器vscode的使用普及,所以本人重點介紹vscode版離線版的python代碼)
2.2 環境:deepin-linux深度操作系統,python3.8,谷歌瀏覽器,微軟vscode編輯器。
2.3 對官方的代碼:進行修改,注釋。
2.4 基本作圖通俗易懂,拿來就可以使用。
2.5 高級的交互式作圖下次講解。
2.6 網址打開奇慢:
https://plot.ly/python/3 安裝:
=====
pip install plotly #sudo pip install plotly #一般是這樣#pip3.8 install plotly #本機安裝#pip install plotly --upgrade #如果已經安裝,升級就這樣4 柱狀圖:
4.1 bar代碼:
import plotly as pyimport plotly.graph_objs as gopyplt = py.offline.plot #離線設置# 2組數據的柱狀圖trace0 = go.Bar(x = ['Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],y = [20,14,25,16,18,22,19,15,12,16,14,17],name = 'Primary Product',marker=dict(color = 'rgb(49,130,189)'))trace1 = go.Bar(x = ['Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],y = [19,14,22,14,16,19,15,14,10,12,12,16],name = 'Secondary Product',marker=dict(color = 'rgb(204,204,204)'))data = [trace0,trace1]layout = go.Layout(title = 'plotly柱狀圖bar')fig = go.Figure(data = data, layout = layout)#文件名,并保存在根目錄下,也可以指定目錄pyplt(fig, filename='plotly的bar.html')4.2 堆疊柱狀圖代碼:
import plotly as pyimport plotly.graph_objs as gopyplt = py.offline.plottrace1 = go.Bar( x=['giraffes', 'orangutans', 'monkeys'], y=[20, 14, 23], name='SF Zoo')trace2 = go.Bar( x=['giraffes', 'orangutans', 'monkeys'], y=[12, 18, 29], name='LA Zoo')data = [trace1, trace2]#堆疊柱狀圖layout = go.Layout(barmode='stack',title = 'plotly的堆疊柱狀圖')fig = go.Figure(data=data, layout=layout)pyplt(fig, filename='plotly的stackbar.html')5 餅狀圖
5.1 plotly的pie.py代碼:
import plotly as pyimport plotly.graph_objs as gopyplt = py.offline.plot #離線設置#定義數值labels = ['產品1','產品2','產品3','產品4','產品5']values = [38.7,15.33,19.9,8.6,17.47]#定義trace或者datatrace = [go.Pie(labels=labels, values=values)]layout = go.Layout(title = 'plotly的pie圖')fig = go.Figure(data = trace, layout = layout)pyplt(fig, filename='plotly的pie.html')5.2 shanpie.py代碼
import plotly as pyimport plotly.graph_objs as gopyplt = py.offline.plot#數據定義labels = ['產品1', '產品2','產品3', '產品4', '產品5']values = [30, 25, 15, 22, 8]colors = ['#FFFF00', '#FF0000', '#E066FF', '#0D0D0D']#trace或者data數據定義trace = [go.Pie(labels = labels, values = values,rotation = 30,opacity = 1,showlegend = False, #圖例顯示否,True就是顯示pull = [0.1,0,0,0,0], #0.1為第一組數據出來pull=產品1=30的那一組hoverinfo = 'label+percent', textinfo = 'percent', # textinfo = 'value',textfont = dict(size = 30, color = 'white'),marker = dict(colors = colors, line = dict(color = '#000000', width = 2)) ) ]fig = go.Figure(data = trace)#注意fig中無layout布局pyplt(trace, filename='plotly的shanpie.html')5.3 plotly的環形圓,circlepie.py代碼:
import plotly as pyimport plotly.graph_objs as gopyplt = py.offline.plotlabels = ['完成','未完成']values = [0.8,0.2]trace = [go.Pie( labels = labels, values = values, hole = 0.7, #空閑大小比值 hoverinfo = "label + percent")]#標題定義layout = go.Layout(title = 'plotly的環形圈圖')fig = go.Figure(data = trace, layout = layout)pyplt(fig, filename='circlepie.html')6 折線圖line
6.1 方法一:
#導出模塊import plotlyimport plotly.graph_objs as go#直接把數據寫入離線模塊里plotly.offline.plot({"data": [ go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1]), go.Scatter(x=[2, 1, 4, 3], y=[3, 1, 4, 2]),],"layout": go.Layout()}, auto_open=True) #自動打開#注意以上并未出現代碼生成的html文件名和保存地址#采用默認法,如下參數介紹,如filename='temp-plot.html''''plot(figure_or_data, show_link=False, link_text='Export to plot.ly', validate=True, output_type='file', include_plotlyjs=True, filename='temp-plot.html', auto_open=True, image=None, image_filename='plot_image', image_width=800, image_height=600, config=None, include_mathjax=False, auto_play=True, animation_opts=None)'''6.2 方法二:
#常規方法import plotly as pyimport plotly.graph_objs as gopyplt = py.offline.plot#數據資料設置trace0 = go.Scatter( x=[1, 2, 3, 4], y=[10, 15, 13, 17])trace1 = go.Scatter( x=[1, 2, 3, 4], y=[16, 5, 11, 9])#如果數據較多較多,以后還可以讀取數據方法,那是高級法data = [trace0, trace1]#布局并定義標題layout = go.Layout(title = '常規方法的折線圖line')#定義畫布,掛在data和layoutfig = go.Figure(data = data, layout = layout)pyplt(fig, filename='line.html')7 散點圖代碼:
#隨機散點圖的常規方法import plotly as pyimport plotly.graph_objs as goimport numpy as nppyplt = py.offline.plot #離線設置#定義數據來源#trace1 = go.Scatter(data = go.Scatter(y = np.random.randn(500),mode = 'markers',marker = dict( size = 16, color = np.random.randn(500), colorscale = 'Viridis', showscale = True ))#data = [trace1] #注意被注釋掉的是規范寫法,但也可以直接data取代trace1layout = go.Layout(title = 'plotly的Scatter的散點圖')fig = go.Figure(data = data, layout = layout)pyplt(fig, filename='plotly的Scatter.html')=====以上為常規基本作圖,高級作圖未完待續=========
我覺得比pyecharts還好用,github非常火的可視化作圖,當然它的功能步僅僅是本文基本作圖,高級作圖下次介紹。
自己整理,分享出來,喜歡的就點贊、收藏和轉發。
總結
以上是生活随笔為你收集整理的pb graph鼠标移上显示数据_Plotly数据可视化:离线版、微软vscode版的Python的基本作图...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么建立微信生态用户增长模型?
- 下一篇: python恶搞图_Python恶搞代码