px :: Plotly express

Published by onesixx on

https://plotly.com/python-api-reference/

import plotly.express as px

px vs. go

import pandas as pd

df = pd.DataFrame({
  "Fruit":        ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
  "Contestant":   ["Alex", "Alex", "Alex", "Jordan", "Jordan", "Jordan"],
  "Number Eaten": [2, 1, 3, 1, 3, 2],
})

# Plotly Express ----------------------------------------------------
import plotly.express as px

fig = px.bar(df, 
             x="Fruit", y="Number Eaten",
             color="Contestant", barmode="group")
fig.show()


# Graph Objects -----------------------------------------------------
import plotly.graph_objects as go

fig = go.Figure()
for contestant, group in df.groupby("Contestant"):
    fig.add_trace(
        go.Bar(
            x=group["Fruit"], y=group["Number Eaten"], 
            name=contestant,
\t\t\thovertemplate="Contestant=%s
Fruit=%%{x}
Number Eaten=%%{y}"% contestant) \t) fig.update_layout(legend_title_text = "Contestant") fig.update_xaxes(title_text="Fruit") fig.update_yaxes(title_text="Number Eaten") fig.show()
Categories: visualization

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x