Skip to content

Commit 8c1cdfa

Browse files
committed
Make dots appear over lines in regression plots
1 parent f4f2258 commit 8c1cdfa

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

source/regression1.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,15 @@ nearest_neighbors
308308
nn_plot = small_plot + rule
309309
310310
# plot horizontal lines which is perpendicular to x=2000
311+
h_lines = []
311312
for i in range(5):
312313
h_line_df = pd.DataFrame({
313314
"sqft": [nearest_neighbors.iloc[i, 4], 2000],
314315
"price": [nearest_neighbors.iloc[i, 6]] * 2
315316
})
316-
h_line = alt.Chart(h_line_df).mark_line(color="orange").encode(x="sqft", y="price")
317-
nn_plot += h_line
317+
h_lines.append(alt.Chart(h_line_df).mark_line(color="orange").encode(x="sqft", y="price"))
318318
319-
nn_plot
319+
nn_plot = alt.layer(*h_lines, small_plot, rule)
320320
```
321321

322322
```{code-cell} ipython3
@@ -487,13 +487,15 @@ errors_plot = (
487487
small_plot
488488
+ alt.Chart(sacr_full_preds_hid).mark_line().encode(x="sqft", y="predicted")
489489
+ alt.Chart(sacr_new_preds_hid)
490-
.mark_circle()
490+
.mark_circle(opacity=1)
491491
.encode(x="sqft", y="price")
492492
)
493+
v_lines = []
493494
for i in pts["sqft"]:
494495
line_df = sacr_new_preds_melted_df.query("sqft == @i")
495-
errors_plot += alt.Chart(line_df).mark_line(color="red").encode(x="sqft", y="value")
496+
v_lines.append(alt.Chart(line_df).mark_line(color="red").encode(x="sqft", y="value"))
496497
498+
errors_plot = alt.layer(*v_lines, errors_plot)
497499
errors_plot
498500
```
499501

source/regression2.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,13 @@ small_sacramento_pred = small_sacramento_pred[["sqft", "price", "predicted"]].me
305305
id_vars=["sqft"]
306306
)
307307
308-
error_plot = small_plot.copy()
309-
308+
v_lines = []
310309
for i in range(len(small_sacramento)):
311310
sqft_val = small_sacramento.iloc[i]["sqft"]
312311
line_df = small_sacramento_pred.query("sqft == @sqft_val")
313-
error_plot += alt.Chart(line_df).mark_line(color="red").encode(x="sqft", y="value")
312+
v_lines.append(alt.Chart(line_df).mark_line(color="red").encode(x="sqft", y="value"))
314313
314+
error_plot = alt.layer(*v_lines, small_plot).configure_circle(opacity=1)
315315
error_plot
316316
```
317317

0 commit comments

Comments
 (0)