高级可视化神器Plotly玩转气泡图 |
您所在的位置:网站首页 › 气泡图可视化基本功能 › 高级可视化神器Plotly玩转气泡图 |
可视化神器Plotly玩转气泡图
本文是可视化神器Plotly绘图的第6篇:将会重点讲解如何通过Plotly绘制气泡图,英文叫Bubble Charts。首先看一段Plotly官网中对气泡图的简介: A bubble chart is a scatter plot in which a third dimension of the data is shown through the size of markers. 气泡图是也是一种散点图。这种散点图和普通散点图的不同之处在于:它会引入第三方维度,即标记markers的大小来进行展示。在Plotly中散点的大小是通过size参数来设置 Plotly的文章会形成连载系列,前面5篇的Plotly可视化文章分别是: 酷炫!36张图爱上高级可视化神器Plotly_Express Plotly玩转散点图 Plotly玩转饼图 Plotly玩转漏斗图 Plotly玩转柱状图 导入库首先还是需要导入我们绘图需要的几个常用库 import pandas as pd import numpy as np # 两种接口 import plotly_express as px import plotly.graph_objects as go 复制代码绘图的时候还是会基于两种方式来实现: plotly_express:px plotly.graph_objects:go 基于px实现 自带GDP数据集这是Plotly非常厉害的一个功能,能够实现自动播放功能,使用的参数是:animation_frame。我们对整个GDP数据集进行绘图: 看下按钮的位置: 再看看播放的实际效果: 上面的图形中都是没有文本显示的,可以通过设置进行文本显示: fig = px.scatter( stu, x="chinese", y="math", hover_data=["name"], # 列表形式 color="age", size="age", # 散点大小 size_max=60, text="name" # 显示name属性中的数据信息 ) fig.show() 复制代码文本显示位置主要顶部top、中间middle、底部bottom,加上左中右left、center、right的组合: top left top center top right middle left middle center middle right bottom left bottom center bottom right # 改变文本显示位置 fig = px.scatter( stu, x="chinese", y="math", hover_data=["name"], # 列表形式 color="age", size="math", # 散点大小 size_max=60, text="name" ) # 文本显示位置:['top left', 'top center', 'top right', 'middle left','middle center', 'middle right', 'bottom left', 'bottom center', 'bottom right'] fig.update_traces(textposition="bottom center") fig.show() 复制代码看一个底部居中的例子: 我们的颜色都是通过color参数的某个属性来设置,我们也可以自定义颜色: # 改变颜色 fig = px.scatter( stu, x="chinese", y="math", hover_data=["name"], # 列表形式 color_discrete_sequence=px.colors.diverging.Tealrose_r, # 自定义颜色 size="math", # 散点大小 size_max=60, text="name" ) fig.update_traces(textposition="middle center") fig.show() 复制代码什么是悬停信息?? 有时候数据之间的大小差异较大,造成某些气泡过大,图形非常难看,需要对气泡的大小进行尺度缩放,Plotly官方有建议的公式和参数: To scale the bubble size, use the attribute sizeref. We recommend using the following formula to calculate a sizeref value: sizeref = 2. * max(array of size values) / (desired maximum marker size ** 2) 通过一个实际的例子来看看什么叫气泡的大小缩放: 1、某份数据不进行尺度缩放的效果 import plotly.graph_objects as go fig = go.Figure(data=[go.Scatter( x=[5, 6, 7, 8], y=[120, 140, 160, 180], text=['Asize: 40', # 1、悬停信息:可以使用html标签 表示空格 'Bsize: 60', 'Csize: 80', 'Dsize: 100'], mode='markers', marker=dict( color=['blue', # 2、自定义颜色 常见颜色英文 'rgb(255, 144, 14)', # 通过rgb来表示 'rgb(44, 160, 101)', '#AFE918'], # 6位字符串,#开头 size=[40, 60, 80, 100], ) )]) fig.show() 复制代码主要是看看最大和最小气泡的区别: 2、进行大小尺度的缩放 # 标准化过程 import plotly.graph_objects as go size = [20, 40, 60, 80, 100] fig = go.Figure(data=[go.Scatter( x=[1, 2, 3, 4, 5], y=[11, 14, 18, 22, 28], mode='markers', marker=dict( size=size, color=['rgb(93, 164, 214)', 'rgb(255, 144, 14)', 'rgb(44, 160, 101)', 'rgb(255, 65, 154)','rgb(93, 164, 14)'], sizemode='area', sizeref=2.*max(size)/(60.**2), # 尺度缩放 sizemin=4 ) )]) fig.show() 复制代码尤而小屋,一个温馨的小屋。小屋主人,一手代码谋求生存,一手掌勺享受生活,欢迎你的光临😃 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |