Lab03 Plotting Distribution - Ipynb
Lab03 Plotting Distribution - Ipynb
[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python
3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":
["***Objectives***\n","\n","Show different ways to present statistical
data."],"metadata":{"id":"qP__Kn0H6HYT"}},{"cell_type":"markdown","source":
["Matplotlib is a comprehensive library for creating static, animated, and
interactive visualizations in Python. Matplotlib makes easy things easy and hard
things possible.\n","\n","1. Create publication quality plots.\n","\n","2. Make
interactive figures that can zoom, pan, update.\n","3. Customize visual style and
layout.\n","4. Export to many file formats.\n","5. Embed in JupyterLab and
Graphical User Interfaces.\n","6. Use a rich array of third-party packages built on
Matplotlib.\n","\n","for more information: https://matplotlib.org/"],"metadata":
{"id":"7Sn_17QMcLdU"}},{"cell_type":"markdown","source":["#**Load important
Libraries**"],"metadata":{"id":"2SCPbfSoGPjY"}},{"cell_type":"code","source":
["import numpy as np\n","import matplotlib.pyplot as plt"],"metadata":
{"id":"YvV_sXxc6cyO"},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":["# Generate data that are normally distributed\n","x
= np.random.randn(50)\n","print(x)"],"metadata":{"id":"qONAy-
TZ6fye"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":
["#**Useful Plots**"],"metadata":{"id":"l7UvyMaLGfSY"}},
{"cell_type":"markdown","source":["##**Scatter plot**"],"metadata":
{"id":"f4uap_L87uhG"}},{"cell_type":"code","source":["#Scatter plot\
n","plt.plot(x,'.')\n","# plt.scatter(x,'.')\n","plt.title('Scatter Plot')\
n","plt.xlabel('X')\n","plt.ylabel('Y')\n","plt.draw()"],"metadata":
{"id":"qFnCfzZC6j6v"},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":["##**Histogram**"],"metadata":{"id":"Bu0W-
ihK7zoK"}},{"cell_type":"code","source":["#Histogram\n","# plt.hist(x)\
n","plt.hist(x, bins=5) # you could use k from: 2^k >= n\n","plt.xlabel('Data
Values')\n","plt.ylabel('Frequency')\n","plt.title('Histogram, default
settings')"],"metadata":{"id":"I4qpuDtQ6-w8"},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":["x = np.random.randn(1000)\n","plt.hist(x)\
n","plt.xlabel('Data Values')\n","plt.ylabel('Frequency')\
n","plt.title('Histogram')"],"metadata":
{"id":"qx85w_YT7AtA"},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":["##**Boxplot**"],"metadata":{"id":"a-
E_5wP273dR"}},{"cell_type":"markdown","source":["TODO: Example with
outliers"],"metadata":{"id":"L3-sTP2d6-az"}},{"cell_type":"code","source":["x =
np.random.randn(50)"],"metadata":
{"id":"6ej3mTS1Vz_0"},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":["#Boxplot\n","# The error bars indicate 1.5* the
inter-quartile-range (IQR), and the box consists of the first, second (middle) and
third quartile\n","plt.boxplot(x, sym='o')\n","plt.title('Boxplot')\
n","plt.ylabel('Values')"],"metadata":
{"id":"avfd0NcF7IKz"},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":["plt.boxplot(x, vert=False, sym='*')\
n","plt.title('Boxplot, horizontal')\n","plt.xlabel('Values')"],"metadata":
{"id":"Ktlvx8_i7TmP"},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":["# random integers between 1 to 20\n","arr =
np.random.randint(1, 20, size=30)\n"," \n","# two outliers taken\n","arr1 =
np.append(arr, [27, 30])\n"," \n","print('Thus the array
becomes{}'.format(arr1))"],"metadata":
{"id":"b0HlUAZxeLWI"},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":["plt.boxplot(arr1)\n","fig = plt.figure(figsize =(10,
7))\n","plt.show()"],"metadata":
{"id":"UkirsHyXefqa"},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":["z= np.array([35, 25, 25, 15, 10, 1000,920,950,900,
25, 36, 45, 50, 55, 54,
24,20,21,22,23,24,200,250,260,300,400,250,265,365,547,154,163,246])\
n","plt.boxplot(z)\n","fig = plt.figure(figsize =(10, 7))\
n","plt.show()"],"metadata":{"id":"2S-5CFNGkQZv"},"execution_count":null,"outputs":
[]},{"cell_type":"markdown","source":["## **Pie Chart**"],"metadata":
{"id":"zSWAt_fQ0cAv"}},{"cell_type":"code","source":["y = np.array([35, 25, 25,
15])\n","\n","plt.pie(y)\n","plt.show()\n"],"metadata":{"id":"J1Ib6-
3y0em8"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["y =
np.array([35, 25, 25, 15])\n","mylabels =
[\"Apples\", \"Bananas\", \"Cherries\", \"Dates\"]\n","\n","plt.pie(y, labels =
mylabels)\n","plt.legend(loc=(1,1))\n","plt.show() "],"metadata":
{"id":"5jEoBMy41EgF"},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":["https://github.com/thomas-haslwanter/
statsintro_python/blob/master/ipynb/4_dataDisplay.ipynb"],"metadata":
{"id":"iy6_T3l6GGwJ"}}]}