--世界上只有一种真正的英雄主义,就是看清生活的真相之后依然热爱生活,学习编程成就更好的自己--

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

今天分享如何使用Plotly绘制一个比较复杂的图表--柱状和折线双坐标复合图表,即是:柱状图和折线图共同展示, 柱状图对应一个坐标轴,折线图对应另一个坐标轴。另外,可把相关制图过程做成一个自定义制图函数,方便后续直接调用出图,可以大大节省时间,提高工作效率。下面还以Plotly内置的世界人口数据集合为案例展开讲解:

1.调包并抽取一个国家数据,方便后续制图和演示:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(1)

首先拿德国数据为例,生成一个新的人口数量字段其单位为百万,方便跟寿命数据共同生成柱状图,同时生成一个新的人均GDP字段其单位为千元后续生成折线图。

2.编写一个作图自定义函数,有两个参数,X参数为数据集合,Y参数为图表名字:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(2)

作图自定义函数编写如下:

#['#0d0887', '#46039f', '#7201a8', '#9c179e', '#bd3786', '#d8576b', '#ed7953', '#fb9f3a', '#fdca26', '#f0f921'] def Get_Two_Bar_One_Line(X,Y): fig = go.Figure() # Create figure with secondary y-axis fig = make_subplots(specs=[[{"secondary_y": True}]]) # Add traces fig_bar_one = go.Bar(name="人口数量", x=X["Year"], y=X["pop-Million"],marker_color='#44cef6') fig_bar_two = go.Bar(name="平均寿命", x=X["Year"], y=X["life"], marker_color='#bcbd22') fig_line_one = go.Scatter(name="人均GDP",x=X["Year"], y=X["gdpPercap-Thousand"],line=dict(color='red', width=3, dash='dash')) #define the positions fig.add_trace(fig_bar_one,secondary_y=False) fig.add_trace(fig_bar_two,secondary_y=False) fig.add_trace(fig_line_one,secondary_y=True) # Add figure title fig.update_layout(title_text=Y) # Set x-axis title fig.update_xaxes(title_text="1952年到2007年期间国家整体情况变化") # Set y-axes titles fig.update_yaxes(title_text="<b>primary</b>--人口数量百万和平均寿命", secondary_y=False) fig.update_yaxes(title_text="<b>secondary</b>--人均GDG水平--千元", secondary_y=True) return fig.show()

该作图函数可做调整,比如:图形颜色,折线形式和标题内容等等,大家根据实际情况进行个性化处理。

3.利用该作图函数生成相应图表:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(3)

生成图表左侧Y轴刻度展示了人口寿命和数量,右侧Y轴刻度展示了人均GDP水平情况,同时还可以进行数据交互,直观和生动展示了相关情况。

4.调取南非国家数据集合来试试:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(4)

通过已有的制图函数生成图表如下:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(5)

5.调取泰国国家数据集合来试试:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(6)

对已有的制图函数柱状图颜色参数(平均寿命)调整:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(7)

生成图表如下:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(8)

6.调取日本国家数据集合来试试:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(9)

通过已有的制图函数生成图表如下:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(10)

7.最后调取越南国家数据集合来试试:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(11)

通过已有的制图函数生成图表如下:

python数据分析绘制折线图(文科生自学Python-用Plotly绘制柱状和折线双坐标复合图表)(12)

代码汇总如下:

