You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Take a look [in this notebook](https://github.com/cal-itp/data-analyses/blob/main/bus_service_increase/competitive-routes.ipynb) to see how you could also weave in HTML and Markdown with `display(HTML())` and `display(Markdown())` to programmatically generate captions. This is the Jupyter notebook equivalent of creating RMarkdown docs the way [Urban Institute creates their fact sheets](https://urban-institute.medium.com/iterated-fact-sheets-with-r-markdown-d685eb4eafce).
149
+
* Take a look [in this notebook](https://github.com/cal-itp/data-analyses/blob/main/bus_service_increase/competitive-routes.ipynb) to see how you could also weave in HTML and Markdown with `display(HTML())` and `display(Markdown())` to programmatically generate captions. This is the Jupyter notebook equivalent of creating RMarkdown docs the way [Urban Institute creates their fact sheets](https://urban-institute.medium.com/iterated-fact-sheets-with-r-markdown-d685eb4eafce).
150
+
151
+
## Exercise 6
152
+
* Re-using the same function to make multiple charts. If it's the same kind of chart (bar chart), then you can probably set up the function differently to take different arguments. This takes a little practice.
153
+
* This function, `make_chart`, takes Operators_Name as an argument, but I don't see it used anywhere inside the function. I see `"Operators_Name"`, which is a string of that exact phrase, but not a variable (no quotation marks).
154
+
```
155
+
def make_chart(Operators_Name, colorscale):
156
+
chart = (alt.Chart(operators_county)
157
+
.mark_bar()
158
+
.encode(
159
+
x=alt.X("county_name", title="County"),
160
+
y=alt.Y("Operators_Name", title="Number of Operators"),
* Instead, I would set up a `make_chart` function that takes a df and an x-column (district or county). Since you're plotting the count of operators for the chart, the y-column is shared. Look at all the places where `df`, `x_col` is used. Also, note how I specified `chart_title` within the function and used that variable later.
174
+
```
175
+
def make_chart(df: pd.DataFrame, x_col: str):
176
+
# Let's create chart_title as a variable, and we will use it later
177
+
# we want 2 different titles depending on the kind of chart
178
+
179
+
if x_col == "county_name":
180
+
chart_title = "Operators by County"
181
+
elif x_col == "Caltrans_District":
182
+
chart_title = "Operators by District"
183
+
184
+
chart = (alt.Chart(df)
185
+
.mark_bar()
186
+
.encode(
187
+
x=alt.X(x_col, title = f"{x_col}.replace('_', ' ').title()"),
188
+
y=alt.Y("Operators_Name", title="Number of Operators"),
# but the complicated part is that faceting is a more complex chart,
227
+
# so will have to use `apply_chart_config(chart)` for a pared down
228
+
# chart formatting instead of
229
+
# `preset_chart_config(chart)`, which will error. you'll have to add
230
+
# additional things like sizing yourself at the end.
231
+
232
+
chart = (alt.Chart(operators_caltransdistrict)
233
+
.mark_bar()
234
+
.encode(
235
+
x=alt.X("county_name:O", title=""),
236
+
y=alt.Y("Operators_Name:Q", title="Number of Operators"),
237
+
color = alt.Color("county_name:O",
238
+
scale = alt.Scale(
239
+
range = cp.CALITP_CATEGORY_BOLD_COLORS)),
240
+
tooltip = ["county_name", "Operators_Name"]
241
+
).facet(
242
+
column = alt.Column('Caltrans_District', title = "District")
243
+
).properties(title="Operators by District and County")
244
+
.resolve_scale(x="independent")
245
+
.interactive()
246
+
)
247
+
248
+
chart = styleguide.apply_chart_config(chart)
249
+
250
+
display(chart)
251
+
```
252
+
253
+
## Exercise 8
254
+
* Whenever you do spatial join, overlay, etc, you might get a new column called `index_right`, which I like to drop. We tend not to use the index anyway, and it's holding the index from your right df.
* When you dissolve, you don't need to keep `Shape__Length` or `Shape__Area` from ESRI. Those units are usually not discernible (if it's in WGS 84, it's decimal degrees, and we won't use it anyway). Instead, generate your own `length` or `area`, because you would have projected the CRS and you would be clear whether the units are meters, feet, etc.
263
+
* I prefer this line: `overlay_percentage = overlay_dissolve.assign(percent = (overlay_dissolve.geom_length / overlay_dissolve.rail_length)*100)`....which basically gets your percent in one line, and you no longer need the step to create the `half_rail_length` (more roundabout).
264
+
* Since you're just assigning a new column, you can keep doing stuff on `overlay_dissolve`.
265
+
```
266
+
overlay_dissolve = overlay_dissolve.assign(
267
+
percent = blah blah
268
+
)
269
+
270
+
# this column name is the same name as the function...avoid because it's sharing the same name
0 commit comments