龙空技术网

文科生自学Python-用Plotly绘制树形占比关系图

斜杆奶爸Thomas 139

前言:

此时小伙伴们对“gn算法python”大概比较关注,兄弟们都需要分析一些“gn算法python”的相关文章。那么小编也在网摘上收集了一些对于“gn算法python””的相关文章,希望你们能喜欢,咱们一起来了解一下吧!

--知中有行,行中有知;以知为行,知行合一,学习编程成就更好的自己--

Python语言简洁生动,特别适合文科生学习入门IT世界,用几十行代码就能够做一个完整的爬虫脚本,开发效率杠杠的!短时间内即可解决工作和学习中碰到的各种棘手问题。(本人外语专业毕业,机缘巧合爱上编程,自学道路曲曲折折,痛并快乐!)在这里总结一下自学Python遇到的难点和重点,分享码过的代码和要点总结,希望能够给初学者一点启示和鼓励,同时愿意结交更多大神交流有助提升自己的水平。

今天分享如何使用Plotly绘制另一种非常有趣的图表--树形占比关系图表,跟上次介绍的环形占比关系图有异曲同工之妙,同样只有几行代码就能够高效出图且专业美观,接下来还以Plotly内置的世界人口数据集为案例进行演示:

1.调用2002年的世界人口数据作为总样本,抽取几大洲数据集合,方便后续制图和演示,同时添加新字段“世界-world”:

下面利用Treemap函数做一个自定义作图函数,方便随时调用出图:

2.编写自定义作图函数(人口和寿命)共有三个参数,X为数据集本身,Y参数为取数值部分,Z参数为图表名字:

# Get_Treemap_Pop_Life(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='lifeExp',color_continuous_scale='RdBu')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()

该作图函数也能展示寿命情况,以颜色渐变的形式呈现,下面以亚洲数据为例来看看结果:

感觉比例关系特征更加凸显了!!!有的小伙伴可能更钟爱这种风格。

3.使用作图函数(人口和寿命)来套用非洲数据集,并调整一下颜色风格:

def Get_Treemap_Pop_Life(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='lifeExp',color_continuous_scale='Spectral')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()

生成结果如下:

艺术和数据的优雅结合,有木有!!

4.生成作图函数(人口和人均GDP)来套用欧洲数据集:

def Get_Treemap_Pop_Gdp(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='gdpPercap',color_continuous_scale='PiYG')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()

把人均GDP维度以颜色渐变形式来同时观察,生成结果如下:

5.使用作图函数(人口和人均GDP)来套用美洲数据集,并调整颜色风格:

def Get_Treemap_Pop_Gdp(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='gdpPercap',color_continuous_scale='Picnic')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()

生成结果如下:

6.使用作图函数(人口和人均GDP)来套用全球数据集,并调整颜色风格:

def Get_Treemap_Pop_Gdp(x,y,z):    fig = px.treemap(x, path=['range','continent','country'],values=y,color='gdpPercap',color_continuous_scale='GnBu')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()

生成结果如下:

代码汇总如下:

import matplotlib.pyplot as pltimport plotly.express as pxdf = px.data.gapminder().query("year==2002")df["range"]="world"display(df.head())df_Europe = df[df["continent"]=="Europe"]df_Asia = df[df["continent"]=="Asia"]df_Americas = df[df["continent"]=="Americas"]df_Africa = df[df["continent"]=="Africa"]display(df_Europe.head())# Get_Treemap_Pop_Life(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='lifeExp',color_continuous_scale='RdBu')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()    Get_Treemap_Pop_Life(df_Asia,df_Asia["pop"],"亚洲国家人口数量和寿命树形关系图")def Get_Treemap_Pop_Life(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='lifeExp',color_continuous_scale='Spectral')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()    Get_Treemap_Pop_Life(df_Africa,df_Africa["pop"],"非洲国家人口数量和寿命树形关系图")def Get_Treemap_Pop_Gdp(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='gdpPercap',color_continuous_scale='PiYG')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()    Get_Treemap_Pop_Gdp(df_Europe,df_Europe["pop"],"欧洲国家人口数量和人均GDP收入树形关系图")def Get_Treemap_Pop_Gdp(x,y,z):    fig = px.treemap(x, path=['continent','country'],values=y,color='gdpPercap',color_continuous_scale='Picnic')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()Get_Treemap_Pop_Gdp(df_Americas,df_Americas["pop"],"美洲国家人口数量和人均GDP收入树形关系图")def Get_Treemap_Pop_Gdp(x,y,z):    fig = px.treemap(x, path=['range','continent','country'],values=y,color='gdpPercap',color_continuous_scale='GnBu')    fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) #set the size of pic    fig.update_layout(title_text=z)    return fig.show()Get_Treemap_Pop_Gdp(df,df["pop"],"全球国家人口数量和人均GDP收入树形关系图")

END

我为人人,人人为我!!欢迎大家关注,点赞和转发!!!

~~人生不是赛场,梦想不容退场~~不断努力学习蜕变出一个更好的自己,不断分享学习路上的收获和感悟帮助他人成就自己!!!

标签: #gn算法python