2. Vorlesung

This commit is contained in:
2024-10-25 13:28:49 +02:00
parent 9ea256c27e
commit 71b9d91eeb
168 changed files with 1172650 additions and 33 deletions

View File

@@ -0,0 +1,923 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-fae4670da2ba00e2",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"# Graphical representations, Matplotlib, Contour Plots\n",
"\n",
"A Python plotting library called Matplotlib creates publication-quality graphics in a range of physical formats and in cross-platform interactive settings.\n",
"Four graphical user interface toolkits, the Python and IPython shells, the Jupyter notebook, web application servers, and Python scripts can all make use of Matplotlib. The open source documentation can be found under: https://matplotlib.org/stable/index.html#\n",
"\n",
"__Creating Plots__\n",
"\n",
"Figure\n",
"\n",
"|Operator |\tDescription |\n",
"|---------|-------------|\n",
"|fig = plt.figures() | a container that contains all plot elements |\n",
"\n",
"Axes\n",
"\n",
"|Operator |\tDescription |\n",
"|---------|-------------|\n",
"|fig.add_axes() <br> a = fig.add_subplot(222) | Initializes subplot <br> A subplot is an axes on a grid system row-col-num. |\n",
"|fig, b = plt.subplots(nrows=3, nclos=2) | Adds subplot |\n",
"|ax = plt.subplots(2, 2) |\tCreates subplot |\n",
"\n",
"__Plotting__\n",
"\n",
"1D Data\n",
"\n",
"|Operator |\tDescription|\n",
"|---------|------------|\n",
"|lines = plt.plot(x,y) | Plot data connected by lines|\n",
"|plt.scatter(x,y) | Creates a scatterplot, unconnected data points|\n",
"|plt.bar(xvalue, data , width, color...) | simple vertical bar chart|\n",
"|plt.barh(yvalue, data, width, color...) | simple horizontal bar|\n",
"|plt.hist(x, y) | Plots a histogram|\n",
"|plt.boxplot(x,y) | Box and Whisker plot|\n",
"|plt.violinplot(x, y) |\tCreates violin plot|\n",
"|ax.fill(x, y, color='lightblue') <br> ax.fill_between(x,y,color='lightblue') | Fill area under/between plots|\n",
"\n",
"2D Data\n",
"\n",
"|Operator |\tDescription|\n",
"|---------|------------|\n",
"|fig,ax = plt.subplots() <br> im = ax.imshow(img,cmap,vmin...) | Colormap or RGB arrays | \n",
"\n",
"Saving plots\n",
"\n",
"|Operator |\tDescription|\n",
"|---------|------------|\n",
"|plt.savefig('fig.png') | Saves plot/figure to image |\n",
"\n",
"__Customization__\n",
"\n",
"Color\n",
"\n",
"|Operator | Description|\n",
"|---------|------------|\n",
"|plt.plot(x,y,color='lightblue') <br> plt.plot(x,y,alpha = 0.4) | Colors plot to light bluw color |\n",
"|plt.colorbar(mappable,orientation='horizontal') | mappable:the image,contourset to which colorbar applies. |\n",
"\n",
"Markers\n",
"\n",
"|Operator | Description|\n",
"|---------|------------|\n",
"|plt.plot(x,y,marker='*') | adds * for every data point |\n",
"|plt.plot(x,y,marker='.') | adds . for every data point |\n",
"\n",
"Lines\n",
"\n",
"|Operator | Description|\n",
"|---------|------------|\n",
"|plt.plot(x, y, linewidth=2) | Sets line width|\n",
"|plt.plot(x, y, ls='solid') | Sets linestyle, ls can be ommitted, see 2 below|\n",
"|plt.plot(x, y, ls='--') | Sets linestyle, ls can be ommitted, see below|\n",
"|plt.plot(x,y,'--', x\\*\\*2, y\\*\\*2, '-.') | Lines are '--' and '-.' |\n",
"|plt.setp(lines,color='red',linewidth=2) | Sets properties of plot lines|\n",
"\n",
"Text\n",
"\n",
"|Operator | Description|\n",
"|---------|------------|\n",
"|plt.text(1,1,'Text',style='italic') | Places text at coordinates (1,1)|\n",
"|ax.annotate('point A',xy=(10,10)) | Annotate the point with coordinates xy|\n",
"|pt.title(r'\\$delta\\_i=20\\$',fontsize=10)| Math text|\n",
"\n",
"Limits\n",
"\n",
"|Operators | Description|\n",
"|----------|------------|\n",
"|plt.xlim(0, 7) | Sets x-axis to display 0 - 7|\n",
"|other = array.copy() | Creates deep copy of array|\n",
"|plt.ylim(-0.5, 9) | Sets y-axis to display -0.5 - 9|\n",
"|ax.set(xlim=[0, 7], ylim=[-0.5, 9]) <br> ax.set_xlim(0, 7) | Sets limits|\n",
"|plt.margins(x=1.0, y=1.0) | Set margins: add padding to a plot, values 0 - 1|\n",
"|plt.axis('equal') | Set the aspect ratio of the plot to 1|\n",
"\n",
"Legends/Labels\n",
"\n",
"|Operator | Description|\n",
"|---------|------------|\n",
"|plt.title('Title') | Sets title of plot|\n",
"|plt.xlabel('x-axis') | Sets label next to x-axis|\n",
"|plt.ylabel('y-axis') | Sets label next to y-axis|\n",
"|ax.set(title='axis', ylabel='Y-Axis', xlabel='X-Axis') | Set title and axis labels|\n",
"|ax.legend(loc='best') | No overlapping plot elements|\n",
"\n",
"Ticks\n",
"\n",
"|Operator | Description|\n",
"|---------|------------|\n",
"|plt.xticks(x, labels, rotation='vertical') | Set ticks|\n",
"|ax.xaxis.set(ticks=range(1,5), ticklabels=[3,100,-12,\"foo\"]) | Set x-ticks|\n",
"|ax.tick_params(axis='y', direction='inout', length=10) | Make y-ticks longer and go in and out|"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-a1f4d01637d085cc",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"Load the necessary packages and define the arrays __x__, __y__ and __z__ by running the cell below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import numpy as np\n",
"x = np.arange(0,100)\n",
"y = x*2\n",
"z = x**2"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-c92f9e3b1186aa0a",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"\n",
"## Exercise 1\n",
"\n",
"Follow along with these steps:\n",
"\n",
"- Create a figure object called __fig__ using plt.figure()\n",
"- Use add_axes to add an axis to the figure canvas at [0,0,1,1]. Call this new axis __ax__.\n",
"- Plot (x,y) on that axes and set the labels and titles to match the plot below:\n",
"\n",
"![Plot XY](exercise1.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"fig = plt.figure()\n",
"ax = fig.add_axes([0,0,1,1])\n",
"ax.plot(x,y)\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n",
"ax.set_title('plot xy')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-f487f69f6d693b81",
"locked": true,
"points": 0,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"outputs": [],
"source": [
"### BEGIN HIDDEN TEST\n",
"assert isinstance(fig,plt.Figure), f\"{fig} is not an instance of plt.Figure\"\n",
"assert isinstance(ax,matplotlib.axes._axes.Axes), f\"{ax} is not an axis \"\n",
"assert ax.get_title().lower() == 'plot xy', f\"{ax.get_title()} is not the same as plot xy\"\n",
"assert ax.get_xlabel().lower() == 'x'\n",
"assert ax.get_ylabel().lower() == 'y'\n",
"### END HIDDEN TEST"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-009335e6db4359f4",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"\n",
"## Exercise 2\n",
"\n",
"Create a figure object and put two axes on it, ax1 and ax2. Located at [0,0,1,1] and [0.5,0.5,0.3,0.3] respectively."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"fig = plt.figure()\n",
"\n",
"ax1 = fig.add_axes([0,0,1,1])\n",
"ax2 = fig.add_axes([0.5,0.5,0.3,0.3])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"### BEGIN HIDDEN TEST\n",
"assert isinstance(ax1,matplotlib.axes._axes.Axes), f\"{ax1} is not an axis \"\n",
"assert isinstance(ax2,matplotlib.axes._axes.Axes), f\"{ax2} is not an axis \"\n",
"### END HIDDEN TEST"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-ad87f38d4c194c66",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"Now plot (x,y) on axes __ax1__ and on axes __ax2__ plot (x,z) as shown in \n",
"\n",
"![Alt text](exercise2.png). \n",
"\n",
"And call your figure object to show it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"ax1.plot(x,y)\n",
"ax1.set_title('plot xy')\n",
"ax1.set_xlabel('x')\n",
"ax1.set_ylabel('y')\n",
"\n",
"ax2.plot(x,z)\n",
"ax2.set_title('plot xz')\n",
"ax2.set_xlabel('x')\n",
"ax2.set_ylabel('z')\n",
"\n",
"fig # Show figure object\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-52e7d1a8a36277f7",
"locked": true,
"points": 0,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"outputs": [],
"source": [
"### BEGIN HIDDEN TEST\n",
"assert ax1.get_title().lower() == 'plot xy', f\"{ax.get_title()} is not the same as plot xy\"\n",
"assert ax1.get_xlabel().lower() == 'x'\n",
"assert ax1.get_ylabel().lower() == 'y'\n",
"\n",
"assert ax2.get_title().lower() == 'plot xz', f\"{ax.get_title()} is not the same as plot xz\"\n",
"assert ax2.get_xlabel().lower() == 'x'\n",
"assert ax2.get_ylabel().lower() == 'z'\n",
"### END HIDDEN TEST"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-383cddeb2332477d",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"\n",
"## Exercise 3\n",
"\n",
"Create the plot below by adding two axes to a figure object at [0,0,1,1] and name it __Plot A__ and at [0.2,0.5,0.4,0.4] and name it as __Plot B__.\n",
"![Exercise 3](exercise3.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"fig = plt.figure()\n",
"\n",
"ax = fig.add_axes([0,0,1,1])\n",
"ax.set_title('Plot A')\n",
"\n",
"ax2 = fig.add_axes([0.2,0.5,0.4,0.4])\n",
"ax2.set_title('Plot B')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-f1b47006770bcca0",
"locked": true,
"points": 0,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"outputs": [],
"source": [
"### BEGIN HIDDEN TEST\n",
"assert isinstance(ax,matplotlib.axes._axes.Axes), f\"{ax} is not an axis \"\n",
"assert isinstance(ax2,matplotlib.axes._axes.Axes), f\"{ax2} is not an axis \"\n",
"assert ax.get_title().lower() == 'plot a', f\"{ax.get_title()} is not the same as plot A\"\n",
"assert ax2.get_title().lower() == 'plot b', f\"{ax2.get_title()} is not the same as plot B\"\n",
"### END HIDDEN TEST"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-91b7c64bab74fa07",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"Now use x and z arrays to create a zoomed version as shown in the plot below. Notice the x limits and y limits on the inserted plot:![Exercise 3.b](exercise3_b.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"ax.plot(x,z)\n",
"ax.set_xlabel('X')\n",
"ax.set_ylabel('Z')\n",
"ax.set_title('full')\n",
"\n",
"ax2.plot(x,z)\n",
"ax2.set_xlabel('X')\n",
"ax2.set_ylabel('Z')\n",
"ax2.set_title('zoom')\n",
"ax2.set_xlim(20,40)\n",
"ax2.set_ylim(0,2000)\n",
"\n",
"fig\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-725ebaa6bc9455b8",
"locked": true,
"points": 0,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"outputs": [],
"source": [
"### BEGIN HIDDEN TEST\n",
"assert isinstance(ax,matplotlib.axes._axes.Axes), f\"{ax1} is not an axis \"\n",
"assert isinstance(ax2,matplotlib.axes._axes.Axes), f\"{ax2} is not an axis \"\n",
"assert ax.get_title().lower() == 'full', f\"{ax.get_title()} is not the same as full\"\n",
"assert ax2.get_title().lower() == 'zoom', f\"{ax.get_title()} is not the same as zoom\"\n",
"### END HIDDEN TEST"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-29c04b7329cfa60e",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"## Exercise 4\n",
"\n",
"Use plt.subplots(nrows=1, ncols=2) to create empty plot below. Name the first subplot as __xy__ and second subplot as __xz__.\n",
"\n",
"![Exercise 4_a](exercise4_a.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"fig, axes = plt.subplots(nrows=1, ncols=2)\n",
"axes[0].set_title('xy')\n",
"axes[1].set_title('xz')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-046fc90eb41678c4",
"locked": true,
"points": 0,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"outputs": [],
"source": [
"### BEGIN HIDDEN TEST\n",
"assert isinstance(axes,np.ndarray)\n",
"assert isinstance(axes[0],matplotlib.axes._axes.Axes) and isinstance(axes[1],matplotlib.axes._axes.Axes)\n",
"assert axes[0].get_title().lower() == 'xy', f\"{axes[0].get_title()} is not the same as xy\"\n",
"assert axes[1].get_title().lower() == 'xz', f\"{axes[1].get_title()} is not the same as xz\"\n",
"### END HIDDEN TEST"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-3e6118aac1523838",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
" Now plot (x,y) and (x,z) on the respective axes. Set the linewidth to 3pt, marker to '__*__' and color of __xy__ plot to red and for the __xz__ plot, set color to blue and linestyle to dashed. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"axes[0].plot(x,y,color=\"blue\", lw=3, marker='*')\n",
"axes[0].set_xlabel('x')\n",
"axes[0].set_ylabel('y')\n",
"\n",
"axes[1].plot(x,z,color=\"red\", lw=3, ls='--')\n",
"axes[1].set_xlabel('x')\n",
"axes[1].set_ylabel('z')\n",
"\n",
"fig\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-8690f8c5e6e59a90",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"Plot the same figure with a size of (10,2) by adding the __figsize__ argument in plt.subplots()."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"fig, axes = plt.subplots(nrows=1, ncols=2,figsize=(10,2))\n",
"\n",
"axes[0].plot(x,y,color=\"blue\", lw=3, marker='*')\n",
"axes[0].set_xlabel('x')\n",
"axes[0].set_ylabel('y')\n",
"\n",
"axes[1].plot(x,z,color=\"red\", lw=3, ls='--')\n",
"axes[1].set_xlabel('x')\n",
"axes[1].set_ylabel('z')\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-becf64c4bbc48d1f",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"## Bar plots\n",
"\n",
"Bar plot enables us to visualize the distribution of categorical data variables. They represent distribution of discrete values. Thus, it represents the comparison of categorical values. The x axis represents the discrete values while the y axis represents the numeric values of comparison and vice versa.The open source documentation can be found under https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-d509dfb9ce8c8417",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"## Exercise 5\n",
"\n",
"Initialize the variables with the following data and plot a bar graph to vizualize the popularity of various Programming languages.\n",
"\n",
"|Programming Languages|Java|Python|PHP|JavaScript|C\\#|C++|\n",
"|--|--|--|--|--|--|--|\n",
"|Popularity|22.2|17.6|8.8|8|7.7|6.7|\n",
"\n",
"- The plot should have grids on (both major and minor). \n",
"- Name the title of the figure as __Popularity of Programming Language Worldwide__.\n",
"- Label both x and y axes with respective labels.\n",
"\n",
"The final plot should look like this:\n",
"\n",
"![Exercise 5a](exercise5_a.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"x = ['Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++']\n",
"p = [22.2, 17.6, 8.8, 8, 7.7, 6.7]\n",
"x_pos = [i for i,val in enumerate(x)]\n",
"\n",
"fig = plt.figure()\n",
"ax = fig.add_axes([0,0,1,1])\n",
"\n",
"ax.set_title('Popularity of Programming Language Worldwide')\n",
"ax.set_xlabel('Languages')\n",
"ax.set_ylabel('Popularity')\n",
"\n",
"ax.minorticks_on()\n",
"ax.grid(which='minor',linestyle=':')\n",
"ax.grid(which='major',linestyle='-')\n",
"\n",
"bar = ax.bar(x_pos,p)\n",
"ax.set_xticks(x_pos,x)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-5e9ee62323d8fb2d",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"Change the color of the bars to ['red', 'black', 'green', 'blue', 'yellow', 'cyan'] :\n",
"\n",
"![Exercise 5c](exercise5_c.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"colors = ['red', 'black', 'green', 'blue', 'yellow', 'cyan']\n",
"for i,color in enumerate(colors):\n",
" bar[i].set_color(color)\n",
"fig\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-345c3a5c2f31b1c4",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"Plot the same data as horizontal bar graph in a new figure as shown below:\n",
"\n",
"![Exercise 5b](exercise5_b.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"fig = plt.figure()\n",
"ax = fig.add_axes([0,0,1,1])\n",
"ax.set_title('Popularity of Programming Language Worldwide')\n",
"ax.set_xlabel('Popularity')\n",
"ax.set_ylabel('Languages')\n",
"\n",
"ax.minorticks_on()\n",
"ax.grid(which='minor',linestyle=':')\n",
"ax.grid(which='major',linestyle='-')\n",
"\n",
"ax.barh(x_pos,p)\n",
"ax.set_yticks(x_pos,x)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-4356eca51fa89384",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"## Pie Charts\n",
"\n",
"A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. In a pie chart, the arc length of each slice is proportional to the quantity it represents. Pie charts are a popular way to represent the results of polls. The open source documentation can be found under https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pie.html#matplotlib.pyplot.pie"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-6c1fcc8a4ad86fac",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"## Exercise 6\n",
"\n",
"Initialize the following data and plot a pie chart to visualize the prefered sports among people:\n",
"|Sports|Cricket|Football|Hockey|F1|\n",
"|--|--|--|--|--|\n",
"|People|15|30|45|10|\n",
"\n",
"The sports title should be the labels of the pie chart as shown below:\n",
"\n",
"![Exercise 6a](exercise6_a.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
"s = ['Cricket', 'Football', 'Hockey', 'F1']\n",
"p = [15, 30, 45, 10]\n",
"\n",
"fig,ax = plt.subplots()\n",
"ax.pie(p,labels=s)\n",
"# ax.axis('equal')\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-d16322e4cb78e72c",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"# Contour plots\n",
"\n",
"Contour plots also called level plots are a tool for doing multivariate analysis and visualizing 3-D plots in 2-D space. If we consider X and Y as our variables we want to plot then the response Z will be plotted as slices on the X-Y plane due to which contours are sometimes referred as Z-slices or iso-response.\n",
"\n",
"Contour plots are widely used to visualize density, altitudes or heights of the mountain as well as in the meteorological department. Due to such wide usage matplotlib.pyplot provides a method contour to make it easy for us to draw contour plots."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"grade_id": "cell-75ecdb619bbc13d3",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
},
"tags": []
},
"source": [
"## Exercise 7\n",
"\n",
"Make a contour plot of the equation $Z=X^2+Y^2$ by first creating a mesh grid between $x$ and $y$. The plot should look as follows:\n",
"\n",
"![Exercise 7a](exercise7_a.png)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"x = np.arange(0,100)\n",
"y = x*2\n",
"\n",
"\n",
"[X, Y] = np.meshgrid(x, y)\n",
"fig, ax = plt.subplots(1, 1)\n",
" \n",
"Z = X**2 + Y**2 \n",
"ax.contour(X, Y, Z)\n",
"\n",
"ax.set_title('Contour Plot')\n",
"ax.set_xlabel('X')\n",
"ax.set_ylabel('Y')\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.16"
},
"vscode": {
"interpreter": {
"hash": "23df9ff646ca1c5e2dfe7a3d7568c302b6a7972f96b6a2ba92f9d9e3e979b69c"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB