4000字,25张精美交互图表,开启Plotly Express之旅

703bb43b4ae75fd553bbe7367240d4a0.gif

作者 | 周萝卜

来源 | 萝卜大杂烩

Plotly Express 是一个新的高级 Python 可视化库,它是 Plotly.py 的高级封装,为复杂图表提供简单的语法。最主要的是 Plotly 可以与 Pandas 数据类型 DataFrame 完美的结合,对于数据分析、可视化来说实在是太便捷了,而且是完全免费的,非常值得尝试

下面我们使用 Ployly 的几个内置数据集来进行相关图表绘制的演示

abf7c0e2e4b7c086fae1c10c7fa316ac.png

数据集

Plotly 内置的所有数据集都是 DataFrame 格式,也即是与 Pandas 深度契合的体现

不同国家历年GDP收入与人均寿命

包含字段:国家、洲、年份、平均寿命、人口数量、GDP、国家简称、国家编号

gap = px.data.gapminder()
gap2007 = gap.query("year==2007")
gap2007

Output

0475faf3263edfbc7e1d972f29f31668.png

餐馆的订单流水

包含字段:总账单、小费、性别、是否抽烟、星期几、就餐时间、人数

tips = px.data.tips()
tips

Output

10fccbfda9f767ffe83db9463d812008.png

鸢尾花

包含字段:萼片长、萼片宽、花瓣长、花瓣宽、种类、种类编号

iris = px.data.iris()  
iris

Output

1b447d2d63c7f69931bcda0ad0521bff.png

风力数据

包含字段:方向、强度、数值

wind = px.data.wind()  
wind

Output

ba6bed4ef7075c0cabbafa9370f27d2d.png

2013年蒙特利尔市长选举投票结果

包括字段:区域、Coderre票数、Bergeron票数、Joly票数、总票数、胜者、结果(占比分类)

election = px.data.election() 
election

Output

35a91ee328da1ffcf7b8e94a30b62435.png

蒙特利尔一个区域中心附近的汽车共享服务的可用性

包括字段:纬度、经度、汽车小时数、高峰小时

carshare = px.data.carshare()
carshare

Output

a12345ee2cd926227765955e43bfc458.png

0f80d8ad22671d7569f1adcedbf7ef9c.png

内置调色板

Plotly 还拥有众多色彩高级的调色板,使得我们在绘制图表的时候不再为颜色搭配而烦恼

卡通片的色彩和序列

px.colors.carto.swatches()

Output

6f03c57f71146923d989832dfe262ebc.png

CMOcean项目的色阶

px.colors.cmocean.swatches()

Output

4070033ed214a30b8ac12939c8554851.png

还有其他很多调色板供选择,就不一一展示了,下面只给出代码,具体颜色样式可以自行运行代码查看

ColorBrewer2项目的色阶

px.colors.colorbrewer

周期性色标,适用于具有自然周期结构的连续数据

px.colors.cyclical

分散色标,适用于具有自然终点的连续数据

px.colors.diverging

定性色标,适用于没有自然顺序的数据

px.colors.qualitative

顺序色标,适用于大多数连续数据

px.colors.sequential

911e720741452e8483e59f73aff2c0db.png

Plotly Express 基本绘图

散点图

Plotly 绘制散点图非常容易,一行代码就可以完成

px.scatter(gap2007, x="gdpPercap", y="lifeExp")

Output

7506dcb732eaad0855f9442a9a4b2402.png

还可以通过参数 color 来区分不同的数据类别

px.scatter(gap2007, x="gdpPercap", y="lifeExp", color="continent")

Output

f771f551364cee11957b2f553cb4abd7.png

这里每个点都代表一个国家,不同颜色则代表不同的大洲

可以使用参数 size 来体现数据的大小情况

px.scatter(gap2007, x="gdpPercap", y="lifeExp", color="continent", size="pop", size_max=60)

Output

8803fe70b675acebc40ef67c598bea25.png

还可以通过参数 hover_name 来指定当鼠标悬浮的时候,展示的信息

73cd06924ad51df0147cc47e8882cc1f.gif

还可以根据数据集中不同的数据类型进行图表的拆分

px.scatter(gap2007, x="gdpPercap", y="lifeExp", color="continent", size="pop", 
           size_max=60, hover_name="country", facet_col="continent", log_x=True)

Output

69c25fc2656c668c34cb31bc841efbde.png

我们当然还可以查看不同年份的数据,生成自动切换的动态图表

px.scatter(gap, x="gdpPercap", y="lifeExp", color="continent", size="pop", 
           size_max=60, hover_name="country", animation_frame="year", animation_group="country", log_x=True,
          range_x=[100, 100000], range_y=[25, 90], labels=dict(pop="Population", gdpPercap="GDP per Capa", lifeExp="Life Expectancy"))