#import plotly to get the datasets from plotly.subplots import make_subplots import plotly.graph_objs as go import plotly.offline as py import plotly_express as px #Get a dataset of a dedicated country df = px.data.gapminder().query("country == ['Germany']") #'Japan','Germany','Italy','Canada','Spain','Thailand','Argentina' df["life"] = df["lifeExp"].astype(int) df["pop-Million"] = (df["pop"]/1000000).astype(int) #Get pop 100W /人口数量以百万单位来显示 df["gdpPercap-Thousand"] = (df["gdpPercap"]/1000).astype(float) #Get gdpPercap as Tousand /人均GDP以千元单位显示 df["Year"] = df["year"].astype(str) #Convert year as int into string display(df) display(df.dtypes) #['#0d0887', '#46039f', '#7201a8', '#9c179e', '#bd3786', '#d8576b', '#ed7953', '#fb9f3a', '#fdca26', '#f0f921'] def Get_Two_Bar_One_Line(X,Y): fig = go.Figure() # Create figure with secondary y-axis fig = make_subplots(specs=[[{"secondary_y": True}]]) # Add traces fig_bar_one = go.Bar(name="人口数量", x=X["Year"], y=X["pop-Million"],marker_color='#44cef6') fig_bar_two = go.Bar(name="平均寿命", x=X["Year"], y=X["life"], marker_color='#bcbd22') fig_line_one = go.Scatter(name="人均GDP",x=X["Year"], y=X["gdpPercap-Thousand"],line=dict(color='red', width=3, dash='dash')) #define the positions fig.add_trace(fig_bar_one,secondary_y=False) fig.add_trace(fig_bar_two,secondary_y=False) fig.add_trace(fig_line_one,secondary_y=True) # Add figure title fig.update_layout(title_text=Y) # Set x-axis title fig.update_xaxes(title_text="1952年到2007年期间国家整体情况变化") # Set y-axes titles fig.update_yaxes(title_text="<b>primary</b>--人口数量百万和平均寿命", secondary_y=False) fig.update_yaxes(title_text="<b>secondary</b>--人均GDG水平--千元", secondary_y=True) return fig.show() Get_Two_Bar_One_Line(df,"德国") #Get a dataset of a dedicated country df = px.data.gapminder().query("country == ['South Africa']") #'Japan','Germany','Italy','Canada','Spain','Thailand','Argentina' df["life"] = df["lifeExp"].astype(int) df["pop-Million"] = (df["pop"]/1000000).astype(int) #Get pop 100W /人口数量以百万单位来显示 df["gdpPercap-Thousand"] = (df["gdpPercap"]/1000).astype(float) #Get gdpPercap as Tousand /人均GDP以千元单位显示 df["Year"] = df["year"].astype(str) #Convert year as int into string display(df) Get_Two_Bar_One_Line(df,"南非") #Get a dataset of a dedicated country df = px.data.gapminder().query("country == ['Thailand']") #'Japan','Germany','Italy','Canada','Spain','Thailand','Argentina' df["life"] = df["lifeExp"].astype(int) df["pop-Million"] = (df["pop"]/1000000).astype(int) #Get pop 100W /人口数量以百万单位来显示 df["gdpPercap-Thousand"] = (df["gdpPercap"]/1000).astype(float) #Get gdpPercap as Tousand /人均GDP以千元单位显示 df["Year"] = df["year"].astype(str) #Convert year as int into string display(df) #['#0d0887','#46039f', '#7201a8', '#9c179e', '#bd3786', '#d8576b', '#ed7953', '#fb9f3a', '#fdca26', '#f0f921'] def Get_Two_Bar_One_Line(X,Y): fig = go.Figure() # Create figure with secondary y-axis fig = make_subplots(specs=[[{"secondary_y": True}]]) # Add traces fig_bar_one = go.Bar(name="人口数量", x=X["Year"], y=X["pop-Million"],marker_color='#44cef6') fig_bar_two = go.Bar(name="平均寿命", x=X["Year"], y=X["life"], marker_color='#9c179e') fig_line_one = go.Scatter(name="人均GDP",x=X["Year"], y=X["gdpPercap-Thousand"],line=dict(color='red', width=3, dash='dash')) #define the positions fig.add_trace(fig_bar_one,secondary_y=False) fig.add_trace(fig_bar_two,secondary_y=False) fig.add_trace(fig_line_one,secondary_y=True) # Add figure title fig.update_layout(title_text=Y) # Set x-axis title fig.update_xaxes(title_text="1952年到2007年期间国家整体情况变化") # Set y-axes titles fig.update_yaxes(title_text="<b>primary</b>--人口数量百万和平均寿命", secondary_y=False) fig.update_yaxes(title_text="<b>secondary</b>--人均GDG水平--千元", secondary_y=True) return fig.show() Get_Two_Bar_One_Line(df,"泰国") #Get a dataset of a dedicated country df = px.data.gapminder().query("country == ['Japan']") #'Japan','Germany','Italy','Canada','Spain','Thailand','Argentina' df["life"] = df["lifeExp"].astype(int) df["pop-Million"] = (df["pop"]/1000000).astype(int) #Get pop 100W /人口数量以百万单位来显示 df["gdpPercap-Thousand"] = (df["gdpPercap"]/1000).astype(float) #Get gdpPercap as Tousand /人均GDP以千元单位显示 df["Year"] = df["year"].astype(str) #Convert year as int into string display(df) Get_Two_Bar_One_Line(df,"日本") #Get a dataset of a dedicated country df = px.data.gapminder().query("country == ['Vietnam']") #'Japan','Germany','Italy','Canada','Spain','Thailand','Argentina' df["life"] = df["lifeExp"].astype(int) df["pop-Million"] = (df["pop"]/1000000).astype(int) #Get pop 100W /人口数量以百万单位来显示 df["gdpPercap-Thousand"] = (df["gdpPercap"]/1000).astype(float) #Get gdpPercap as Tousand /人均GDP以千元单位显示 df["Year"] = df["year"].astype(str) #Convert year as int into string display(df) Get_Two_Bar_One_Line(df,"越南")

通过上面的内容看到,如何做好作图自定义函数是关键!!!方便后续调用并直接生成相关复杂图表,减少重复代码出现,现实中事先固定好几个模式的作图自定义函数,然后利用处理好的数据集直接生成优雅和统一的可视化报告,效率真的哇塞啊!!!!

END

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

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

,