{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Exercise 9\n\nExercise 9 with matplotlib.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8, 5), dpi=80)\nplt.subplot(111)\n\nX = np.linspace(-np.pi, np.pi, 256, endpoint=True)\nC = np.cos(X)\nS = np.sin(X)\n\nplt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\", label=\"cosine\")\nplt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\", label=\"sine\")\n\nax = plt.gca()\nax.spines[\"right\"].set_color(\"none\")\nax.spines[\"top\"].set_color(\"none\")\nax.xaxis.set_ticks_position(\"bottom\")\nax.spines[\"bottom\"].set_position((\"data\", 0))\nax.yaxis.set_ticks_position(\"left\")\nax.spines[\"left\"].set_position((\"data\", 0))\n\nplt.xlim(X.min() * 1.1, X.max() * 1.1)\nplt.xticks(\n [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],\n [r\"$-\\pi$\", r\"$-\\pi/2$\", r\"$0$\", r\"$+\\pi/2$\", r\"$+\\pi$\"],\n)\n\nplt.ylim(C.min() * 1.1, C.max() * 1.1)\nplt.yticks([-1, +1], [r\"$-1$\", r\"$+1$\"])\n\nt = 2 * np.pi / 3\nplt.plot([t, t], [0, np.cos(t)], color=\"blue\", linewidth=1.5, linestyle=\"--\")\nplt.scatter(\n [\n t,\n ],\n [\n np.cos(t),\n ],\n 50,\n color=\"blue\",\n)\nplt.annotate(\n r\"$sin(\\frac{2\\pi}{3})=\\frac{\\sqrt{3}}{2}$\",\n xy=(t, np.sin(t)),\n xycoords=\"data\",\n xytext=(+10, +30),\n textcoords=\"offset points\",\n fontsize=16,\n arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=.2\"},\n)\n\nplt.plot([t, t], [0, np.sin(t)], color=\"red\", linewidth=1.5, linestyle=\"--\")\nplt.scatter(\n [\n t,\n ],\n [\n np.sin(t),\n ],\n 50,\n color=\"red\",\n)\nplt.annotate(\n r\"$cos(\\frac{2\\pi}{3})=-\\frac{1}{2}$\",\n xy=(t, np.cos(t)),\n xycoords=\"data\",\n xytext=(-90, -50),\n textcoords=\"offset points\",\n fontsize=16,\n arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=.2\"},\n)\n\nplt.legend(loc=\"upper left\")\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 0 }