Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Advanced Matplotlib in Python 1695062970

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Advanced Matplotlib in Python 1695062970

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

9/18/23, 11:59 AM 2.

advanced matplotlib - Jupyter Notebook

In [1]:

1 import numpy as np
2 import pandas as pd
3 import matplotlib.pyplot as plt

Colored Scatterplots
In [2]:

1 iris = pd.read_csv('iris.csv')
2 iris.head()

Out[2]:

Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

In [3]:

1 plt.scatter(iris['SepalLengthCm'],iris['PetalLengthCm'])
2
3 plt.xlabel('Sepal Length')
4 plt.ylabel('Petal Length')
5
6 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 1/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [4]:

1 iris['Species'] = iris['Species'].replace({'Iris-setosa':0,'Iris-versicolor':1,'Iris
2 iris

Out[4]:

Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 0

1 2 4.9 3.0 1.4 0.2 0

2 3 4.7 3.2 1.3 0.2 0

3 4 4.6 3.1 1.5 0.2 0

4 5 5.0 3.6 1.4 0.2 0

... ... ... ... ... ... ...

145 146 6.7 3.0 5.2 2.3 2

146 147 6.3 2.5 5.0 1.9 2

147 148 6.5 3.0 5.2 2.0 2

148 149 6.2 3.4 5.4 2.3 2

In [5]:

1 plt.scatter(iris['SepalLengthCm'],iris['PetalLengthCm'], c=iris['Species'])
2
3 plt.xlabel('Sepal Length')
4 plt.ylabel('Petal Length')
5
6 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 2/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [6]:

1 plt.scatter(iris['SepalLengthCm'],iris['PetalLengthCm'], c=iris['Species'],
2 cmap='winter')
3
4 plt.xlabel('Sepal Length')
5 plt.ylabel('Petal Length')
6
7 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 3/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [7]:

1 plt.scatter(iris['SepalLengthCm'],iris['PetalLengthCm'], c=iris['Species'],
2 cmap='winter')
3
4 plt.xlabel('Sepal Length')
5 plt.ylabel('Petal Length')
6
7 plt.colorbar() # adding the colour bar
8 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 4/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [8]:

1 plt.scatter(iris['SepalLengthCm'],iris['PetalLengthCm'], c=iris['Species'],
2 cmap='winter', alpha=0.8)
3 # alpha parameter will show the instensity of the colour
4
5 plt.xlabel('Sepal Length')
6 plt.ylabel('Petal Length')
7
8 plt.colorbar()
9 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 5/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

Plot size
In [9]:

1 # increase or decrease the graph size


2 # for that use the function plt.figure(figsize=(6,6))
3
4 plt.figure(figsize=(10,6))
5
6 plt.scatter(iris['SepalLengthCm'],iris['PetalLengthCm'], c=iris['Species'],
7 cmap='winter', alpha=.8)
8
9 plt.xlabel('Sepal Length')
10 plt.ylabel('Petal Length')
11
12 plt.colorbar()
13 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 6/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

Annotations
In [10]:

1 # sample code
2
3 x = [1,2,3,4]
4 y = [5,6,7,8]
5
6 plt.scatter(x,y)
7
8 plt.text(1,5,'Point 1')
9 plt.text(2,6,'Point 2')
10 plt.text(3,7,'Point 3')
11 plt.text(4,8,'Point 4')
12
13 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 7/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [11]:

1 x = [1,2,3,4]
2 y = [5,6,7,8]
3
4 plt.scatter(x,y)
5
6 plt.text(1,5,'Point 1', fontdict={'size':10,'color':'green'})
7 plt.text(2,6,'Point 2', fontdict={'size':14,'color':'black'})
8 plt.text(3,7,'Point 3', fontdict={'size':18,'color':'grey'})
9 plt.text(4,8,'Point 4', fontdict={'size':22,'color':'blue'})
10
11 plt.show()

using the real data to show the annotations

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 8/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [12]:

1 batter = pd.read_csv('batter.csv')
2 batter

Out[12]:

batter runs avg strike_rate

0 V Kohli 6634 36.251366 125.977972

1 S Dhawan 6244 34.882682 122.840842

2 DA Warner 5883 41.429577 136.401577

3 RG Sharma 5881 30.314433 126.964594

4 SK Raina 5536 32.374269 132.535312

... ... ... ... ...

600 C Nanda 0 0.000000 0.000000

601 Akash Deep 0 0.000000 0.000000

602 S Ladda 0 0.000000 0.000000

603 V Pratap Singh 0 0.000000 0.000000

604 S Lamichhane 0 0.000000 0.000000

605 rows × 4 columns

In [13]:

1 batter.shape

Out[13]:

(605, 4)

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 9/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [14]:

1 sample_df = batter.head(100).sample(25,random_state=5)
2 sample_df

Out[14]:

batter runs avg strike_rate

66 KH Pandya 1326 22.100000 132.203390

32 SE Marsh 2489 39.507937 130.109775

46 JP Duminy 2029 39.784314 120.773810

28 SA Yadav 2644 29.707865 134.009123

74 IK Pathan 1150 21.698113 116.751269

23 JC Buttler 2832 39.333333 144.859335

10 G Gambhir 4217 31.007353 119.665153

20 BB McCullum 2882 27.711538 126.848592

17 KA Pollard 3437 28.404959 140.457703

35 WP Saha 2427 25.281250 124.397745

In [15]:

1 plt.scatter(sample_df['avg'], sample_df['strike_rate'] )
2 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 10/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [16]:

1 plt.scatter(sample_df['avg'], sample_df['strike_rate'] )
2
3 for i in range(sample_df.shape[0]):
4 plt.text(sample_df['avg'].values[i],sample_df['strike_rate'].values[i],
5 sample_df['batter'].values[i])
6
7 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 11/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [17]:

1 # increase the figure size


2 plt.figure(figsize=(18,10))
3
4 plt.scatter(sample_df['avg'], sample_df['strike_rate'] )
5
6 for i in range(sample_df.shape[0]):
7 plt.text(sample_df['avg'].values[i],sample_df['strike_rate'].values[i],
8 sample_df['batter'].values[i])
9
10 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 12/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [18]:

1 '''we want to show the size of the each dot


2 based on the runs scored by the batsmen'''
3
4 plt.figure(figsize=(18,10))
5
6 plt.scatter(sample_df['avg'], sample_df['strike_rate'], s=sample_df['runs'] )
7 # including the s parameter for showing the size of the data point
8
9 for i in range(sample_df.shape[0]):
10 plt.text(sample_df['avg'].values[i],sample_df['strike_rate'].values[i],
11 sample_df['batter'].values[i])
12
13 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 13/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

Horizontal and Vertical lines


In [19]:

1 '''suppose we want batsmen who has strike rate of 130,


2 for that we can plot a line'''
3
4 plt.figure(figsize=(18,10))
5 plt.scatter(sample_df['avg'],sample_df['strike_rate'],s=sample_df['runs'])
6
7 plt.axhline(130,color='red')
8 plt.axhline(140,color='green')
9 plt.axvline(30,color='black')
10
11 for i in range(sample_df.shape[0]):
12 plt.text(sample_df['avg'].values[i],
13 sample_df['strike_rate'].values[i],
14 sample_df['batter'].values[i])
15
16 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 14/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

Subplot
In [20]:

1 batter = pd.read_csv('batter.csv')
2 batter

Out[20]:

batter runs avg strike_rate

0 V Kohli 6634 36.251366 125.977972

1 S Dhawan 6244 34.882682 122.840842

2 DA Warner 5883 41.429577 136.401577

3 RG Sharma 5881 30.314433 126.964594

4 SK Raina 5536 32.374269 132.535312

... ... ... ... ...

600 C Nanda 0 0.000000 0.000000

601 Akash Deep 0 0.000000 0.000000

602 S Ladda 0 0.000000 0.000000

603 V Pratap Singh 0 0.000000 0.000000

604 S Lamichhane 0 0.000000 0.000000

605 rows × 4 columns

In [21]:

1 # A diff way to plot graphs


2 batter.head()

Out[21]:

batter runs avg strike_rate

0 V Kohli 6634 36.251366 125.977972

1 S Dhawan 6244 34.882682 122.840842

2 DA Warner 5883 41.429577 136.401577

3 RG Sharma 5881 30.314433 126.964594

4 SK Raina 5536 32.374269 132.535312

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 15/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [22]:

1 plt.subplots()

Out[22]:

(<Figure size 640x480 with 1 Axes>, <AxesSubplot:>)

in the above code plt.subplots() we are getting two object, figure and axis. now we will separate this two
objects with help of unpacking of the plt.subplots()

In [23]:

1 fig, ax = plt.subplots()
2
3 ax.scatter(batter['avg'],batter['strike_rate'])
4 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 16/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [24]:

1 fig, ax = plt.subplots()
2
3 ax.scatter(batter['avg'],batter['strike_rate'])
4 ax.set_title("comparison")
5 ax.set_xlabel("Avg")
6 ax.set_ylabel("Strike Rate")
7
8 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 17/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [25]:

1 fig, ax = plt.subplots(figsize=(12,8))
2
3 ax.scatter(batter['avg'],batter['strike_rate'])
4 ax.set_title("comparison")
5 ax.set_xlabel("Avg")
6 ax.set_ylabel("Strike Rate")
7
8 plt.show()

In [26]:

1 fig, ax = plt.subplots(nrows=4,ncols=4, figsize=(12,8))


2 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 18/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [27]:

1 fig, ax = plt.subplots(nrows=2,ncols=1)
2 plt.show()

In [28]:

1 '''fig is our figure where our graph will be plotted'''


2 fig

Out[28]:

In [29]:

1 '''we have total two axis here'''


2 ax

Out[29]:

array([<AxesSubplot:>, <AxesSubplot:>], dtype=object)

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 19/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [30]:

1 fig, ax = plt.subplots(nrows=2,ncols=1)
2
3 ax[0].scatter(batter['avg'],batter['strike_rate'])
4 ax[1].scatter(batter['avg'],batter['runs'])
5
6 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 20/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [31]:

1 fig, ax = plt.subplots(nrows=2,ncols=1)
2
3 ax[0].scatter(batter['avg'],batter['strike_rate'])
4 ax[1].scatter(batter['avg'],batter['runs'])
5
6 ax[0].set_title("avg Vs strike rate")
7 ax[0].set_ylabel("strike rate")
8
9 ax[1].set_title("avg Vs runs")
10 ax[1].set_ylabel("runs")
11 ax[1].set_xlabel("avg")
12
13 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 21/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [32]:

1 fig, ax = plt.subplots(nrows=2,ncols=1,sharex=True, figsize = (10,8))


2
3 ax[0].scatter(batter['avg'],batter['strike_rate'], c="red")
4 ax[1].scatter(batter['avg'],batter['runs'], c='green')
5
6 ax[0].set_title("avg Vs strike rate")
7 ax[0].set_ylabel("strike rate")
8
9 ax[1].set_title("avg Vs runs")
10 ax[1].set_ylabel("runs")
11 ax[1].set_xlabel("avg")
12
13 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 22/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [33]:

1 fig, ax = plt.subplots(nrows=2,ncols=2,figsize=(10,10)) #we will get 2D array axis


2
3 ax[0,0].scatter(batter['avg'],batter['strike_rate'])
4 ax[0,0].set_title('avg vs strike_rate')
5
6 ax[0,1].scatter(batter['avg'],batter['runs'])
7 ax[0,1].set_title('avg vs runs')
8
9 ax[1,0].hist(batter['avg'])
10 ax[1,0].set_title('avg')
11
12 ax[1,1].hist(batter['runs'])
13 ax[1,1].set_title('runs')
14
15 plt.show()

another way to create subplots

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 23/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [34]:

1 fig = plt.figure(figsize=(9,9))
2
3 ax1 = fig.add_subplot(2,2,1)
4 ax1.scatter(batter['avg'],batter['strike_rate'],color='red')
5
6 ax2 = fig.add_subplot(2,2,2)
7 ax2.hist(batter['runs'])
8
9 ax3 = fig.add_subplot(2,2,4)
10 ax3.hist(batter['avg'])
11
12 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 24/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

3D scatter plot
In [35]:

1 batter = pd.read_csv('batter.csv')
2 batter

Out[35]:

batter runs avg strike_rate

0 V Kohli 6634 36.251366 125.977972

1 S Dhawan 6244 34.882682 122.840842

2 DA Warner 5883 41.429577 136.401577

3 RG Sharma 5881 30.314433 126.964594

4 SK Raina 5536 32.374269 132.535312

... ... ... ... ...

600 C Nanda 0 0.000000 0.000000

601 Akash Deep 0 0.000000 0.000000

602 S Ladda 0 0.000000 0.000000

603 V Pratap Singh 0 0.000000 0.000000

In [36]:

1 batter

Out[36]:

batter runs avg strike_rate

0 V Kohli 6634 36.251366 125.977972

1 S Dhawan 6244 34.882682 122.840842

2 DA Warner 5883 41.429577 136.401577

3 RG Sharma 5881 30.314433 126.964594

4 SK Raina 5536 32.374269 132.535312

... ... ... ... ...

600 C Nanda 0 0.000000 0.000000

601 Akash Deep 0 0.000000 0.000000

602 S Ladda 0 0.000000 0.000000

603 V Pratap Singh 0 0.000000 0.000000

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 25/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [37]:

1 fig = plt.figure()
2
3 ax = plt.subplot(projection="3d")
4
5 plt.show()

In [38]:

1 fig = plt.figure()
2
3 ax = plt.subplot(projection="3d")
4 ax.scatter3D(batter['runs'],batter['avg'],batter['strike_rate'])
5
6 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 26/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [39]:

1 fig = plt.figure()
2
3 ax = plt.subplot(projection="3d")
4 ax.scatter3D(batter['runs'],batter['avg'],batter['strike_rate'])
5
6 ax.set_title('IPL batsman analysis')
7 ax.set_xlabel('Runs')
8 ax.set_ylabel('Avg')
9 ax.set_zlabel('SR')
10
11 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 27/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

3D line plot
In [40]:

1 x = [0,1,5,25]
2 y = [0,10,13,0]
3 z = [0,13,20,9]
4
5 fig = plt.figure()
6
7 ax = plt.subplot(projection="3d")
8
9 ax.scatter3D(x,y,z,s=[100,100,100,100])
10 ax.plot3D(x,y,z, color='red')
11 plt.show()

3D Surface plot
In [41]:

1 x = np.linspace(-10,10,100)
2 y = np.linspace(-10,10,100)

In [42]:

1 xx, yy = np.meshgrid(x,y)
2 xx.shape

Out[42]:

(100, 100)

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 28/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [43]:

1 # we want to create z with help of x and y


2 z = xx**2 + yy**2
3 z.shape

Out[43]:

(100, 100)

In [44]:

1 fig = plt.figure(figsize=(12,8))
2
3 ax = plt.subplot(projection='3d')
4 ax.plot_surface(xx,yy,z)
5
6 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 29/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [45]:

1 fig = plt.figure(figsize=(12,8))
2
3 ax = plt.subplot(projection='3d')
4 p = ax.plot_surface(xx,yy,z, cmap='viridis')
5
6 fig.colorbar(p)
7 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 30/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [46]:

1 x = np.linspace(-10,10,100)
2 y = np.linspace(-10,10,100)
3
4 xx, yy = np.meshgrid(x,y)
5
6 z = np.sin(xx) + np.cos(yy)
7
8 fig = plt.figure(figsize=(12,8))
9
10 ax = plt.subplot(projection='3d')
11 p = ax.plot_surface(xx,yy,z, cmap='viridis')
12
13 fig.colorbar(p)
14 plt.show()

Contour plot
In [47]:

1 x = np.linspace(-10,10,100)
2 y = np.linspace(-10,10,100)
3
4 xx, yy = np.meshgrid(x,y)
5
6 z = xx**2 + yy**2

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 31/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [48]:

1 fig = plt.figure(figsize=(12,8))
2
3 ax = plt.subplot()
4 p = ax.contour(xx,yy,z, cmap='viridis')
5
6 fig.colorbar(p)
7 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 32/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [49]:

1 # filled cointour graph


2
3 fig = plt.figure(figsize=(12,8))
4
5 ax = plt.subplot()
6 p = ax.contourf(xx,yy,z, cmap='viridis')
7
8 fig.colorbar(p)
9 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 33/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [50]:

1 x = np.linspace(-10,10,100)
2 y = np.linspace(-10,10,100)
3
4 xx, yy = np.meshgrid(x,y)
5
6 z = np.sin(xx) + np.cos(yy)
7
8 fig = plt.figure(figsize=(12,8))
9
10 ax = plt.subplot()
11 p = ax.contourf(xx,yy,z, cmap='viridis')
12
13 fig.colorbar(p)
14 plt.show()

Heatmap
A heat map is a two-dimensional representation of data in which values are represented by colors. A
simple heat map provides an immediate visual summary of information. More elaborate heat maps
allow the viewer to understand complex data sets.

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 34/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [51]:

1 delivery = pd.read_csv("IPL_Ball_by_Ball_2008_2022.csv")
2 delivery.head()

Out[51]:

non-
ID innings overs ballnumber batter bowler extra_type batsman_ru
striker

YBK Mohammed JC
0 1312200 1 0 1 NaN
Jaiswal Shami Buttler

YBK Mohammed JC
1 1312200 1 0 2 legbyes
Jaiswal Shami Buttler

JC Mohammed YBK
2 1312200 1 0 3 NaN
Buttler Shami Jaiswal

YBK Mohammed JC
3 1312200 1 0 4 NaN
Jaiswal Shami Buttler

YBK Mohammed JC
4 1312200 1 0 5 NaN
Jaiswal Shami Buttler

we want the data of of 20 overs of all the ipl matches played so far and then we want the six hitted by
the batsman on each ball of the 20 over

In [52]:

1 delivery['ballnumber'].unique()

Out[52]:

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=int64)

In [53]:

1 temp_df = delivery[(delivery['ballnumber'].isin([1,2,3,4,5,6]))
2 & (delivery['batsman_run']==6)]

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 35/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [54]:

1 temp_df.pivot_table(index='overs',columns='ballnumber',
2 values='batsman_run', aggfunc='count')

Out[54]:

ballnumber 1 2 3 4 5 6

overs

0 9 17 31 39 33 27

1 31 40 49 56 58 54

2 75 62 70 72 58 76

3 60 74 74 103 74 71

4 71 76 112 80 81 72

5 77 102 63 86 78 80

6 34 56 49 59 64 38

7 59 62 73 70 69 56

8 86 83 79 81 73 52

In [55]:

1 grid = temp_df.pivot_table(index='overs',columns='ballnumber',
2 values='batsman_run', aggfunc='count')

In [56]:

1 plt.figure(figsize=(20,10))
2 plt.imshow(grid)
3 plt.colorbar()
4 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 36/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [57]:

1 plt.figure(figsize=(20,10))
2 plt.imshow(grid)
3 plt.colorbar()
4
5 plt.yticks(delivery['overs'].unique(), list(range(1,21)))
6 plt.xticks(np.arange(0,6), list(range(1,7)))
7 plt.show()

Pandas Plots()
In [58]:

1 # on a series
2
3 s = pd.Series([1,2,3,4,5,6,7])
4 s.plot(kind='pie')

Out[58]:

<AxesSubplot:ylabel='None'>

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 37/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

can be used on a dataframe as well

In [59]:

1 import seaborn as sns

In [60]:

1 tips = sns.load_dataset('tips')
2 tips.head()

Out[60]:

total_bill tip sex smoker day time size

0 16.99 1.01 Female No Sun Dinner 2

1 10.34 1.66 Male No Sun Dinner 3

2 21.01 3.50 Male No Sun Dinner 3

3 23.68 3.31 Male No Sun Dinner 2

4 24.59 3.61 Female No Sun Dinner 4

Scatter plot -> labels -> markers -> figsize -> color -> cmap

In [61]:

1 tips.plot(kind='scatter',x='total_bill',y='tip')

Out[61]:

<AxesSubplot:xlabel='total_bill', ylabel='tip'>

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 38/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [62]:

1 tips.plot(kind='scatter',x='total_bill',y='tip',title='Cost Analysis',color='red',
2 marker='*',figsize=(10,6))

Out[62]:

<AxesSubplot:title={'center':'Cost Analysis'}, xlabel='total_bill', yla


bel='tip'>

In [63]:

1 tips.plot(kind='scatter',x='total_bill',y='tip',title='Cost Analysis',
2 marker='*',figsize=(10,6),c='sex',cmap='viridis')

Out[63]:

<AxesSubplot:title={'center':'Cost Analysis'}, xlabel='total_bill', yla


bel='tip'>

dataset = 'https://raw.githubusercontent.com/m-
mehdi/pandas_tutorials/main/weekly_stocks.csv' (https://raw.githubusercontent.com/m-
mehdi/pandas_tutorials/main/weekly_stocks.csv')

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 39/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [64]:

1 stocks = pd.read_csv('weekly_stocks.csv')
2 stocks.head()

Out[64]:

Date MSFT FB AAPL

0 2021-05-24 249.679993 328.730011 124.610001

1 2021-05-31 250.789993 330.350006 125.889999

2 2021-06-07 257.890015 331.260010 127.349998

3 2021-06-14 259.429993 329.660004 130.460007

4 2021-06-21 265.019989 341.369995 133.110001

line plot

In [65]:

1 stocks['MSFT'].plot(kind='line')
2 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 40/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [66]:

1 stocks.plot(kind='line')
2 plt.show()

In [67]:

1 stocks.plot(kind='line',x='Date')
2
3 plt.xticks(rotation='50')
4 plt.show()

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 41/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [68]:

1 stocks[['Date','MSFT','FB']].plot(kind='line',x='Date')
2 # here by using the fancy indexing we will showcase the MSFT and FB only
3
4 plt.xticks(rotation='50')
5 plt.show()

bat chart

In [69]:

1 temp = pd.read_csv('batsman_season_record.csv')
2 temp

Out[69]:

batsman 2015 2016 2017

0 AB de Villiers 513 687 216

1 DA Warner 562 848 641

2 MS Dhoni 372 284 290

3 RG Sharma 482 489 333

4 V Kohli 505 973 308

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 42/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [70]:

1 temp['2015'].plot(kind='bar')

Out[70]:

<AxesSubplot:>

In [71]:

1 tips

Out[71]:

total_bill tip sex smoker day time size

0 16.99 1.01 Female No Sun Dinner 2

1 10.34 1.66 Male No Sun Dinner 3

2 21.01 3.50 Male No Sun Dinner 3

3 23.68 3.31 Male No Sun Dinner 2

4 24.59 3.61 Female No Sun Dinner 4

... ... ... ... ... ... ... ...

239 29.03 5.92 Male No Sat Dinner 3

240 27.18 2.00 Female Yes Sat Dinner 2

241 22.67 2.00 Male Yes Sat Dinner 2

242 17.82 1.75 Male No Sat Dinner 2

243 18.78 3.00 Female No Thur Dinner 2

244 rows × 7 columns

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 43/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [72]:

1 tips.groupby('sex')['total_bill'].mean()

Out[72]:

sex
Male 20.744076
Female 18.056897
Name: total_bill, dtype: float64

In [73]:

1 tips.groupby('sex')['total_bill'].mean().plot(kind='bar')

Out[73]:

<AxesSubplot:xlabel='sex'>

In [74]:

1 temp

Out[74]:

batsman 2015 2016 2017

0 AB de Villiers 513 687 216

1 DA Warner 562 848 641

2 MS Dhoni 372 284 290

3 RG Sharma 482 489 333

4 V Kohli 505 973 308

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 44/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [75]:

1 temp.plot(kind='bar',x='batsman')

Out[75]:

<AxesSubplot:xlabel='batsman'>

In [76]:

1 temp.plot(kind='bar', stacked=True, x='batsman')

Out[76]:

<AxesSubplot:xlabel='batsman'>

histogram

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 45/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [77]:

1 stocks.head()

Out[77]:

Date MSFT FB AAPL

0 2021-05-24 249.679993 328.730011 124.610001

1 2021-05-31 250.789993 330.350006 125.889999

2 2021-06-07 257.890015 331.260010 127.349998

3 2021-06-14 259.429993 329.660004 130.460007

4 2021-06-21 265.019989 341.369995 133.110001

In [78]:

1 stocks.plot(kind='hist')

Out[78]:

<AxesSubplot:ylabel='Frequency'>

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 46/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [79]:

1 stocks[['MSFT','FB']].plot(kind='hist',bins=40)

Out[79]:

<AxesSubplot:ylabel='Frequency'>

Pie charts

In [80]:

1 df = pd.DataFrame(
2 {
3 'batsman':['Dhawan','Rohit','Kohli','SKY','Pandya','Pant'],
4 'match1':[120,90,35,45,12,10],
5 'match2':[0,1,123,130,34,45],
6 'match3':[50,24,145,45,10,90]
7 }
8 )
9
10 df.head()

Out[80]:

batsman match1 match2 match3

0 Dhawan 120 0 50

1 Rohit 90 1 24

2 Kohli 35 123 145

3 SKY 45 130 45

4 Pandya 12 34 10

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 47/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [81]:

1 df['match1'].plot(kind='pie', labels=df['batsman'])

Out[81]:

<AxesSubplot:ylabel='match1'>

In [82]:

1 df['match1'].plot(kind='pie', labels=df['batsman'], autopct='%0.1f%%',


2 explode=[0,0,0,0,0,0])

Out[82]:

<AxesSubplot:ylabel='match1'>

multiple pie charts

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 48/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [83]:

1 df[['match1','match2','match3']].plot(kind='pie', subplots=True, figsize=(15,8),


2 labels=df['batsman'], grid=True, fontsize=12, autopct = '%0.2f%%')

Out[83]:

array([<AxesSubplot:ylabel='match1'>, <AxesSubplot:ylabel='match2'>,
<AxesSubplot:ylabel='match3'>], dtype=object)

multiple separate graphs together

In [84]:

1 stocks.head()

Out[84]:

Date MSFT FB AAPL

0 2021-05-24 249.679993 328.730011 124.610001

1 2021-05-31 250.789993 330.350006 125.889999

2 2021-06-07 257.890015 331.260010 127.349998

3 2021-06-14 259.429993 329.660004 130.460007

4 2021-06-21 265.019989 341.369995 133.110001

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 49/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [85]:

1 stocks.plot(kind='line', subplots=True)

Out[85]:

array([<AxesSubplot:>, <AxesSubplot:>, <AxesSubplot:>], dtype=object)

multiindex dataframe

In [86]:

1 tips.head()

Out[86]:

total_bill tip sex smoker day time size

0 16.99 1.01 Female No Sun Dinner 2

1 10.34 1.66 Male No Sun Dinner 3

2 21.01 3.50 Male No Sun Dinner 3

3 23.68 3.31 Male No Sun Dinner 2

4 24.59 3.61 Female No Sun Dinner 4

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 50/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [87]:

1 tips.pivot_table(index='day', columns='sex', values='total_bill',


2 aggfunc='mean').plot(kind='line')

Out[87]:

<AxesSubplot:xlabel='day'>

In [88]:

1 tips.pivot_table(index=['day','time'], columns='sex', values='total_bill',


2 aggfunc='mean')

Out[88]:

sex Male Female

day time

Thur Lunch 18.714667 16.648710

Dinner NaN 18.780000

Fri Lunch 11.386667 13.940000

Dinner 23.487143 14.310000

Sat Dinner 20.802542 19.680357

Sun Dinner 21.887241 19.872222

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 51/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [89]:

1 tips.pivot_table(index=['day','time'], columns='sex', values='total_bill',


2 aggfunc='mean').plot(kind='line')

Out[89]:

<AxesSubplot:xlabel='day,time'>

In [90]:

1 tips.pivot_table(index=['day','time'], columns=['sex','smoker'], values='total_bill'


2 aggfunc='mean')

Out[90]:

sex Male Female

smoker Yes No Yes No

day time

Thur Lunch 19.171000 18.486500 19.218571 15.899167

Dinner NaN NaN NaN 18.780000

Fri Lunch 11.386667 NaN 13.260000 15.980000

Dinner 25.892000 17.475000 12.200000 22.750000

Sat Dinner 21.837778 19.929063 20.266667 19.003846

Sun Dinner 26.141333 20.403256 16.540000 20.824286

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 52/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [91]:

1 tips.pivot_table(index=['day','time'], columns=['sex','smoker'], values='total_bill'


2 aggfunc='mean').plot(kind='line')

Out[91]:

<AxesSubplot:xlabel='day,time'>

In [92]:

1 tips.pivot_table(index=['day','time'], columns=['sex','smoker'], values='total_bill'


2 aggfunc='mean').plot(kind='bar')

Out[92]:

<AxesSubplot:xlabel='day,time'>

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 53/54


9/18/23, 11:59 AM 2. advanced matplotlib - Jupyter Notebook

In [93]:

1 tips.pivot_table(index=['day','time'], columns=['sex','smoker'], values='total_bill'


2 aggfunc='mean').plot(kind='pie', subplots=True, figsize=(20,10))

Out[93]:

array([<AxesSubplot:ylabel='(Male, Yes)'>,
<AxesSubplot:ylabel='(Male, No)'>,
<AxesSubplot:ylabel='(Female, Yes)'>,
<AxesSubplot:ylabel='(Female, No)'>], dtype=object)

localhost:8888/notebooks/1. Python/4. Python libraries/3. Matplotlib/Campus X Tutorials/2. advanced matplotlib.ipynb 54/54

You might also like