|
377 | 377 | "Names may be added along the axes with the `xlabel` and `ylabel` functions, e.g., `plt.xlabel('this is the x-axis')`. Note that both functions take a string as argument. A title can be added to the figure with the `plt.title` command. Multiple curves can be added to the same figure by giving multiple plotting commands in the same code cell. They are automatically added to the same figure."
|
378 | 378 | ]
|
379 | 379 | },
|
| 380 | + { |
| 381 | + "cell_type": "markdown", |
| 382 | + "metadata": {}, |
| 383 | + "source": [ |
| 384 | + "### New figure and figure size\n", |
| 385 | + "\n", |
| 386 | + "Whenever you give a plotting statement in a code cell, a figure with a default size is automatically created, and all subsequent plotting statements in the code cell are added to the same figure. If you want a different size of the figure, you can create a figure first with the desired figure size using the `plt.figure(figsize=(width, height))` syntax. Any subsequent plotting statement in the code cell is then added to the figure. You can even create a second figure (or third or fourth...)." |
| 387 | + ] |
| 388 | + }, |
| 389 | + { |
| 390 | + "cell_type": "code", |
| 391 | + "execution_count": null, |
| 392 | + "metadata": {}, |
| 393 | + "outputs": [], |
| 394 | + "source": [ |
| 395 | + "plt.figure(figsize=(10, 3))\n", |
| 396 | + "plt.plot([1, 2, 3], [2, 4, 3], linewidth=6)\n", |
| 397 | + "plt.title('very wide figure')\n", |
| 398 | + "plt.figure() # new figure of default size\n", |
| 399 | + "plt.plot([1, 2, 3], [1, 3, 1], 'r')\n", |
| 400 | + "plt.title('second figure');" |
| 401 | + ] |
| 402 | + }, |
380 | 403 | {
|
381 | 404 | "cell_type": "markdown",
|
382 | 405 | "metadata": {},
|
|
399 | 422 | "<a href=\"#ex2answer\">Answer to Exercise 2</a>"
|
400 | 423 | ]
|
401 | 424 | },
|
| 425 | + { |
| 426 | + "cell_type": "markdown", |
| 427 | + "metadata": {}, |
| 428 | + "source": [ |
| 429 | + "### Style\n", |
| 430 | + "\n", |
| 431 | + "As was already mentioned above, good coding style is important. It makes the code easier to read so that it is much easier to find errors and bugs. For example, consider the code below, which recreates the graph we produced earlier (with a wider line), but now there are no additional spaces inserted" |
| 432 | + ] |
| 433 | + }, |
| 434 | + { |
| 435 | + "cell_type": "code", |
| 436 | + "execution_count": null, |
| 437 | + "metadata": {}, |
| 438 | + "outputs": [], |
| 439 | + "source": [ |
| 440 | + "a=1\n", |
| 441 | + "b=1\n", |
| 442 | + "c=-6\n", |
| 443 | + "x=np.linspace(-4,4,100)\n", |
| 444 | + "y=a*x**2+b*x+c#Compute y for all x values\n", |
| 445 | + "plt.plot(x,y,linewidth=3)" |
| 446 | + ] |
| 447 | + }, |
| 448 | + { |
| 449 | + "cell_type": "markdown", |
| 450 | + "metadata": {}, |
| 451 | + "source": [ |
| 452 | + "The code in the previous code cell is difficult to read. Good style includes at least the following:\n", |
| 453 | + "* spaces around every mathematical symbol (`=`, `+`, `-`, `*`, `/`), but not needed around `**`\n", |
| 454 | + "* spaces between arguments of a function\n", |
| 455 | + "* no spaces around an equal sign for a keyword argument (so `linewidth=3` is correct)\n", |
| 456 | + "* one space after every comma\n", |
| 457 | + "* one space after each `#`\n", |
| 458 | + "* two spaces before a `#` when it follows a Python statement\n", |
| 459 | + "* no space between the function name and the list of arguments. So `plt.plot(x, y)` is good style, and `plt.plot (x, y)` is not good style.\n", |
| 460 | + "\n", |
| 461 | + "These rules are (a very small part of) the official Python style guide called PEP8. When these rules are applied, the code is as follows:" |
| 462 | + ] |
| 463 | + }, |
| 464 | + { |
| 465 | + "cell_type": "code", |
| 466 | + "execution_count": null, |
| 467 | + "metadata": {}, |
| 468 | + "outputs": [], |
| 469 | + "source": [ |
| 470 | + "a = 1\n", |
| 471 | + "b = 1\n", |
| 472 | + "c = -6\n", |
| 473 | + "x = np.linspace(-4, 4, 100)\n", |
| 474 | + "y = a * x**2 + b * x + c # Compute y for all x values\n", |
| 475 | + "plt.plot(x, y, linewidth=3)" |
| 476 | + ] |
| 477 | + }, |
| 478 | + { |
| 479 | + "cell_type": "markdown", |
| 480 | + "metadata": {}, |
| 481 | + "source": [ |
| 482 | + "Use correct style in all other exercises and all Notebooks to come. " |
| 483 | + ] |
| 484 | + }, |
| 485 | + { |
| 486 | + "cell_type": "markdown", |
| 487 | + "metadata": {}, |
| 488 | + "source": [ |
| 489 | + "### Exercise 2b. First graph revisited\n", |
| 490 | + "Go back to your Exercise 2 and apply correct style. " |
| 491 | + ] |
| 492 | + }, |
402 | 493 | {
|
403 | 494 | "cell_type": "markdown",
|
404 | 495 | "metadata": {},
|
|
0 commit comments