import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from plotly.subplots import make_subplots
data = pd.read_csv(path_baby)
fig = make_subplots(
rows=1,
cols=4,
specs=[
[
{"type": "Histogram"},
{"type": "Bar"},
{"type": "Histogram"},
{"type": "Histogram"}
]
],
subplot_titles=[
'Age of Mother',
'Father Race',
'Total Pregnancies',
'Birth Weight (kg)'
],
horizontal_spacing=0.1
)
fig.add_trace(
go.Histogram(
x=data.MAGE,
hovertemplate="Age: %{x}<br>Count: %{y}",
showlegend=False
),
row=1,
col=1
)
fig.update_yaxes(
row=1,
col=1,
title='Count'
)
mean_age = data.MAGE.mean()
fig.add_trace(
go.Scatter(
x=[mean_age] * 2,
y=[0, 6000],
mode='lines',
line = dict(
width=3,
color='red',
dash='dash'
),
hovertext=f'Mean: {mean_age}',
showlegend=False
),
row=1,
col=1
)
racedad = data.RACEDAD.value_counts()
fig.add_trace(
go.Bar(
x=racedad.index,
y=racedad.values,
hovertemplate="Race: %{x}<br>Proportion: %{y}",
showlegend=False
),
row=1,
col=2
)
fig.update_yaxes(
title='Count (log scaled)',
type='log',
row=1,
col=2
)
fig.add_trace(
go.Histogram(
x=data.TOTALP,
hovertemplate="Age: %{x}<br>Count: %{y}",
showlegend=False
),
row=1,
col=3
)
mean_tot = data.TOTALP.mean()
fig.add_trace(
go.Scatter(
x=[mean_tot] * 2,
y=[0, 35000],
mode='lines',
line = dict(
width=3,
color='red',
dash='dash'
),
hovertext=f'Mean: {mean_tot}',
showlegend=False
),
row=1,
col=3
)
fig.update_yaxes(
title='Count',
row=1,
col=3
)
RATE = 0.453592
fig.add_trace(
go.Histogram(
x=data.BWEIGHT * RATE,
hovertemplate="Weight: %{x}<br>Count: %{y}",
showlegend=False
),
row=1,
col=4
)
mean_w = data.BWEIGHT.mean() * RATE
fig.add_trace(
go.Scatter(
x=[mean_w] * 2,
y=[0, 2500],
mode='lines',
line = dict(
width=3,
color='red',
dash='dash'
),
showlegend=False
),
row=1,
col=4
)
fig.update_yaxes(
title='Count',
row=1,
col=4
)
fig.update_layout(
width=950,
height=200,
title="Some Variables from <a href='https://www.kaggle.com/datasets/ashiskb/baby-birthweight-dataset?select=baby-weights-dataset.csv'>Baby Weight Dataset</a> " + str(data.shape),
margin=dict(
t=50,
l=10,
r=10,
b=10
)
)