-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathm2w2_marimo.py
More file actions
179 lines (145 loc) · 3.9 KB
/
m2w2_marimo.py
File metadata and controls
179 lines (145 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import marimo
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
import pandas as pd
import matplotlib.pyplot as plt
return mo, pd, plt
@app.cell
def _(mo):
mo.md("""
# Week 2 Quiz: Data Visualization
## Starter Code (RUN THIS FIRST IN YOUR OWN ENVIRONMENT)
```python
import pandas as pd
import matplotlib.pyplot as plt
data = {
"genre": [
"Pop","Rock","Hip-Hop","Electronic","Pop","Rock","Hip-Hop","Electronic",
"Pop","Rock","Hip-Hop","Electronic","Pop","Rock","Hip-Hop","Electronic",
"Pop","Rock","Hip-Hop","Electronic"
],
"streams": [
120,90,150,70,130,95,140,80,
110,85,155,75,125,100,145,78,
118,92,160,82
],
"likes": [
30,20,50,15,35,22,48,18,
28,19,55,16,32,25,47,17,
29,21,60,19
],
"shares": [
10,8,20,5,12,9,18,6,
9,7,22,5,11,10,19,6,
10,8,25,7
],
"year": [
2020,2020,2020,2020,2021,2021,2021,2021,
2022,2022,2022,2022,2023,2023,2023,2023,
2024,2024,2024,2024
]
}
df = pd.DataFrame(data)
```
""")
@app.cell
def _(pd):
data = {
"genre": [
"Pop","Rock","Hip-Hop","Electronic","Pop","Rock","Hip-Hop","Electronic",
"Pop","Rock","Hip-Hop","Electronic","Pop","Rock","Hip-Hop","Electronic",
"Pop","Rock","Hip-Hop","Electronic"
],
"streams": [
120,90,150,70,130,95,140,80,
110,85,155,75,125,100,145,78,
118,92,160,82
],
"likes": [
30,20,50,15,35,22,48,18,
28,19,55,16,32,25,47,17,
29,21,60,19
],
"shares": [
10,8,20,5,12,9,18,6,
9,7,22,5,11,10,19,6,
10,8,25,7
],
"year": [
2020,2020,2020,2020,2021,2021,2021,2021,
2022,2022,2022,2022,2023,2023,2023,2023,
2024,2024,2024,2024
]
}
df = pd.DataFrame(data)
return df
@app.cell
def _(mo):
mo.md("""## 1. Create a Bar Plot
- x-axis: genre
- y-axis: streams
- title: Streams by Genre
""")
@app.cell
def _(mo):
bar_show = mo.ui.checkbox(label="Show expected output", value=False)
bar_show
return bar_show
@app.cell
def _(df, mo, plt, bar_show):
bar_output = None
if bar_show.value:
_fig_bar, _ax_bar = plt.subplots()
df.groupby("genre")["streams"].mean().plot(kind="bar", ax=_ax_bar)
_ax_bar.set_xlabel("genre")
_ax_bar.set_ylabel("streams")
_ax_bar.set_title("Streams by Genre")
bar_output = mo.vstack([mo.md("Expected output"), _fig_bar])
bar_output
@app.cell
def _(mo):
mo.md("""## 2. Create a Scatter Plot
- x-axis: likes
- y-axis: streams
- title: Streams vs Likes
""")
@app.cell
def _(mo):
scatter_show = mo.ui.checkbox(label="Show expected output", value=False)
scatter_show
return scatter_show
@app.cell
def _(df, mo, plt, scatter_show):
scatter_output = None
if scatter_show.value:
_fig_scatter, _ax_scatter = plt.subplots()
_ax_scatter.scatter(df["likes"], df["streams"])
_ax_scatter.set_xlabel("likes")
_ax_scatter.set_ylabel("streams")
_ax_scatter.set_title("Streams vs Likes")
scatter_output = mo.vstack([mo.md("Expected output"), _fig_scatter])
scatter_output
@app.cell
def _(mo):
mo.md("""## 3. Create a Histogram
- Use streams column
- title: Streams Distribution
""")
@app.cell
def _(mo):
hist_show = mo.ui.checkbox(label="Show expected output", value=False)
hist_show
return hist_show
@app.cell
def _(df, mo, plt, hist_show):
hist_output = None
if hist_show.value:
_fig_hist, _ax_hist = plt.subplots()
_ax_hist.hist(df["streams"])
_ax_hist.set_title("Streams Distribution")
hist_output = mo.vstack([mo.md("Expected output"), _fig_hist])
hist_output
if __name__ == "__main__":
app.run()