今天小编和大家分享一下“组合图”的绘制,在我们的日常生活工作当中,通常都会遇到需要去绘制“组合图”,例如折线图和直方图的组合,那么如何将“组合图”绘制的高颜值一点、通俗易懂一点呢?
01
准备工作
首先导入需要用到的模块
代码语言:javascript复制import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objects as go
%matplotlib inline
创建需要用到的数据集,数据集中的数据都是随意捏造的,包含了“苹果”公司近几年的营收和利润,
代码语言:javascript复制Apple_Financials ={'Year': [2009, 2010,2011,2012,2013,2014,2015,2016,2017,2018,2019],
'Revenue($M)': [24515, 34216,48080,61095,74456,88985,107010,135983,177860,232888,280534],
'Profit($M)':[5520,7647,10794,15128,20277,26230,35359,47720,65950,93735,114980]}
df = pd.DataFrame(data=Apple_Financials)
02
可视化部分
我们先来简单地画一个折线图和直方图的组合
代码语言:javascript复制fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df['Year'],
y=df['Revenue($M)'],
name="Revenue"
))
fig.add_trace(
go.Bar(
x=df['Year'],
y=df['Profit($M)'],
name="Profit"
))
fig.show()
可以看到上面的图画的十分的简单、粗糙,X轴上面的标记都没有完全显现出来,字体标记上面的字比较小,看不清,直方图上面都没有标识,因为我们无法直接看到直方图上的值,所以我们下一步便来优化一下上面的代码
代码语言:javascript复制fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df['Year'],y=df['Revenue($M)'],name="Revenue",
mode="lines markers", marker={"size":9},
line = dict(color="firebrick", width=3)
))
fig.add_trace(
go.Bar(x=df['Year'],y=df["Profit($M)"],
name="Profit($M)",
text = df["Profit($M)"],
textposition="outside", textfont=dict(size=13,color='#1f77b4'),
marker_color='rgb(158,202,225)', marker_line_color='rgb(17, 69, 126)',
marker_line_width=2,
opacity=0.7
))
# strip down the rest of the plot
fig.update_layout(
showlegend=True,
plot_bgcolor='rgb(240,240,240)',
margin=dict(t=50,l=10,b=10,r=10),
title_text='2009–2019 Financial Report',
title_font_family='Times New Roman',
legend_title_text='Financials',
title_font_size = 25,
title_font_color='darkblue',
title_x=0.5,
xaxis=dict(tickfont_size=14,tickangle = 270,showgrid = True,zeroline = True,showline = True,showticklabels = True,dtick=1),
yaxis=dict(title="USD (millions)",titlefont_size=16,tickfont_size=14),
legend=dict(x=0.01,y=0.99,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
bargap=0.15
)
fig.update_traces(texttemplate='%{text:.2s}')
fig.show()
经过我们的努力之后,上面的可视化展示的整体效果就好了许多,不仅在X轴上展示出了所有的年份,而且直方图上面也有标识,当然可能会有读者觉得这直方图的颜色实在是太丑了,想要再美化一下,这也很好办
代码语言:javascript复制fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df['Year'],y=df['Revenue($M)'],name="Revenue",
mode='lines markers',
marker= dict(size=9,symbol = 'diamond',
color ='RGB(251, 177, 36)',line_width = 2),
line = dict(color='firebrick', width=3)
))
fig.add_trace(
go.Bar(
x=df['Year'],y=df['Profit($M)'],
name="Profit",
text = df['Profit($M)'],
textposition='outside',
textfont=dict(size=13,color='#1f77b4'),
marker_color=["#f3e5f5", '#e1bee7', '#ce93d8', '#ba68c8','#ab47bc',
'#9c27b0','#8e24aa','#7b1fa2','#6a1b9a','#4a148c','#3c0a99'],
marker_line_color='rgb(17, 69, 126)',
marker_line_width=1,
opacity=0.7
))
# strip down the rest of the plot
fig.update_layout(
showlegend=True,
plot_bgcolor="rgb(240,240,240)",
margin=dict(t=50,l=10,b=10,r=10),
title_text='2009-2019 Financial Report',
title_font_family="Times New Roman",
title_font_size = 25,
title_font_color="darkblue",
title_x=0.5,
xaxis=dict(tickfont_size=14,tickangle = 270,showgrid = True,zeroline = True,showline = True,showticklabels = True,dtick=1),
yaxis=dict(title='USD (millions)',titlefont_size=16,tickfont_size=14),
legend=dict(x=0.01,y=0.99,bgcolor='rgba(255, 255, 255, 0)',bordercolor='rgba(255, 255, 255, 0)'),
bargap=0.15
)
fig.update_traces(texttemplate='%{text:.2s}')
fig.show()
最后出来的直方图柱子的颜色就美观了许多,希望对大家有所帮助!