Plotly Bar Chart

 Coming from an analyst background, I have been making bar charts for years.  So, this example is not very interesting.  However, it shows the simplicity of plotly express and how easily one can create a bar graph.  I didn’t stray far from the path regarding the official plotly example.  It is their example.  However, I wanted to show how easy it is to get going.  The only thing I modified was the graphic size in Google Colab.  Very easy and approachable!

import plotly.express as px

long_df = px.data.medals_long()

fig.update_layout(
    width=800, #set the width of the plot to 800 pixels
    height=500, #set the height of the plot to 500 pixels
)

fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")

fig.show(


fig = px.bar(filtered_df, x="nation", y="count", color="medal", title="Long-Form Input")


fig.show()

The full example is located at: Google Colab

Without the layout update, this interactive graph is only one line of code.  That is insanely easy! 

The next step is to use a lower-level object.  That is the graph object.  It looks surprisingly simple, but wait, it will get complex quick! The example below takes two lists and creates a bar chart.  

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig.update_layout(
    title="Count of Anamals by Type",
    xaxis_title=" ",
    yaxis_title="Count of Anamals",
    height=500,
    width=800
)
fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
fig.show()

I like this example; we can see where the data comes from.  We can create a quick graph if we feed lists to plotly.  It is just that simple! 

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
    go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
])
# Change the bar mode
fig.update_layout(barmode='stack')
fig.show()

The next goal is to create an interactive dashboard with a dropdown. 

Comments

Popular posts from this blog

Drawing Tables with ReportLab: A Comprehensive Example

Blog Topics

fillna