|
504 | 504 | "source": [ |
505 | 505 | "# 1-2 code" |
506 | 506 | ] |
| 507 | + }, |
| 508 | + { |
| 509 | + "cell_type": "markdown", |
| 510 | + "metadata": {}, |
| 511 | + "source": [ |
| 512 | + "# Bonus Content\n", |
| 513 | + "\n", |
| 514 | + "Did you notice that, once we figured out the height, width, and template that we wanted, we ended up reusing those arguments again and again?\n", |
| 515 | + "There is a way to capture those and reuse them.\n", |
| 516 | + "We use a dictionary and [dictionary unpacking](https://docs.python.org/3/tutorial/controlflow.html?highlight=unpack%20dictionary#unpacking-argument-lists).\n", |
| 517 | + "See the example below." |
| 518 | + ] |
| 519 | + }, |
| 520 | + { |
| 521 | + "cell_type": "code", |
| 522 | + "execution_count": null, |
| 523 | + "metadata": {}, |
| 524 | + "outputs": [], |
| 525 | + "source": [ |
| 526 | + "fig_b03 = px.line(\n", |
| 527 | + " vis,\n", |
| 528 | + " x=\"year\",\n", |
| 529 | + " y=\"vis\",\n", |
| 530 | + " title=\"WSJ Coverage by Firm-year\",\n", |
| 531 | + " color=\"ticker\",\n", |
| 532 | + " template=\"plotly_dark\",\n", |
| 533 | + " width=800,\n", |
| 534 | + " height=600,\n", |
| 535 | + ")\n", |
| 536 | + "\n", |
| 537 | + "fig_b03.show()" |
| 538 | + ] |
| 539 | + }, |
| 540 | + { |
| 541 | + "cell_type": "code", |
| 542 | + "execution_count": null, |
| 543 | + "metadata": {}, |
| 544 | + "outputs": [], |
| 545 | + "source": [ |
| 546 | + "GRAPH_FORMAT = {\"template\": \"plotly_dark\", \"width\": 800, \"height\": 600}\n", |
| 547 | + "\n", |
| 548 | + "fig_b03a = px.line(\n", |
| 549 | + " vis,\n", |
| 550 | + " x=\"year\",\n", |
| 551 | + " y=\"vis\",\n", |
| 552 | + " title=\"WSJ Coverage by Firm-year\",\n", |
| 553 | + " color=\"ticker\",\n", |
| 554 | + " **GRAPH_FORMAT,\n", |
| 555 | + ")\n", |
| 556 | + "\n", |
| 557 | + "fig_b03a.show()" |
| 558 | + ] |
| 559 | + }, |
| 560 | + { |
| 561 | + "cell_type": "markdown", |
| 562 | + "metadata": {}, |
| 563 | + "source": [ |
| 564 | + "A key benefit of extracting our favorite formatiing options this way is that we can change all of our graphs by making a single change to a single dictionary." |
| 565 | + ] |
507 | 566 | } |
508 | 567 | ], |
509 | 568 | "metadata": { |
|
526 | 585 | "name": "python", |
527 | 586 | "nbconvert_exporter": "python", |
528 | 587 | "pygments_lexer": "ipython3", |
529 | | - "version": "3.11.3" |
| 588 | + "version": "3.11.4" |
530 | 589 | }, |
531 | 590 | "vscode": { |
532 | 591 | "interpreter": { |
|
0 commit comments