Skip to content

Files

Latest commit

6f51594 · Jul 23, 2020

History

History
98 lines (74 loc) · 2.58 KB

6.2.2-advanced-bar-chart.md

File metadata and controls

98 lines (74 loc) · 2.58 KB

6.2.2 Advanced Bar Chart

1. Horizontal Bar

In chapter 6.1.1 we have shown several regular bar charts. To make a horizontal bar is very intuitive, you just need to add a 'orientation='h'

import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()
fig = px.bar(df, x="total_bill", y="day", orientation='h')
fig.show()

Grouped Horizontal Bar

fig = px.bar(df, x="total_bill", y="time", color='day', orientation='h',
             hover_data=["tip", "size"], height=400, title='Restaurant bills')
fig.show()

Stacked Horizontal Bar

2. Relative Bar

x = ['a', 'b', 'c', 'd']

fig = go.Figure()
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16],name = 'sales Revenue'))
fig.add_trace(go.Bar(x=x, y=[6, -8, -4.5, 8]))
fig.add_trace(go.Bar(x=x, y=[-15, -3, 4.5, -8]))
fig.add_trace(go.Bar(x=x, y=[-1, 3, -3, -4]))

fig.update_layout(barmode='relative', title_text='Relative Bar: Cashflow example')
fig.show()

Relative Bar

3. Customized Bar

fruit = ['apple','orange','banana']
fig = go.Figure()

fig.add_trace(go.Bar(
    y=fruit ,x=[14, 18, 26],
    name='Q1 Sales', orientation='h',
    marker=dict(
        color='greenyellow', line=dict(color='#f3f3f3', width=3)
    )
))
fig.add_trace(go.Bar(
    y=fruit, x=[30, 50, 40],
    name='Q2 Sales', orientation='h',
    marker=dict(
        color='dodgerblue', line=dict(color='#f3f3f3', width=3)
    )
))
fig.update_layout(barmode='stack')
fig.show()

Customerized Bar

# bar with annotations
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 10.e6")
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
fig.show()

Bar with text annotations

colors = ['dodgerblue',] * 5
colors[3] = 'firebrick'

fig = go.Figure(data=[go.Bar(
    x=['Jonas', 'Julian', 'Marius',
       'Adrien', 'Jane'],
    y=[80, 85, 79, 50, 90],
    marker_color=colors # marker color can be a single color value or an iterable
)])
fig.update_layout(title_text= 'Students Score')

Bar with Customized colors