龙空技术网

Plotly数据可视化:离线版、微软vscode版的Python的基本作图

易三一世 868

前言:

现时同学们对“matlab画布画图”大体比较关怀,兄弟们都需要学习一些“matlab画布画图”的相关内容。那么小编同时在网上网罗了一些有关“matlab画布画图””的相关内容,希望大家能喜欢,看官们一起来学习一下吧!

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 网址打开奇慢:

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(title="plotly的line图")}, 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非常火的可视化作图,当然它的功能步仅仅是本文基本作图,高级作图下次介绍。

自己整理,分享出来,喜欢的就点赞、收藏和转发。

标签: #matlab画布画图 #matlab新画布 #导出python离线模块