gstdl commited on
Commit
929bdc6
1 Parent(s): fc10970

add lineplot

Browse files
Files changed (1) hide show
  1. app.py +51 -3
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
 
4
  from gapminder import gapminder
5
 
6
  #######
@@ -45,6 +46,46 @@ def scatter_plot(df, x, y, hue):
45
  return fig
46
 
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  #######
49
  # Streamlit app code
50
  #######
@@ -65,8 +106,15 @@ with col2:
65
  y = st.selectbox("Select y Axis", metrics, key="boxplot_y")
66
  st.plotly_chart(box_plot(df, x, y))
67
 
68
- ## TODO add lineplot
69
- # st.markdown('## Gapminder Lineplot')
 
 
 
 
 
 
 
70
 
71
 
72
  st.markdown('## Gapminder Scatterplot')
@@ -77,4 +125,4 @@ with col2:
77
  y = st.selectbox("Select y Axis", metrics, key="scatterplot_y")
78
  with col3:
79
  hue = st.selectbox("Select hue", ["country", "continent"], key="scatterplot_hue")
80
- st.plotly_chart(scatter_plot(df, x, y, hue))
 
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
+ import plotly.graph_objects as go
5
  from gapminder import gapminder
6
 
7
  #######
 
46
  return fig
47
 
48
 
49
+ def line_plot(df, y_axis, label, highlighted):
50
+ fig = go.Figure()
51
+ if label=="continent":
52
+ df = df.groupby(["continent", "year"]).agg({
53
+ "lifeExp": "mean",
54
+ "pop": "sum",
55
+ "gdpPercap": "mean",
56
+ }).reset_index()
57
+ data = df[df[label]==highlighted]
58
+ x = data["year"]
59
+ y = data[y_axis]
60
+ fig.add_trace(go.Scatter(x=x, y=y,
61
+ hovertext=[
62
+ f"{label}: {highlighted}<br>year: {year}<br>{y_axis}: {value}"
63
+ for year, value in zip(x,y)
64
+ ],
65
+ hoverinfo="text",
66
+ mode='lines',
67
+ line = dict(color='orange', width=10),
68
+ # name=highlighted
69
+ ))
70
+ for i in df[label].unique():
71
+ if i == highlighted:
72
+ continue
73
+ data = df[df[label]==i]
74
+ x = data["year"]
75
+ y = data[y_axis]
76
+ fig.add_trace(go.Scatter(x=x, y=y,
77
+ hovertext=[
78
+ f"{label}: {i}<br>year: {year}<br>{y_axis}: {value}"
79
+ for year, value in zip(x,y)
80
+ ],
81
+ hoverinfo="text",
82
+ mode='lines',
83
+ line = dict(color='gray', width=1),
84
+ # name=i
85
+ ))
86
+ fig.update_layout(showlegend=False)
87
+ return fig
88
+
89
  #######
90
  # Streamlit app code
91
  #######
 
106
  y = st.selectbox("Select y Axis", metrics, key="boxplot_y")
107
  st.plotly_chart(box_plot(df, x, y))
108
 
109
+ st.markdown('## Gapminder Lineplot')
110
+ col1, col2, col3 = st.columns(3)
111
+ with col1:
112
+ label = st.selectbox("Select label", ["country", "continent"], key="lineplot_label")
113
+ with col2:
114
+ highlighted = st.selectbox("Select value to hightlight", df[label].unique(), key="lineplot_highlighting")
115
+ with col3:
116
+ y = st.selectbox("Select hue", metrics, key="lineplot_y")
117
+ st.plotly_chart(line_plot(df, y, label, highlighted))
118
 
119
 
120
  st.markdown('## Gapminder Scatterplot')
 
125
  y = st.selectbox("Select y Axis", metrics, key="scatterplot_y")
126
  with col3:
127
  hue = st.selectbox("Select hue", ["country", "continent"], key="scatterplot_hue")
128
+ st.plotly_chart(scatter_plot(df, x, y, hue))