Output

6960a2d24e299ad1d850b30b7d49c333.gif

地理信息图

Plotly 绘制动态的地理信息图表也是非常方便,通过这种地图的形式,我们也可以清楚的看到数据集中缺少前苏联的相关数据

px.choropleth(gap, locations="iso_alpha", color="lifeExp", hover_name="country", animation_frame="year", 
              color_continuous_scale=px.colors.sequential.Plasma, projection="natural earth")

Output

1ca2ec06ee43dd0e92323ebddedfeadc.gif

矩阵散点图

px.scatter_matrix(iris, dimensions=['sepal_width', 'sepal_length', 'petal_width', 'petal_length'], color='species', symbol='species')

Output

dfe72d82d403ca433811a38374db312f.png

平行坐标图

px.parallel_coordinates(tips, color='size', color_continuous_scale=px.colors.sequential.Inferno)

Output

63aba4aaf58668a43e7abe4bdc9b6ea0.png

三元散点图

px.scatter_ternary(election, a="Joly", b="Coderre", c="Bergeron", color="winner", size="total", hover_name="district",
                   size_max=15, color_discrete_map = {"Joly": "blue", 
                   "Bergeron": "green", "Coderre":"red"} )

Output

71e261ec621d9357e0ce0d9cd5e73f72.png

极坐标线条图

px.line_polar(wind, r="frequency", theta="direction", color="strength", 
            line_close=True,color_discrete_sequence=px.colors.sequential.Plotly3[-2::-1])

Output

b931a71d1f8756287716a71e9b9f4d4b.png

小提琴图

px.violin(tips, y="tip", x="sex", color="smoker", facet_col="day", facet_row="time",box=True, points="all", 
          category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]},
          hover_data=tips.columns)

Output

aada0dee0ba0f99e4bcdd138353af7fa.png

极坐标条形图

px.bar_polar(wind, r="frequency", theta="direction", color="strength",
            color_discrete_sequence= px.colors.sequential.Plotly3[-2::-1])

Output

b819f406f68b738d5758d0d0e459f431.png

并行类别图

px.parallel_categories(tips, color="size", color_continuous_scale=px.
            colors.sequential.Inferno)

Output

971a667742c133e68805c1fb38aa1a1c.png

直方图

px.histogram(tips, x="total_bill", color="smoker",facet_row="day", facet_col="time")

Output

db1113e462495a296ae3c3116879fe65.png

三维散点图

px.scatter_3d(election, x="Joly", y="Coderre", z="Bergeron", color="winner", 
              size="total", hover_name="district",symbol="result", 
              color_discrete_map = {"Joly": "blue", "Bergeron": "green", 
              "Coderre":"red"})

Output

91fb0891a87ee0ace30cdd45c43b887b.png

密度等值线图

px.density_contour(iris, x="sepal_width", y="sepal_length", color="species")

Output

b1ddb1defd26cd3881f8347b7b76d65b.png

箱形图

px.box(tips, x="sex", y="tip", color="smoker", notched=True)

Output

5d9e1d0366b5a0d6ae19474a15f2e93e.png

地理坐标线条图

px.line_geo(gap.query("year==2007"), locations="iso_alpha", 
            color="continent", projection="orthographic")

Output

5194292bd3dba5189dbc5095296f4cb3.png

条线图

px.line(gap, x="year", y="lifeExp", color="continent", 
        line_group="country", hover_name="country",
        line_shape="spline", render_mode="svg")

Output

cf3b55c50f5415881fd35a03babb62d1.png

面积图

px.area(gap, x="year", y="pop", color="continent", 
        line_group="country")

Output

3bb204f02a6125a0bbc0362490827627.png

热力图

px.density_heatmap(iris, x="sepal_width", y="sepal_length", 
                   marginal_x="rug", marginal_y="histogram")

Output

aa04cd03a5602b1a549b9cf2921effaf.png

条形图

px.bar(tips, x="sex", y="total_bill", color="smoker", barmode="group")

Output

be9f742a9d5473380883b880f376231c.png

总体来说,Plotly/Plotly Express 还是非常强大绘图工具,值得我们细细研究~

e569b784643248974553b673754140ec.gif

技术

全面解析Kmeans聚类算法

资讯

商汤科技上市,开启AI新篇章

技术

2021年有用的数据清洗python库

资讯

这个AI模型火上GitHub热榜

7745d6ce16e541557cc0e144e24fa208.png

分享

9d42e6cdb50ac88e65bbebd520bdff7d.png

点收藏

290c380b5cdf9fc5dc4821953a396bdd.png

点点赞

ee1b53f050a33e3e2893abb1ddec4534.png

点在看

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值