点击上方“Python爬虫与数据挖掘”,进行关注
回复“书籍”即可获赠Python从入门到进阶共10本电子书
今
日
鸡
汤
一去紫台连朔漠,独留青冢向黄昏。
高级可视化神器Plotly_Express快速入门
Plotly_Express
是新一代的高级可视化神器,它是plotly.py的高级封装,内置了大量实用、现代的绘图模板。
使用者只需要调用简单的API函数,便可快速地生成漂亮的动态可视化图表;同时其内置了很多的数据集,方便自行调用,快速模拟作图。
![f4d8ab7bf85fee486452403d986e3b73.png](https://img-blog.csdnimg.cn/img_convert/f4d8ab7bf85fee486452403d986e3b73.png)
安装
用pip install plotly_express
命令可以安装plotly_express
pip install plotly_express
内置数据集
先导入相关库,进行查看数据集:
import pandas as pd
import numpy as np
import plotly_express as px # 或 import plotly.express as px
GDP数据
记录的是不同国家历年GDP收入与人均寿命,包含的字段:
国家country
洲continent
年份year
平均寿命lifeExp
人口数量pop
GDPgdpPercap
国家简称iso_alpha
国家编号iso_num
![59f5b0c93b1dfba593a1017f007126ef.png](https://img-blog.csdnimg.cn/img_convert/59f5b0c93b1dfba593a1017f007126ef.png)
餐厅流水数据
餐厅的订单流水数据,包含字段:
总账单费用bill
小费tip
顾客性别sex
顾客是否抽烟smoker
就餐日期day
就餐时间time
就餐人数size
![af1f6917d01124b22d8793a77d913287.png](https://img-blog.csdnimg.cn/img_convert/af1f6917d01124b22d8793a77d913287.png)
鸢尾花数据集
著名的鸢尾花数据集,包含字段:
萼片长sepal_length
萼片宽sepal_width
花瓣长petal_length
花瓣宽petal_width
花的种类species
种类所属编号species_id
![3b2144e9840591057446020287f363b5.png](https://img-blog.csdnimg.cn/img_convert/3b2144e9840591057446020287f363b5.png)
风力数据
一份关于风力等级的数据:
方向direction
强度strength
频率frequency
![62be15352f273d9e0de86ac3f0901b4f.png](https://img-blog.csdnimg.cn/img_convert/62be15352f273d9e0de86ac3f0901b4f.png)
选举投票结果
该数据集记录的是2013年蒙特利尔市长选举
投票结果,包含的主要字段:
区域district
Coderre票数
Bergeron票数
Joly票数
总票数total
胜者winner
结果result
区编号district_id
![375c1bc91812b9cdf19624abc0d20916.png](https://img-blog.csdnimg.cn/img_convert/375c1bc91812b9cdf19624abc0d20916.png)
汽车共享可用性数据
该数据记录的是蒙特利尔一个区域中心附近的汽车共享服务的可用性,包含的字段:
纬度centroid_lat
经度centroid_lon
汽车小时数car_hours
高峰小时peak_hour
![9f7990a515f757e346ccf3a515e6c8c7.png](https://img-blog.csdnimg.cn/img_convert/9f7990a515f757e346ccf3a515e6c8c7.png)
股票数据
内置的一份股票数据,包含字段:
日期date
6个公司名称:GOOG、AAPL、AMZN、FB、NFLX、MSFT
![f87f7ae6ff51a3a33aad89dcec1c67c2.png](https://img-blog.csdnimg.cn/img_convert/f87f7ae6ff51a3a33aad89dcec1c67c2.png)
内置颜色面板
plotly_express
还内置了很多颜色面板,颜色任你选择,下面是各个主题下的部分截图:
卡通片主题
px.colors.carto.swatches()
![ca4a139f84373e2dce8db50aefb2096c.png](https://img-blog.csdnimg.cn/img_convert/ca4a139f84373e2dce8db50aefb2096c.png)
CMOcean系列
px.colors.cmocean.swatches()
![387d44af48c95ce8c55b738b02a4f4e1.png](https://img-blog.csdnimg.cn/img_convert/387d44af48c95ce8c55b738b02a4f4e1.png)
ColorBrewer2系列
px.colors.colorbrewer.swatches()
![cd357514904848bc69783898fce4a673.png](https://img-blog.csdnimg.cn/img_convert/cd357514904848bc69783898fce4a673.png)
周期性色调
适用于具有自然周期结构的连续数据
px.colors.cyclical.swatches()
![16885dd4f9b66519f6132bda7f4267a5.png](https://img-blog.csdnimg.cn/img_convert/16885dd4f9b66519f6132bda7f4267a5.png)
分散色标
适用于具有自然中点的连续数据
px.colors.diverging.swatches()
![56fe8f32b5c04e764dd46d6bfacdbc94.png](https://img-blog.csdnimg.cn/img_convert/56fe8f32b5c04e764dd46d6bfacdbc94.png)
定性色标系列
适用于没有自然顺序的数据
px.colors.qualitative.swatches()
![1dddb087bfee68fbc256792a7eaff110.png](https://img-blog.csdnimg.cn/img_convert/1dddb087bfee68fbc256792a7eaff110.png)
顺序色标系列
渐变的颜色系列,适用于大多数连续数据
px.colors.sequential.swatches()
![c151516a3d5a358d4071217aa965f519.png](https://img-blog.csdnimg.cn/img_convert/c151516a3d5a358d4071217aa965f519.png)
作图
下面介绍使用Plotly_express绘制常见的图形,所有的图形在jupyter notebook中都是动态可视化的,本文中采用截图展示。
柱状图
# 指定选取国家:Switzerland
Switzerland = gapminder[gapminder["country"] == "Switzerland"]
Switzerland # 数据显示如下
![f497d62024e6819feacfb41219b74fa8.png](https://img-blog.csdnimg.cn/img_convert/f497d62024e6819feacfb41219b74fa8.png)
px.bar(Switzerland, # 上面指定的数据
x="year", # 横坐标
y="pop", # 纵坐标
color="pop") # 颜色取值
具体结果如下:
![9623d4aacaa5a64674fda02a2805b8d2.png](https://img-blog.csdnimg.cn/img_convert/9623d4aacaa5a64674fda02a2805b8d2.png)
散点图
先选取绘图需要的数据:
# 写法1
# gapminder_2002 = gapminder.query("year==2002")
# 写法2
gapminder_2002 = gapminder[gapminder["year"] == 2002]
gapminder_2002
![b37c33020934e3182175bf246adef233.png](https://img-blog.csdnimg.cn/img_convert/b37c33020934e3182175bf246adef233.png)
px.scatter(gapminder_2002, # 传入的数据集
x="gdpPercap", # 横坐标是人均GDP
y="lifeExp", # 纵坐标是平均寿命
color="continent" # 颜色取值:根据洲的值来取
)
![73f38c9b83950b8ec575a4fdb22ae63a.png](https://img-blog.csdnimg.cn/img_convert/73f38c9b83950b8ec575a4fdb22ae63a.png)
冒泡散点图
px.scatter(gapminder_2002 # 绘图DataFrame数据集
,x="gdpPercap" # 横坐标
,y="lifeExp" # 纵坐标
,color="continent" # 区分颜色
,size="pop" # 区分圆的大小
,size_max=60 # 散点大小
)
![f804545b27f74b0f4147b8ce0930532b.png](https://img-blog.csdnimg.cn/img_convert/f804545b27f74b0f4147b8ce0930532b.png)
散点矩阵图
px.scatter_matrix(iris, # 传入绘图数据
dimensions=["sepal_width","sepal_length","petal_width","petal_length"], # 维度设置
color="species") # 颜色取值
![20720ba80ec36bb49be44bb61845b3b9.png](https://img-blog.csdnimg.cn/img_convert/20720ba80ec36bb49be44bb61845b3b9.png)
面积图
# area 图
px.area(gapminder, # 绘图的数据集
x="year", # 横轴数据
y="pop", # 纵轴数据
color="continent", # 颜色取值
line_group="country") # 线型分组
![91946acd8514177f9f6aecc6daa06e96.png](https://img-blog.csdnimg.cn/img_convert/91946acd8514177f9f6aecc6daa06e96.png)
股票趋势图
# FB公司股票趋势图
px.line(stock, x='date', y="FB")
![ae11f10e8e0e62bee55255c059717427.png](https://img-blog.csdnimg.cn/img_convert/ae11f10e8e0e62bee55255c059717427.png)
饼图
1、我们使用小费tips数据,查看前5行数据:
![a0347ef40e44b00c2b7bc8c5fd525ea6.png](https://img-blog.csdnimg.cn/img_convert/a0347ef40e44b00c2b7bc8c5fd525ea6.png)
2、根据day分组,统计total_bill字段的和
![262dedb461c12fca76489da92cfe1c22.png](https://img-blog.csdnimg.cn/img_convert/262dedb461c12fca76489da92cfe1c22.png)
3、绘制饼图,自动显示每个day的占比
px.pie(total_bill_byday, # 绘图数据
names="day", # 每个组的名字
values="total_bill" # 组的取值
)
![453e419b5ae63caa576addde4ed14710.png](https://img-blog.csdnimg.cn/img_convert/453e419b5ae63caa576addde4ed14710.png)
旭日图
# 选取2002年数据
gapminder_2002 = gapminder[gapminder["year"] == 2002]
px.sunburst(gapminder_2002, # 绘图数据
path=['continent', 'country'], # 指定路径:从洲到国家
values='pop', # 数据大小:人口数
color='lifeExp', # 颜色
hover_data=['iso_alpha'] # 显示数据
)
![8ba39f216f3cdc44d688fefadfeb9732.png](https://img-blog.csdnimg.cn/img_convert/8ba39f216f3cdc44d688fefadfeb9732.png)
漏斗图
漏斗图形在互联网的电商、用户分群等领域使用的比较广泛,自行模拟一个电商UV-付款转化的数据绘图:
data = dict( # 创建原始数据
number = [1000, 800, 400, 200, 100, 30],
stage = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]
)
# 传入数据和数轴
px.funnel(data,
x="number",
y="stage")
![c6b76a90cc1a12fd70adcdecf2ac6b60.png](https://img-blog.csdnimg.cn/img_convert/c6b76a90cc1a12fd70adcdecf2ac6b60.png)
加入一个颜色参数color,改变每个阶段的颜色:
data = dict( # 创建原始数据
number = [1000, 800, 400, 200, 100, 30],
stage = ["UV", "搜索", "搜藏", "加购", "下单", "付款"]
)
# 传入数据和数轴
px.funnel(data,
x="number",
y="stage",
color="number" # 颜色设置
)
![9ab426a622dbb74fb08466a664a0bec8.png](https://img-blog.csdnimg.cn/img_convert/9ab426a622dbb74fb08466a664a0bec8.png)
直方图
px.histogram(
tips, # 绘图数据
x="sex", # 指定两个数轴
y="tip",
histfunc="avg", # 直方图函数:均值
color="smoker", # 颜色取值
barmode="group", # 柱状图模式
facet_row="time", # 横纵纵轴的字段设置
facet_col="day",
category_orders={"day":["Thur","Fri","Sat","Sun"], # 分类
"time":["Lunch","Dinner"]})
![b133f1d0c68632b299eef51a8a075f44.png](https://img-blog.csdnimg.cn/img_convert/b133f1d0c68632b299eef51a8a075f44.png)
联合分布图
多种图形的组合显示:
px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species",
marginal_x="histogram",
marginal_y="rug")
![2c17ceab131621f4168d2be233d34e10.png](https://img-blog.csdnimg.cn/img_convert/2c17ceab131621f4168d2be233d34e10.png)
箱型图
# notched=True显示连接处的锥形部分
px.box(tips, # 数据集
x="day", # 横轴数据
y="total_bill", # 纵轴数据
color="smoker", # 颜色
notched=True) # 连接处的锥形部分显示出来
![0c98aec4dbd096bf2c02e4cec8861137.png](https://img-blog.csdnimg.cn/img_convert/0c98aec4dbd096bf2c02e4cec8861137.png)
小提琴图
px.scatter(iris, # 传入数据
x="sepal_width", # 设置XY轴
y="sepal_length",
color="species", # 颜色取值
marginal_y="violin", # xy两表图形的设置:小提琴图和箱型图
marginal_x="box",
trendline="ols") # 趋势线设置
![7e926a9069dcaceb32d83c17a1db5932.png](https://img-blog.csdnimg.cn/img_convert/7e926a9069dcaceb32d83c17a1db5932.png)
等高线图
px.density_contour(iris, # 数据集
x="sepal_width", # xy轴
y="sepal_length",
color="species" # 颜色取值
)
![8803ae04ff92288ac9aac0c93599a4db.png](https://img-blog.csdnimg.cn/img_convert/8803ae04ff92288ac9aac0c93599a4db.png)
还可以绘制密度等值线图;
px.density_heatmap(iris, # 传入数据
x="sepal_width", # 两个轴的数据设置
y="sepal_length",
marginal_y="rug", # 边缘图形设置
marginal_x="histogram" # 在密度图的基础上,指定另外两种图形
)
![c33ffcef85344ddb1f85bdb8644efb96.png](https://img-blog.csdnimg.cn/img_convert/c33ffcef85344ddb1f85bdb8644efb96.png)
密度热力图
数据的设置和密度等值图相同,只是选择的图形种类不同:
px.density_heatmap( # 密度热力图
iris,
x="sepal_width",
y="sepal_length",
marginal_y="rug",
marginal_x="histogram"
)
![45af42eb1d22803a47e2d019745f69b3.png](https://img-blog.csdnimg.cn/img_convert/45af42eb1d22803a47e2d019745f69b3.png)
并行分类图
px.parallel_categories(
tips, # 传入数据
color="size", # 颜色取值
color_continuous_scale=px.colors.sequential.Inferno # 颜色变化趋势
)
![060ae5b3eed4bcf4cf5e6de4de332a51.png](https://img-blog.csdnimg.cn/img_convert/060ae5b3eed4bcf4cf5e6de4de332a51.png)
3D散点图
使用的是选举结果数据集:
px.scatter_3d(
election, # 传入数据集
x="Joly", # 指定XYZ坐标轴的数据
y="Coderre",
z="Bergeron",
color="winner", # 颜色取值
size="total", # 大小取值
hover_name="district_id", # 指定颜色种类、大小和显示名称
symbol="result", # 右边的圆形和菱形
color_discrete_map={"Joly":"blue",
"Bergeron":"green",
"Coderre":"red"} # 改变默认颜色
)
![6e9d83020bb7771d7cfd6a529162b8eb.png](https://img-blog.csdnimg.cn/img_convert/6e9d83020bb7771d7cfd6a529162b8eb.png)
3D线型图
px.line_3d(
election, # 绘图数据集
x="Joly", # 3个坐标轴
y="Coderre",
z="Bergeron",
color="winner", # 颜色和线型设置
line_dash="winner"
)
![2947e0013f89450f79085acd657bfc9d.png](https://img-blog.csdnimg.cn/img_convert/2947e0013f89450f79085acd657bfc9d.png)
基于地图的图形
基于choropleth分布的地图:
px.choropleth(
gapminder, # 数据
locations="iso_alpha", # 简称
color="lifeExp", # 颜色取值
hover_name="country", # 悬停数据
animation_frame="year", # 播放按钮设置
color_continuous_scale=px.colors.sequential.Plasma, # 颜色变化取值
projection="natural earth" # 使用的地图设置
)
![839b0cf8437c7ec4a6d43fa1c9de1fe5.png](https://img-blog.csdnimg.cn/img_convert/839b0cf8437c7ec4a6d43fa1c9de1fe5.png)
基于line_geo线型地图:
px.line_geo(
gapminder_2002,
locations="iso_alpha",
color="continent",
projection="orthographic")
![9f3f4a8929ce487240c2fa0edea35185.png](https://img-blog.csdnimg.cn/img_convert/9f3f4a8929ce487240c2fa0edea35185.png)
矩阵式树状结构图
矩阵式树状结构图是一种用于分层数据的复杂、基于区域的数据展示图形:
# 选取2002年数据
gapminder_2002 = gapminder[gapminder["year"] == 2002]
px.treemap(
gapminder_2002, # 数据
path=[px.Constant('world'), 'continent', 'country'], # 绘图路径:world---continent---country
values='pop', # 数据取值
color='pop', # 颜色取值
hover_data=['iso_alpha']) # 显示数据:国家简称
![9b499973ca17190593c84562a62fbf51.png](https://img-blog.csdnimg.cn/img_convert/9b499973ca17190593c84562a62fbf51.png)
散点极坐标图
px.scatter_polar( # 散点极坐标
wind, # 数据集
r="frequency", # 半径
theta="direction", # 角度
color="strength", # 颜色
symbol="strength", # 符号
color_discrete_sequence=px.colors.sequential.Plasma_r) # 颜色
![586371a09f635d819b1e3600f4be1a07.png](https://img-blog.csdnimg.cn/img_convert/586371a09f635d819b1e3600f4be1a07.png)
线性极坐标图
px.line_polar( # 线性极坐标
wind, # 数据集
r="frequency", # 半径
theta="direction", # 角度
color="strength", # 颜色
line_close=True, # 线性闭合
color_discrete_sequence=px.colors.sequential.Plasma_r) # 颜色
![f11b4c8ff9d3287d6b504d220e283fe6.png](https://img-blog.csdnimg.cn/img_convert/f11b4c8ff9d3287d6b504d220e283fe6.png)
柱状极坐标图
px.bar_polar( # 柱状图极坐标图
wind, # 数据集
r="frequency", # 半径
theta="direction", # 角度
color="strength", # 颜色
template="plotly_dark", # 主题
color_discrete_sequence=px.colors.sequential.Plasma_r) # 颜色
![d3a168f64c03678339f07908d36988c5.png](https://img-blog.csdnimg.cn/img_convert/d3a168f64c03678339f07908d36988c5.png)
内置主题
Plotly_Express内置了3种主题可供选择:
plotly
plotly_white
plotly_dark
px.scatter(gapminder_2002, # 传入的数据集
x="gdpPercap", # 横坐标是人均GDP
y="lifeExp", # 纵坐标是平均寿命
color="continent", # 颜色取值:根据洲的值来取
template="plotly" # 分别主题设置为:plotly、plotly_dark
)
![afff1458a693ca7716cce22c5450182c.png](https://img-blog.csdnimg.cn/img_convert/afff1458a693ca7716cce22c5450182c.png)
![51041a16ad20806e7eaaf6c7d60abed3.png](https://img-blog.csdnimg.cn/img_convert/51041a16ad20806e7eaaf6c7d60abed3.png)
![f9ec017e1263fe92ac88a98c1ea81867.png](https://img-blog.csdnimg.cn/img_convert/f9ec017e1263fe92ac88a98c1ea81867.png)
总结
本文详细介绍了一个新的高级可视化库Plotly_Express,从其简介、安装、内置的颜色面板、主题到各种图形的绘制。
这个库最大的特点:代码量非常少,图形种类全,基本上一行代码就能绘制出非常精美的动态可视化图形。以后会介绍更多关于plotly_express的使用文章,特别是plotly
和dash
的结合,更是无比强大。敬请期待!
小伙伴们,快快用实践一下吧!如果在学习过程中,有遇到任何问题,欢迎加我好友,我拉你进Python学习交流群共同探讨学习。
------------------- End -------------------
往期精彩文章推荐:
欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持
想加入Python学习群请在后台回复【入群】
万水千山总是情,点个【在看】行不行
/今日留言主题/
随便说一两句吧~~