Skip to content

Instantly share code, notes, and snippets.

@andrewxiechina
Created December 4, 2017 04:45
Show Gist options
  • Select an option

  • Save andrewxiechina/33cbc4211631dfe6fd449f14a0ec9875 to your computer and use it in GitHub Desktop.

Select an option

Save andrewxiechina/33cbc4211631dfe6fd449f14a0ec9875 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"_uuid": "2564c18425c3d77f997a6da54420fd5cfb54e727",
"_cell_guid": "df8572c4-d10b-4323-88d8-b1a52bac47c9"
},
"cell_type": "markdown",
"source": "[-> Learn Data Science by Coding](https://www.kaggle.com/andyxie/learn-data-science-by-coding/)"
},
{
"metadata": {
"_uuid": "2a983708e38e5d9777a86aa0e8c440623df7e62f",
"_cell_guid": "d78073be-04da-474d-af26-2c47042ed890"
},
"cell_type": "markdown",
"source": "# Notes\nIn order to make a plot, first load matplotlib:"
},
{
"metadata": {
"_uuid": "b84e9921a9c2527a2b33f79e775315fc360ac65c",
"_cell_guid": "38d26513-183f-4500-b938-f288495dda48",
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "# RUN ALL THE CODE BEFORE YOU START\nimport numpy as np\nfrom matplotlib.pylab import plt #load plot library\n# indicate the output of plotting function is printed to the notebook\n%matplotlib inline ",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"_uuid": "9038815a4738e43afde4068f096d67b4fc58dc96",
"_cell_guid": "ebeb7ab5-24d3-491f-b551-25949f0a799d"
},
"cell_type": "markdown",
"source": "`plt.plot` will use default setting to make a plot, `plt.show()` will then render the plot:"
},
{
"metadata": {
"scrolled": true,
"_uuid": "ee3021e1a47016135b29b676626483bf472bee04",
"_cell_guid": "71dd52f9-c981-4f16-93f7-ea52105a7085",
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "x = np.linspace(-3, 3, 100)\nplt.plot(x)\nplt.show()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"_uuid": "0d3e968ecb22ac636403c46f11f8997cd2684d4a",
"_cell_guid": "dac9465c-589c-48e0-963e-ca186936e2ce"
},
"cell_type": "markdown",
"source": "To change the style, add a string to the function to indicate color and line style, for example `'r-.'` means red dash-dot line: "
},
{
"metadata": {
"_uuid": "ab4bf5f49fb470552efe77cab80017f1fd6987a5",
"_cell_guid": "0a76e935-7721-4bd0-b2c1-0669aefb26df",
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "plt.plot(x, 'r-.')\nplt.show()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"_uuid": "e5d0ca8041caa2416ab52d2ce9b162bd26bf8273",
"_cell_guid": "c3b3098d-d3b3-4ab7-9608-44715ac40829"
},
"cell_type": "markdown",
"source": "Here are some color and styles to choose from:\n\n**Color Character**\n```\n========== ========\ncharacter color\n========== ========\n'b' blue\n'g' green\n'r' red\n'c' cyan\n'm' magenta\n'y' yellow\n'k' black\n'w' white\n========== ========\n```\n\n**Line Style Character**\n```\n================ ===============================\ncharacter description\n================ ===============================\n``'-'`` solid line style\n``'--'`` dashed line style\n``'-.'`` dash-dot line style\n``':'`` dotted line style\n``'.'`` point marker\n``','`` pixel marker\n``'o'`` circle marker\n``'v'`` triangle_down marker\n``'^'`` triangle_up marker\n``'<'`` triangle_left marker\n``'>'`` triangle_right marker\n``'1'`` tri_down marker\n``'2'`` tri_up marker\n``'3'`` tri_left marker\n``'4'`` tri_right marker\n``'s'`` square marker\n``'p'`` pentagon marker\n``'*'`` star marker\n``'h'`` hexagon1 marker\n``'H'`` hexagon2 marker\n``'+'`` plus marker\n``'x'`` x marker\n``'D'`` diamond marker\n``'d'`` thin_diamond marker\n``'|'`` vline marker\n``'_'`` hline marker\n================ ===============================\n```"
},
{
"metadata": {
"_uuid": "91409f8d5effc82512a82cd3b483d46e45635623",
"_cell_guid": "1455c041-da1d-4079-8ad5-05093868b60a"
},
"cell_type": "markdown",
"source": "# Task 1: Plot Simple Line\nPlot $y = x^2, x \\in [-3, 3]$"
},
{
"metadata": {
"_uuid": "3fd7b04fa902fcf2634064fa6cde923b9d0851cc",
"_cell_guid": "db9e9ea9-e6b1-4ae9-9be8-6f8e6ea166e2",
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "import numpy as np\nfrom matplotlib.pylab import plt\n%matplotlib inline\n\nX = np.linspace(-3, 3, 100) # Create an array of 100 numbers between -3 and 3\nX[:10]\nY = np.power(X, 2) # Calculate Y\nplt.plot(X, Y)\nplt.show()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"_uuid": "f0512accd071c2c97ac7276a4c780593567548b8",
"_cell_guid": "08b1f25c-f557-4fb2-8ded-a1306d7fa8be"
},
"cell_type": "markdown",
"source": "# Task 2: Change Line Style\nChange the line color to black and style to dotted line."
},
{
"metadata": {
"_uuid": "bf284bf1be3a23069a5024d60a9bcd93e55c0ea3",
"_cell_guid": "33841e30-ba94-41c8-8013-3e2d43f013da",
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "plt.plot(X, Y, 'k--')\nplt.show()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"_uuid": "cbea03731c09aad91f25fde5d08e71770cd86cbb",
"_cell_guid": "37f00d6c-ae48-420d-b50d-12f40588b3c9"
},
"cell_type": "markdown",
"source": "# Task 3: Plot Line with Error\nPlot $y = x^2 + \\epsilon, x \\in [-3, 3]$, change the color to red.\n\n**Hints: **\n\n- To introduce error, I generated some random number from **standard deviation of 0.4."
},
{
"metadata": {
"_uuid": "d811d11de65277f4ab0b8faaadcf4ede70f1f517",
"_cell_guid": "97157bea-7d98-46ba-80c2-4e85628108d9",
"trusted": false,
"collapsed": true
},
"cell_type": "code",
"source": "X = np.linspace(-3, 3, 100)\nerror = np.random.normal(0,0.4,100) # Make an array of 100 numbers from normal distribution\n #with mean = 0, std = 0.4\nY = np.power(X, 2) + error\nplt.plot(X, Y, 'r.') # r. means red dots\nplt.show()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"_uuid": "0203884c392a48a7a03ae7c68a5c87226c792eca",
"_cell_guid": "6f993e8c-6952-4e5d-8688-80db9650550f"
},
"cell_type": "markdown",
"source": "[-> Learn Data Science by Coding](https://www.kaggle.com/andyxie/learn-data-science-by-coding/)"
}
],
"metadata": {
"language_info": {
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"mimetype": "text/x-python",
"version": "3.6.3",
"file_extension": ".py"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment