How to Create a Beautify Combo Chart in Python Plotly | by Di(Candice) Han | Towards Data Science

Nobody would deny that line and bar combo chart is one of the most widely used combo charts. In Excel, there is a build-in feature of Combo chart. It is also one of the most popular charts to analyze financial data.
In this tutorial, we are going to build a customized combo plot using plotly. The finished combo chart will look like this.
import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport plotly.graph_objects as go%matplotlib inline
Amazon_Financials ={'Year': [2009, 2010,2011,2012,2013,2014,2015,2016,2017,2018,2019],                     'Revenue($M)': [24509, 34204,48077,61093,74452,88988,107006,135987,177866,232887,280522],                   'Profit($M)':[5531,7643,10789,15122,20271,26236,35355,47722,65932,93731,114986]}df = pd.DataFrame(data=Amazon_Financials)
The dataset is very simple. It contains the revenue and profit data of Amazon.com, Inc between 2009 and 2020.
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()
It can be clearly seen that the font of the axis labels on the horizontal and vertical coordinates is a little bit small, It is hard to read. The label of x-axis is not completely shown. And the scale line is not very helpful in pinpointing the real number for each bar. The first step is to enlarge the font of the axis labels and remove the scale line. There is no title for the legend or for the chart. Titles need to be added to provide information to the end users about what is the topic of the chart.
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",        text = df['Profit($M)'],        textposition='outside',        textfont=dict(        size=13,        color='#1f77b4')    ))fig.update_traces(texttemplate='%{text:.2s}')fig.update_layout(legend_title_text='Financials',                 title_text='2009-2019 Financial Report')fig.show()
In this example, the color of the line will become ‘firebrick’. You can use Hex Color Codes or RBG as well. The bars will become light blue color surrounded by lines to enhance the visibility of bars. I also made the bars a little bit transparent. The background color is changed to light grey. X-axis and y-axis are labeled to avoid any confusion.
import plotly.graph_objects as gofig = go.Figure()fig.add_trace(    go.Scatter(        x=df['Year'],        y=df['Revenue($M)'],        name="Revenue",        mode='lines+markers', #         mode = 'lines', if you don't want the markers        marker={'size':9},        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='rgb(158,202,225)',         marker_line_color='rgb(17, 69, 126)',        marker_line_width=2,         opacity=0.7    ))# strip down the rest of the plotfig.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',    legend_title_text='Financials',    xaxis_tickfont_size=14,    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()
There are still some small adjustments required to make the chart more informative and less confusing.
•The x-axis doesn’t have all years
•Fonts of both axes are very small
•Title of the chart should be centered
fig = go.Figure()fig.add_trace( go.Scatter( x=df[‘Year’], y=df[‘Revenue($M)’], name=”Revenue”, mode=’lines+markers’, # mode = ‘lines’ marker={‘size’:9}, 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=’rgb(158,202,225)’,  marker_line_color=’rgb(17, 69, 126)’, marker_line_width=2,  opacity=0.7 ))# strip down the rest of the plotfig.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()
Now this chart looks much better than the preliminary version. We can further beautify the chart by introducing more customization.
We can change the shape, color of markers, and also the color of those bars to make the chart more impressive.
import plotly.graph_objects as gofig = go.Figure()fig.add_trace(    go.Scatter(        x=df['Year'],        y=df['Revenue($M)'],        name="Revenue",        mode='lines+markers', #         mode = 'lines'        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 plotfig.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()
If you want to use one of the variable/feature to define the color of the bars, simple replace
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    ))
with
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=df['Profit($M)'],        marker_line_color='rgb(17, 69, 126)',        marker_line_width=2,         opacity=0.7    ))
Then you will get a combo chart like this,
At the very end, let us take a final look at the Before and After charts.
The beautified chart is much more clear and and can more information to the audience. Labels and title are also informative. Grid lines is helpful in pinpointing the actual numbers of each bars.