Skip to content

Instantly share code, notes, and snippets.

@GabrielSGoncalves
Created November 19, 2020 21:07
Show Gist options
  • Save GabrielSGoncalves/98b885a1004056871460a00152347d1c to your computer and use it in GitHub Desktop.
Save GabrielSGoncalves/98b885a1004056871460a00152347d1c to your computer and use it in GitHub Desktop.
Code for plotting a line chart and barplot using Plotly.
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objects as
fig = make_subplots(rows=1, cols=2)
colors = px.colors.qualitative.Set1
text_position = [
"top center",
"bottom center",
"top center",
"bottom center",
"top center",
"bottom center",
]
for index, group in enumerate(df_collection_gb.group.unique()):
df_filtered = df_collection_gb[df_collection_gb.group == group]
fig.add_trace(
go.Scatter(
x=df_filtered.year_month,
y=df_filtered.collec_default_status,
text=df_filtered.collec_default_status.round(2),
textposition=text_position[index],
mode="lines+markers+text",
name=group,
marker_color=colors[index],
),
row=1,
col=1,
).add_trace(
go.Bar(
x=df_filtered.year_month,
y=df_filtered.cpf,
text=df_filtered.cpf,
name=group,
marker_color=colors[index],
marker_line_color="black",
),
row=1,
col=2,
).update_layout(
title_text="Title",
title_font_family="Arial",
title_font_size=28,
title_x=0.5,
width=1500,
height=600,
xaxis_showgrid=True,
yaxis_showgrid=True,
yaxis={"title": "% Default", "range": [38, 100]},
yaxis2={"title": "Number of Customers", "range": [0, 14000]},
)
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment