0% found this document useful (0 votes)
2 views

Matplotlib Data Visualization Notebook

Matplotlib is a popular Python library for creating static, animated, and interactive visualizations with a simple and customizable interface. It supports various plot types and is commonly used for exploratory data analysis, statistical visualization, and machine learning performance visualization. The document includes practical examples of plotting data using Matplotlib, including line plots, scatter plots, and customization options.

Uploaded by

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

Matplotlib Data Visualization Notebook

Matplotlib is a popular Python library for creating static, animated, and interactive visualizations with a simple and customizable interface. It supports various plot types and is commonly used for exploratory data analysis, statistical visualization, and machine learning performance visualization. The document includes practical examples of plotting data using Matplotlib, including line plots, scatter plots, and customization options.

Uploaded by

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

2/12/25, 3:38 PM 15-Matplotlib

📊 Introduction to Matplotlib
Matplotlib is a widely used data visualization library in Python that enables the creation
of static, animated, and interactive visualizations. It provides an easy-to-use interface
for plotting data and is highly customizable.

🔹 Why Use Matplotlib?


Simple and intuitive syntax.
Highly customizable for professional-quality plots.
Supports multiple plot types (line, bar, scatter, histogram, etc.).
Works seamlessly with NumPy and Pandas.
Enables interactive and animated visualizations.

🔹 Common Use Cases


✔ Exploratory Data Analysis (EDA)
✔ Statistical Data Visualization
✔ Machine Learning Model Performance Visualization
✔ Business and Financial Data Presentation
📌 Let's dive into Matplotlib with hands-on practice! 🚀

Types of Data
Numerical Data
Categorical Data

In [78]: # import the library


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('default')

2D Line plot

file:///C:/Users/goura/Downloads/15-Matplotlib.html 1/77
2/12/25, 3:38 PM 15-Matplotlib

Bivariate Analysis
categorical -> numerical and numerical -> numerical
Use case - Time series data

In [79]: # plotting a simple function


price = [48000,54000,57000,49000,47000,45000]
year = [2015,2016,2017,2018,2019,2020]

plt.plot(year,price)

Out[79]: [<matplotlib.lines.Line2D at 0x1a0f3125c10>]

file:///C:/Users/goura/Downloads/15-Matplotlib.html 2/77
2/12/25, 3:38 PM 15-Matplotlib

In [80]: # from a pandas dataframe


batsman = pd.read_csv('sharma-kohli.csv')
batsman

plt.plot(batsman['index'],batsman['V Kohli'])

Out[80]: [<matplotlib.lines.Line2D at 0x1a0f3010b10>]

In [81]: # plotting multiple plots


plt.plot(batsman['index'],batsman['V Kohli'])
plt.plot(batsman['index'],batsman['RG Sharma'])

Out[81]: [<matplotlib.lines.Line2D at 0x1a0f327f7d0>]

file:///C:/Users/goura/Downloads/15-Matplotlib.html 3/77
2/12/25, 3:38 PM 15-Matplotlib

In [82]: # labels title


plt.plot(batsman['index'],batsman['V Kohli'])
plt.plot(batsman['index'],batsman['RG Sharma'])

plt.title('Rohit Sharma Vs Virat Kohli Career Comparison')


plt.xlabel('Season')
plt.ylabel('Runs Scored')

Out[82]: Text(0, 0.5, 'Runs Scored')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 4/77
2/12/25, 3:38 PM 15-Matplotlib

In [83]: # colors(hex) and line(width and style) and marker(size)


plt.plot(batsman['index'],batsman['V Kohli'],color='#D9F10F')
plt.plot(batsman['index'],batsman['RG Sharma'],color='#FC00D6')

plt.title('Rohit Sharma Vs Virat Kohli Career Comparison')


plt.xlabel('Season')
plt.ylabel('Runs Scored')

Out[83]: Text(0, 0.5, 'Runs Scored')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 5/77
2/12/25, 3:38 PM 15-Matplotlib

In [84]: plt.plot(batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',l


plt.plot(batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdo

plt.title('Rohit Sharma Vs Virat Kohli Career Comparison')


plt.xlabel('Season')
plt.ylabel('Runs Scored')

Out[84]: Text(0, 0.5, 'Runs Scored')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 6/77
2/12/25, 3:38 PM 15-Matplotlib

In [85]: plt.plot(batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',l


plt.plot(batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdo

plt.title('Rohit Sharma Vs Virat Kohli Career Comparison')


plt.xlabel('Season')
plt.ylabel('Runs Scored')

Out[85]: Text(0, 0.5, 'Runs Scored')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 7/77
2/12/25, 3:38 PM 15-Matplotlib

In [86]: # legend -> location


plt.plot(batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',l
plt.plot(batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdo

plt.title('Rohit Sharma Vs Virat Kohli Career Comparison')


plt.xlabel('Season')
plt.ylabel('Runs Scored')

plt.legend(loc='upper right')

Out[86]: <matplotlib.legend.Legend at 0x1a0f3609c90>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 8/77
2/12/25, 3:38 PM 15-Matplotlib

In [87]: # limiting axes


price = [48000,54000,57000,49000,47000,45000,4500000]
year = [2015,2016,2017,2018,2019,2020,2021]

plt.plot(year,price)
plt.ylim(0,75000)
plt.xlim(2017,2019)

Out[87]: (2017.0, 2019.0)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 9/77
2/12/25, 3:38 PM 15-Matplotlib

In [88]: # grid
plt.plot(batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',l
plt.plot(batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdo

plt.title('Rohit Sharma Vs Virat Kohli Career Comparison')


plt.xlabel('Season')
plt.ylabel('Runs Scored')

plt.grid()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 10/77
2/12/25, 3:38 PM 15-Matplotlib

In [89]: # show
plt.plot(batsman['index'],batsman['V Kohli'],color='#D9F10F',linestyle='solid',l
plt.plot(batsman['index'],batsman['RG Sharma'],color='#FC00D6',linestyle='dashdo

plt.title('Rohit Sharma Vs Virat Kohli Career Comparison')


plt.xlabel('Season')
plt.ylabel('Runs Scored')

plt.grid()

plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 11/77
2/12/25, 3:38 PM 15-Matplotlib

Scatter Plots

Bivariate Analysis
numerical vs numerical
Use case - Finding correlation

In [90]: # plt.scatter simple function


x = np.linspace(-10,10,50)

y = 10*x + 3 + np.random.randint(0,300,50)
y

file:///C:/Users/goura/Downloads/15-Matplotlib.html 12/77
2/12/25, 3:38 PM 15-Matplotlib

Out[90]: array([115. , 188.08163265, -40.83673469, -35.75510204,


-36.67346939, 146.40816327, 66.48979592, 5.57142857,
229.65306122, 98.73469388, 8.81632653, 173.89795918,
117.97959184, 226.06122449, 161.14285714, 36.2244898 ,
189.30612245, 182.3877551 , 124.46938776, 19.55102041,
156.63265306, 146.71428571, 97.79591837, 245.87755102,
234.95918367, 214.04081633, 189.12244898, 307.20408163,
17.28571429, 233.36734694, 203.44897959, 56.53061224,
90.6122449 , 194.69387755, 231.7755102 , 240.85714286,
68.93877551, 121.02040816, 349.10204082, 115.18367347,
147.26530612, 358.34693878, 168.42857143, 228.51020408,
380.59183673, 296.67346939, 144.75510204, 232.83673469,
350.91836735, 313. ])

In [91]: plt.scatter(x,y)

Out[91]: <matplotlib.collections.PathCollection at 0x1a0f366d750>

In [92]: # plt.scatter on pandas data


df = pd.read_csv('batter.csv')
df = df.head(50)
df

file:///C:/Users/goura/Downloads/15-Matplotlib.html 13/77
2/12/25, 3:38 PM 15-Matplotlib

Out[92]: 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

5 AB de Villiers 5181 39.853846 148.580442

6 CH Gayle 4997 39.658730 142.121729

7 MS Dhoni 4978 39.196850 130.931089

8 RV Uthappa 4954 27.522222 126.152279

9 KD Karthik 4377 26.852761 129.267572

10 G Gambhir 4217 31.007353 119.665153

11 AT Rayudu 4190 28.896552 124.148148

12 AM Rahane 4074 30.863636 117.575758

13 KL Rahul 3895 46.927711 132.799182

14 SR Watson 3880 30.793651 134.163209

15 MK Pandey 3657 29.731707 117.739858

16 SV Samson 3526 29.140496 132.407060

17 KA Pollard 3437 28.404959 140.457703

18 F du Plessis 3403 34.373737 127.167414

19 YK Pathan 3222 29.290909 138.046272

20 BB McCullum 2882 27.711538 126.848592

21 RR Pant 2851 34.768293 142.550000

22 PA Patel 2848 22.603175 116.625717

23 JC Buttler 2832 39.333333 144.859335

24 SS Iyer 2780 31.235955 121.132898

25 Q de Kock 2767 31.804598 130.951254

26 Yuvraj Singh 2754 24.810811 124.784776

27 V Sehwag 2728 27.555556 148.827059

28 SA Yadav 2644 29.707865 134.009123

29 M Vijay 2619 25.930693 118.614130

30 RA Jadeja 2502 26.617021 122.108346

31 SPD Smith 2495 34.652778 124.812406

32 SE Marsh 2489 39.507937 130.109775

file:///C:/Users/goura/Downloads/15-Matplotlib.html 14/77
2/12/25, 3:38 PM 15-Matplotlib

batter runs avg strike_rate

33 DA Miller 2455 36.102941 133.569097

34 JH Kallis 2427 28.552941 105.936272

35 WP Saha 2427 25.281250 124.397745

36 DR Smith 2385 28.392857 132.279534

37 MA Agarwal 2335 22.669903 129.506378

38 SR Tendulkar 2334 33.826087 114.187867

39 GJ Maxwell 2320 25.494505 147.676639

40 N Rana 2181 27.961538 130.053667

41 R Dravid 2174 28.233766 113.347237

42 KS Williamson 2105 36.293103 123.315759

43 AJ Finch 2092 24.904762 123.349057

44 AC Gilchrist 2069 27.223684 133.054662

45 AD Russell 2039 29.985294 168.234323

46 JP Duminy 2029 39.784314 120.773810

47 MEK Hussey 1977 38.764706 119.963592

48 HH Pandya 1972 29.878788 140.256046

49 Shubman Gill 1900 32.203390 122.186495

In [93]: plt.scatter(df['avg'],df['strike_rate'],color='red',marker='+')
plt.title('Avg and SR analysis of Top 50 Batsman')
plt.xlabel('Average')
plt.ylabel('SR')

Out[93]: Text(0, 0.5, 'SR')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 15/77
2/12/25, 3:38 PM 15-Matplotlib

In [94]: # size
tips = sns.load_dataset('tips')

# slower
plt.scatter(tips['total_bill'],tips['tip'],s=tips['size']*20)

Out[94]: <matplotlib.collections.PathCollection at 0x1a0f32ee910>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 16/77
2/12/25, 3:38 PM 15-Matplotlib

In [95]: # scatterplot using plt.plot


# faster
plt.plot(tips['total_bill'],tips['tip'],'o')

Out[95]: [<matplotlib.lines.Line2D at 0x1a0f3d65850>]

Bar chart

file:///C:/Users/goura/Downloads/15-Matplotlib.html 17/77
2/12/25, 3:38 PM 15-Matplotlib

Bivariate Analysis
Numerical vs Categorical
Use case - Aggregate analysis of groups

In [96]: # simple bar chart


children = [10,20,40,10,30]
colors = ['red','blue','green','yellow','pink']

plt.bar(colors,children,color='black')

Out[96]: <BarContainer object of 5 artists>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 18/77
2/12/25, 3:38 PM 15-Matplotlib

In [97]: # horizontal bar chart


plt.barh(colors,children,color='black')

Out[97]: <BarContainer object of 5 artists>

In [98]: # color and label


df = pd.read_csv('batsman_season_record.csv')
df

file:///C:/Users/goura/Downloads/15-Matplotlib.html 19/77
2/12/25, 3:38 PM 15-Matplotlib

Out[98]: 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

In [99]: plt.bar(np.arange(df.shape[0]) - 0.2,df['2015'],width=0.2,color='yellow')


plt.bar(np.arange(df.shape[0]),df['2016'],width=0.2,color='red')
plt.bar(np.arange(df.shape[0]) + 0.2,df['2017'],width=0.2,color='blue')

plt.xticks(np.arange(df.shape[0]), df['batsman'])

plt.show()

In [100… np.arange(df.shape[0])

Out[100… array([0, 1, 2, 3, 4])

In [101… # a problem
children = [10,20,40,10,30]
colors = ['red red red red red red','blue blue blue blue','green green green gre

plt.bar(colors,children,color='black')
plt.xticks(rotation='vertical')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 20/77
2/12/25, 3:38 PM 15-Matplotlib

Out[101… ([0, 1, 2, 3, 4],


[Text(0, 0, 'red red red red red red'),
Text(1, 0, 'blue blue blue blue'),
Text(2, 0, 'green green green green green'),
Text(3, 0, 'yellow yellow yellow yellow '),
Text(4, 0, 'pink pinkpinkpink')])

In [102… # Stacked Bar chart


plt.bar(df['batsman'],df['2017'],label='2017')
plt.bar(df['batsman'],df['2016'],bottom=df['2017'],label='2016')
plt.bar(df['batsman'],df['2015'],bottom=(df['2016'] + df['2017']),label='2015')

plt.legend()
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 21/77
2/12/25, 3:38 PM 15-Matplotlib

Histogram

Univariate Analysis
Numerical col
Use case - Frequency Count

In [103… # simple data

data = [32,45,56,10,15,27,61]

plt.hist(data,bins=[10,25,40,55,70])

file:///C:/Users/goura/Downloads/15-Matplotlib.html 22/77
2/12/25, 3:38 PM 15-Matplotlib

Out[103… (array([2., 2., 1., 2.]),


array([10., 25., 40., 55., 70.]),
<BarContainer object of 4 artists>)

In [104… # on some data


df = pd.read_csv('vk.csv')
df

Out[104… match_id batsman_runs

0 12 62

1 17 28

2 20 64

3 27 0

4 30 10

... ... ...

136 624 75

137 626 113

138 632 54

139 633 0

140 636 54

141 rows × 2 columns

In [105… plt.hist(df['batsman_runs'],bins=[0,10,20,30,40,50,60,70,80,90,100,110,120])
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 23/77
2/12/25, 3:38 PM 15-Matplotlib

In [106… # logarithmic scale


arr = np.load('big-array.npy')
plt.hist(arr,bins=[10,20,30,40,50,60,70],log=True)
plt.show()

Pie Chart

file:///C:/Users/goura/Downloads/15-Matplotlib.html 24/77
2/12/25, 3:38 PM 15-Matplotlib

Univariate/Bivariate Analysis
Categorical vs numerical
Use case - To find contibution on a standard scale

In [107… # simple data


data = [23,45,100,20,49]
subjects = ['eng','science','maths','sst','hindi']
plt.pie(data,labels=subjects)

plt.show()

In [108… # dataset
df = pd.read_csv('gayle-175.csv')
df

file:///C:/Users/goura/Downloads/15-Matplotlib.html 25/77
2/12/25, 3:38 PM 15-Matplotlib

Out[108… batsman batsman_runs

0 AB de Villiers 31

1 CH Gayle 175

2 R Rampaul 0

3 SS Tiwary 2

4 TM Dilshan 33

5 V Kohli 11

In [109… plt.pie(df['batsman_runs'],labels=df['batsman'],autopct='%0.1f%%')
plt.show()

In [110… # percentage and colors


plt.pie(df['batsman_runs'],labels=df['batsman'],autopct='%0.1f%%',colors=['blue'
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 26/77
2/12/25, 3:38 PM 15-Matplotlib

In [111… # explode shadow


plt.pie(df['batsman_runs'],labels=df['batsman'],autopct='%0.1f%%',explode=[0.3,0
plt.show()

Changing styles
In [112… plt.style.available

file:///C:/Users/goura/Downloads/15-Matplotlib.html 27/77
2/12/25, 3:38 PM 15-Matplotlib

Out[112… ['Solarize_Light2',
'_classic_test_patch',
'_mpl-gallery',
'_mpl-gallery-nogrid',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn-v0_8',
'seaborn-v0_8-bright',
'seaborn-v0_8-colorblind',
'seaborn-v0_8-dark',
'seaborn-v0_8-dark-palette',
'seaborn-v0_8-darkgrid',
'seaborn-v0_8-deep',
'seaborn-v0_8-muted',
'seaborn-v0_8-notebook',
'seaborn-v0_8-paper',
'seaborn-v0_8-pastel',
'seaborn-v0_8-poster',
'seaborn-v0_8-talk',
'seaborn-v0_8-ticks',
'seaborn-v0_8-white',
'seaborn-v0_8-whitegrid',
'tableau-colorblind10']

In [113… plt.style.use('dark_background')

In [114… arr = np.load('big-array.npy')


plt.hist(arr,bins=[10,20,30,40,50,60,70],log=True)
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 28/77
2/12/25, 3:38 PM 15-Matplotlib

Save figure
In [115… arr = np.load('big-array.npy')
plt.hist(arr,bins=[10,20,30,40,50,60,70],log=True)

plt.savefig('sample.png')

In [116… import numpy as np


import pandas as pd

import matplotlib.pyplot as plt

plt.style.use('default')

In [117… # code here


df = pd.read_csv('https://tinyurl.com/2fe6vz4u')
df.head()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 29/77
2/12/25, 3:38 PM 15-Matplotlib

Out[117…
Unnamed: Temporal Tempora
Position Country City/Town Year PM2.5 PM10
0 coverage coverage.1

0 0 1 India Kanpur 2016 173 >75% 319 NaN

1 1 2 India Faridabad 2016 172 >75% 316 NaN

50% -<
2 2 3 India Gaya 2016 149 275 NaN
75%

3 3 4 India Varanasi 2016 146 >75% 260 NaN

4 4 5 India Patna 2016 144 >75% 266 NaN

In [118… iran_series = df.query('Country == "Iran"').groupby('Year')['PM2.5'].sum()


china_series = df.query('Country == "China"').groupby('Year')['PM2.5'].sum()

In [119… plt.plot(iran_series.index,iran_series.values,label='Iran',linestyle='dashed')
plt.plot(china_series.index,china_series.values,label='China',
linestyle='dotted',color='red')
plt.xlabel('Year')
plt.ylabel('PM2.5')
plt.title('PM2.5 Over the years')
plt.xticks(df['Year'].value_counts().index)
plt.legend()
plt.grid()
plt.show()

In [120… # code here


plt.hist(df['PM10'],density=True,bins=50,facecolor='green',alpha=0.6)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 30/77
2/12/25, 3:38 PM 15-Matplotlib

plt.xlabel('Bins')
plt.ylabel('Probability')
plt.title('Histogram of PM10')
plt.grid()
plt.show()

In [121… # code here


chile_df = df.query("Country == 'Chile'")
poland_df = df.query("Country == 'Poland'")

In [122… plt.scatter(chile_df['PM2.5'],chile_df['PM10'],marker="+",color='red',
label='Chile')
plt.scatter(poland_df['PM2.5'],poland_df['PM10'],marker="D",color='black',
label='Poland')
plt.xlabel('PM2.5')
plt.ylabel('PM10')
plt.title('PM2.5 Vs PM10 for Chile and Poland')
plt.legend()
plt.grid()
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 31/77
2/12/25, 3:38 PM 15-Matplotlib

In [123… # code here


freq_ser = df['Country'].value_counts().head()

In [124… plt.pie(freq_ser,labels=freq_ser.index,autopct='%0.1f%%')
plt.show()

In [125… # code here


plt.bar(freq_ser.index,freq_ser)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 32/77
2/12/25, 3:38 PM 15-Matplotlib

plt.xlabel('Country')
plt.ylabel('Frequency Count')
plt.show()

In [126… # code here


df = pd.read_csv('https://docs.google.com/spreadsheets/d/e/2PACX-1vTJh6X4_mqixWs
df

Out[126… month_number facecream facewash toothpaste bathingsoap shampoo moisturiz

0 1 2500 1500 5200 9200 1200 15

1 2 2630 1200 5100 6100 2100 12

2 3 2140 1340 4550 9550 3550 13

3 4 3400 1130 5870 8870 1870 11

4 5 3600 1740 4560 7760 1560 17

5 6 2760 1555 4890 7490 1890 15

6 7 2980 1120 4780 8980 1780 11

7 8 3700 1400 5860 9960 2860 14

8 9 3540 1780 6100 8100 2100 17

9 10 1990 1890 8300 10300 2300 18

10 11 2340 2100 7300 13300 2400 21

11 12 2900 1760 7400 14400 1800 17

file:///C:/Users/goura/Downloads/15-Matplotlib.html 33/77
2/12/25, 3:38 PM 15-Matplotlib

In [127… plt.plot(df['month_number'],df['total_profit'],label='Month on month Profit',


color='b',marker='o',linestyle='dotted')
plt.xlabel('Month')
plt.ylabel('Total Profit')
plt.title('Company sales profit')
plt.legend(loc="upper left")
plt.show()

In [128… # code here


labels = df[df['month_number'] == 3].iloc[:,1:7].stack().index.get_level_values(
values = df[df['month_number'] == 3].iloc[:,1:7].stack().values

In [129… plt.pie(values,labels=labels,autopct='%0.1f%%',explode=[0,0,0.1,0,0,0],
shadow=True)
plt.title("Sales in March")
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 34/77
2/12/25, 3:38 PM 15-Matplotlib

In [130… # code here


monthList = df['month_number'].tolist()

plt.plot(monthList, df['facecream'], label = 'Face cream', linestyle='dotted',

marker='o', linewidth=3)
plt.plot(monthList, df['toothpaste'], label = 'Tooth Paste', marker='o',
linewidth=3)
plt.plot(monthList, df['bathingsoap'], label = 'Bathing Soap', marker='o',
linewidth=3)
plt.plot(monthList, df['shampoo'], label = 'Shampoo', linestyle='dashdot',
linewidth=3)
plt.plot(monthList, df['moisturizer'], label = 'Moisturizer', marker='o',
linewidth=3)
plt.plot(monthList, df['facewash'], label = 'Face Wash', linestyle='dashed',
linewidth=2)

plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
plt.legend(loc='upper right')
plt.xticks(monthList)
plt.title('Sales data')
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 35/77
2/12/25, 3:38 PM 15-Matplotlib

In [131… # code here


df['date'] = pd.to_datetime(['2020-{}-01'.format(month) for month in df[
'month_number']])

In [132… final_df = df.groupby(df['date'].dt.quarter).sum(numeric_only=True)

In [133… i = -1
for col in final_df.columns[1:7]:
plt.bar(final_df.index + i,final_df[col],width=0.15,label=col)
i = i - 0.15

plt.xticks(final_df.index-1.4,final_df.index)
plt.xlabel('Product')
plt.ylabel('Sales')
plt.legend()
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 36/77
2/12/25, 3:38 PM 15-Matplotlib

In [134… all_cols = []

for col in final_df.columns[1:7]:


if len(all_cols) == 0:
plt.bar(final_df.index,final_df[col],label=col)
else:
plt.bar(final_df.index,final_df[col],bottom=sum(all_cols),label=col)
all_cols.append(final_df[col])

plt.xticks(final_df.index - 0.02, final_df.index)


plt.legend()
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 37/77
2/12/25, 3:38 PM 15-Matplotlib

Advance Matplotlib
In [135… import warnings
warnings.filterwarnings('ignore')

In [136… iris = pd.read_csv("iris.csv")


iris.head()

Out[136… 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 [137… iris['Species'] = iris['Species'].replace({'Iris-setosa':0,'Iris-versicolor':1,'


iris.sample(5)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 38/77
2/12/25, 3:38 PM 15-Matplotlib

Out[137… Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

75 76 6.6 3.0 4.4 1.4 1

35 36 5.0 3.2 1.2 0.2 0

106 107 4.9 2.5 4.5 1.7 2

115 116 6.4 3.2 5.3 2.3 2

138 139 6.0 3.0 4.8 1.8 2

In [138… plt.scatter('SepalLengthCm','PetalLengthCm',data = iris,c = 'Species',cmap = 'je


plt.xlabel('SepalLengthCm') # matplotlib cmap search on google for variety
plt.ylabel('PetalLengthCm')
plt.colorbar()

Out[138… <matplotlib.colorbar.Colorbar at 0x1a0f6059350>

Plot Size
In [139… plt.figure(figsize = (5,3))

plt.scatter('SepalLengthCm','PetalLengthCm',data = iris,c = 'Species',cmap = 'je


plt.xlabel('SepalLengthCm') # matplotlib cmap search on google for variety
plt.ylabel('PetalLengthCm')
plt.colorbar()

Out[139… <matplotlib.colorbar.Colorbar at 0x1a0f5d87b50>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 39/77
2/12/25, 3:38 PM 15-Matplotlib

Annotation
In [140… batters = pd.read_csv("batter.csv")

In [141… sample_df = batters.head(100).sample(25)


sample_df

file:///C:/Users/goura/Downloads/15-Matplotlib.html 40/77
2/12/25, 3:38 PM 15-Matplotlib

Out[141… batter runs avg strike_rate

92 STR Binny 880 19.130435 125.714286

74 IK Pathan 1150 21.698113 116.751269

46 JP Duminy 2029 39.784314 120.773810

95 MS Bisla 798 21.000000 108.276798

22 PA Patel 2848 22.603175 116.625717

30 RA Jadeja 2502 26.617021 122.108346

56 PP Shaw 1588 25.206349 143.580470

35 WP Saha 2427 25.281250 124.397745

66 KH Pandya 1326 22.100000 132.203390

38 SR Tendulkar 2334 33.826087 114.187867

55 KC Sangakkara 1687 25.953846 118.469101

53 MK Tiwary 1695 28.728814 113.834788

37 MA Agarwal 2335 22.669903 129.506378

2 DA Warner 5883 41.429577 136.401577

11 AT Rayudu 4190 28.896552 124.148148

93 Harbhajan Singh 833 15.145455 130.974843

80 SP Narine 1025 14.855072 154.367470

76 ML Hayden 1107 35.709677 131.942789

31 SPD Smith 2495 34.652778 124.812406

94 SO Hetmyer 831 30.777778 144.020797

49 Shubman Gill 1900 32.203390 122.186495

4 SK Raina 5536 32.374269 132.535312

90 MM Ali 910 23.333333 139.570552

14 SR Watson 3880 30.793651 134.163209

86 A Symonds 974 36.074074 124.711908

In [142… plt.figure(figsize = (18,10))

plt.scatter('avg','strike_rate', data = sample_df, s = 'runs')

for i in range (sample_df.shape[0]):


plt.text(sample_df['avg'].values[i],sample_df['strike_rate'].values[i],sampl

file:///C:/Users/goura/Downloads/15-Matplotlib.html 41/77
2/12/25, 3:38 PM 15-Matplotlib

In [143… x = [1,2,3,4]
y = [5,6,7,8]

plt.scatter(x,y)
plt.text(1,5,'Point 1')
plt.text(2,6,'Point 2')
plt.text(3,7,'Point 3')
plt.text(4,8,'Point 4',fontdict={'size':12,'color':'brown'})

Out[143… Text(4, 8, 'Point 4')

Horizontal and Vertical lines

file:///C:/Users/goura/Downloads/15-Matplotlib.html 42/77
2/12/25, 3:38 PM 15-Matplotlib

In [144… plt.figure(figsize = (18,10))

plt.scatter('avg','strike_rate', data = sample_df, s = 'runs')

plt.axhline(110,color='red')
plt.axhline(130,color='green')
plt.axvline(30,color='red')

for i in range (sample_df.shape[0]):


plt.text(sample_df['avg'].values[i],sample_df['strike_rate'].values[i],sampl

Subplots
In [146… fig,ax = plt.subplots()

ax.plot('avg','strike_rate',data = batters)
ax.set_title('something')
ax.set_xlabel('Avg')
ax.set_ylabel('Strike rate')
fig.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 43/77
2/12/25, 3:38 PM 15-Matplotlib

In [147… fig,ax = plt.subplots(nrows = 2, ncols = 1,sharex = True,figsize = (10,6))


ax[0].scatter(batters['avg'],batters['strike_rate'],color = 'cyan')
ax[1].scatter(batters['avg'],batters['runs'],color = 'green')

ax[0].set_title('Avg Vs Strike Rate')


ax[0].set_ylabel('Strike Rate')

ax[1].set_title('Avg Vs Runs')
ax[1].set_ylabel('Runs')
ax[1].set_xlabel('Avg')

Out[147… Text(0.5, 0, 'Avg')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 44/77
2/12/25, 3:38 PM 15-Matplotlib

In [148… fig,ax = plt.subplots(nrows = 2, ncols = 2) #,sharex = True,figsize = (10,6))

ax[0,0].scatter(batters['avg'],batters['strike_rate'],c= 'red')
ax[0,1].scatter(batters['avg'],batters['runs'],c = 'green')
ax[1,0].hist(batters['avg'])
ax[1,1].hist(batters['runs'])

Out[148… (array([499., 40., 19., 19., 9., 6., 4., 4., 3., 2.]),
array([ 0. , 663.4, 1326.8, 1990.2, 2653.6, 3317. , 3980.4, 4643.8,
5307.2, 5970.6, 6634. ]),
<BarContainer object of 10 artists>)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 45/77
2/12/25, 3:38 PM 15-Matplotlib

In [149… fig = plt.figure()

ax1 = fig.add_subplot(2,2,1)
ax1.scatter(batters['avg'],batters['strike_rate'],color='red')

ax2 = fig.add_subplot(2,2,2)
ax2.hist(batters['runs'])

ax3 = fig.add_subplot(2,2,3)
ax3.hist(batters['avg'])

Out[149… (array([102., 125., 103., 82., 78., 43., 22., 14., 2., 1.]),
array([ 0. , 5.56666667, 11.13333333, 16.7 , 22.26666667,
27.83333333, 33.4 , 38.96666667, 44.53333333, 50.1 ,
55.66666667]),
<BarContainer object of 10 artists>)

In [150… fig, ax = plt.subplots(nrows=2,ncols=2,sharex=True,figsize=(6,3))

ax[1,1]

Out[150… <Axes: >

file:///C:/Users/goura/Downloads/15-Matplotlib.html 46/77
2/12/25, 3:38 PM 15-Matplotlib

3D Scatter Plots
In [151… batters

fig = plt.figure()

ax = plt.subplot(projection='3d')

ax.scatter3D(batters['runs'],batters['avg'],batters['strike_rate'],marker='+')
ax.set_title('IPL batsman analysis')

ax.set_xlabel('Runs')
ax.set_ylabel('Avg')
ax.set_zlabel('SR')

Out[151… Text(0.5, 0, 'SR')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 47/77
2/12/25, 3:38 PM 15-Matplotlib

3D Line Plot
In [152… x = [0,1,5,25]
y = [0,10,13,0]
z = [0,13,20,9]

fig = plt.figure()

ax = plt.subplot(projection='3d')

ax.scatter3D(x,y,z,s=[100,100,100,100])
ax.plot3D(x,y,z,color='red')

Out[152… [<mpl_toolkits.mplot3d.art3d.Line3D at 0x1a0f5ab3a10>]

3D Surface Plots
In [5]: x = np.linspace(-10,10,100)
y = np.linspace(-10,10,100)

xx, yy = np.meshgrid(x,y)

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

fig = plt.figure(figsize=(12,8))

ax = plt.subplot(projection='3d')

p = ax.plot_surface(xx,yy,z,cmap='viridis')
fig.colorbar(p)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 48/77
2/12/25, 3:38 PM 15-Matplotlib

Out[5]: <matplotlib.colorbar.Colorbar at 0x2226781f790>

In [6]: z = np.sin(xx) + np.cos(yy)

fig = plt.figure(figsize=(12,8))

ax = plt.subplot(projection='3d')

p = ax.plot_surface(xx,yy,z,cmap='viridis')
fig.colorbar(p)

Out[6]: <matplotlib.colorbar.Colorbar at 0x222672ee850>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 49/77
2/12/25, 3:38 PM 15-Matplotlib

In [11]: import numpy as np


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnnings.filterwarnings("ignore")

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 6
4 import matplotlib.pyplot as plt
5 import warnings
----> 6 warnnings.filterwarnings("ignore")

NameError: name 'warnnings' is not defined

In [12]: z = np.sin(xx) + np.log(xx)

fig = plt.figure(figsize=(12,8))

ax = plt.subplot(projection='3d')

p = ax.plot_surface(xx,yy,z,cmap='viridis')
fig.colorbar(p)

C:\Users\goura\AppData\Local\Temp\ipykernel_200\684431574.py:1: RuntimeWarning: i
nvalid value encountered in log
z = np.sin(xx) + np.log(xx)
Out[12]: <matplotlib.colorbar.Colorbar at 0x22267c08b50>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 50/77
2/12/25, 3:38 PM 15-Matplotlib

In [13]: fig = plt.figure(figsize=(12,8))

ax = plt.subplot(projection='3d')

p = ax.plot_surface(xx,yy,z,cmap='viridis')
fig.colorbar(p)

Out[13]: <matplotlib.colorbar.Colorbar at 0x22269f58b50>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 51/77
2/12/25, 3:38 PM 15-Matplotlib

Contour Plots
In [17]: # 3D Surface Plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(projection='3d') # Corrected way to add 3D subplot
p = ax.plot_surface(xx, yy, z, cmap='viridis')
fig.colorbar(p)

Out[17]: <matplotlib.colorbar.Colorbar at 0x2226b6d0850>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 52/77
2/12/25, 3:38 PM 15-Matplotlib

In [19]: # Contour Plot


fig, ax = plt.subplots(figsize=(12, 8)) # Corrected subplot creation
p = ax.contourf(xx, yy, z, cmap='viridis')
fig.colorbar(p)

plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 53/77
2/12/25, 3:38 PM 15-Matplotlib

In [20]: z = np.sin(xx) + np.cos(yy)

fig = plt.figure(figsize=(12,8))

ax = plt.subplot()

p = ax.contourf(xx,yy,z,cmap='viridis')
fig.colorbar(p)

Out[20]: <matplotlib.colorbar.Colorbar at 0x2226bc37310>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 54/77
2/12/25, 3:38 PM 15-Matplotlib

Heatmap
In [23]: delivery = pd.read_csv("ball_deliveries.csv")
delivery.head()

Out[23]: match_id inning batting_team bowling_team over ball batsman non_striker bo

Royal
Sunrisers DA
0 1 1 Challengers 1 1 S Dhawan
Hyderabad Warner
Bangalore

Royal
Sunrisers DA
1 1 1 Challengers 1 2 S Dhawan
Hyderabad Warner
Bangalore

Royal
Sunrisers DA
2 1 1 Challengers 1 3 S Dhawan
Hyderabad Warner
Bangalore

Royal
Sunrisers DA
3 1 1 Challengers 1 4 S Dhawan
Hyderabad Warner
Bangalore

Royal
Sunrisers DA
4 1 1 Challengers 1 5 S Dhawan
Hyderabad Warner
Bangalore

5 rows × 21 columns

In [24]: temp_df = delivery[(delivery['ball'].isin([1,2,3,4,5,6])) & (delivery['batsman_r

file:///C:/Users/goura/Downloads/15-Matplotlib.html 55/77
2/12/25, 3:38 PM 15-Matplotlib

In [25]: grid = temp_df.pivot_table(index='over',columns='ball',values='batsman_runs',agg

In [26]: plt.figure(figsize=(10,5))
plt.imshow(grid)
plt.yticks(delivery['over'].unique(), list(range(1,21)))
plt.xticks(np.arange(0,6), list(range(1,7)))
plt.colorbar()

Out[26]: <matplotlib.colorbar.Colorbar at 0x2226ff80810>

Pandas Plot()
In [27]: # on a series

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

Out[27]: <Axes: >

file:///C:/Users/goura/Downloads/15-Matplotlib.html 56/77
2/12/25, 3:38 PM 15-Matplotlib

In [28]: # can be used on a dataframe as well

import seaborn as sns


tips = sns.load_dataset('tips')

tips['size'] = tips['size'] * 100

tips.head()

Out[28]: total_bill tip sex smoker day time size

0 16.99 1.01 Female No Sun Dinner 200

1 10.34 1.66 Male No Sun Dinner 300

2 21.01 3.50 Male No Sun Dinner 300

3 23.68 3.31 Male No Sun Dinner 200

4 24.59 3.61 Female No Sun Dinner 400

In [29]: # Scatter plot -> labels -> markers -> figsize -> color -> cmap
tips.plot(kind='scatter',x='total_bill',y='tip',title='Cost Analysis',marker='+'
figsize=(10,6),s='size',c='sex',cmap='viridis')

Out[29]: <Axes: title={'center': 'Cost Analysis'}, xlabel='total_bill', ylabel='tip'>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 57/77
2/12/25, 3:38 PM 15-Matplotlib

In [30]: stocks = pd.read_csv(


'https://raw.githubusercontent.com/m-mehdi/pandas_tutorials/main/weekly_stoc
stocks.head()

Out[30]: 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 [31]: # line plot


stocks['MSFT'].plot(kind='line')

Out[31]: <Axes: >

file:///C:/Users/goura/Downloads/15-Matplotlib.html 58/77
2/12/25, 3:38 PM 15-Matplotlib

In [32]: stocks.plot(kind='line',x='Date')

Out[32]: <Axes: xlabel='Date'>

In [33]: stocks[['Date','AAPL','FB']].plot(kind='line',x='Date')

Out[33]: <Axes: xlabel='Date'>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 59/77
2/12/25, 3:38 PM 15-Matplotlib

In [34]: # bar chart -> single -> horizontal -> multiple


# using tips
temp = pd.read_csv("batsman_season_record.csv")
temp.head()

Out[34]: 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

In [35]: tips.groupby('sex')['total_bill'].mean().plot(kind='bar')

C:\Users\goura\AppData\Local\Temp\ipykernel_200\709106644.py:1: FutureWarning: Th
e default of observed=False is deprecated and will be changed to True in a future
version of pandas. Pass observed=False to retain current behavior or observed=Tru
e to adopt the future default and silence this warning.
tips.groupby('sex')['total_bill'].mean().plot(kind='bar')
Out[35]: <Axes: xlabel='sex'>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 60/77
2/12/25, 3:38 PM 15-Matplotlib

In [36]: temp['2015'].plot(kind='bar')

Out[36]: <Axes: >

In [37]: temp.plot(kind='bar')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 61/77
2/12/25, 3:38 PM 15-Matplotlib

Out[37]: <Axes: >

In [38]: # stacked bar chart


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

Out[38]: <Axes: >

In [39]: # histogram
# using stocks

file:///C:/Users/goura/Downloads/15-Matplotlib.html 62/77
2/12/25, 3:38 PM 15-Matplotlib

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

Out[39]: <Axes: ylabel='Frequency'>

In [40]: # pie -> single and multiple


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

df.head()

Out[40]: 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

In [41]: df['match1'].plot(kind='pie',labels=df['batsman'].values,autopct='%0.1f%%')

Out[41]: <Axes: ylabel='match1'>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 63/77
2/12/25, 3:38 PM 15-Matplotlib

In [42]: # multiple pie charts

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

Out[42]: array([<Axes: ylabel='match1'>, <Axes: ylabel='match2'>,


<Axes: ylabel='match3'>], dtype=object)

In [43]: # multiple separate graphs together


# using stocks

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

Out[43]: array([<Axes: >, <Axes: >, <Axes: >], dtype=object)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 64/77
2/12/25, 3:38 PM 15-Matplotlib

In [44]: # on multiindex dataframes


# using tips

tips.pivot_table(index=['day','time'],columns=['sex','smoker'],values='total_bil
).plot(kind='pie',subplots=True,figsize=(20,10))

C:\Users\goura\AppData\Local\Temp\ipykernel_200\264610415.py:4: FutureWarning: Th
e default value of observed=False is deprecated and will change to observed=True
in a future version of pandas. Specify observed=False to silence this warning and
retain the current behavior
tips.pivot_table(index=['day','time'],columns=['sex','smoker'],values='total_bi
ll',aggfunc='mean'
Out[44]: array([<Axes: ylabel='(Male, Yes)'>, <Axes: ylabel='(Male, No)'>,
<Axes: ylabel='(Female, Yes)'>, <Axes: ylabel='(Female, No)'>],
dtype=object)

In [45]: import numpy as np


import pandas as pd
import matplotlib.pyplot as plt

from sklearn.preprocessing import LabelEncoder

plt.style.use('default')

In [46]: df = pd.read_csv('https://rb.gy/gsmddu')
df.head()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 65/77
2/12/25, 3:38 PM 15-Matplotlib

Out[46]: Total Cap.


Planned Other
Power Monitored Under Forced
Dates Maintanence Reasons
Station Cap.(MW) Maintenace Maintanence(MW)
(MW) (MW)
(MW)

2017-
0 Delhi 2235.4 135.00 0.00 135.0 0
09-01

2017-
1 Haryana 2720.0 2470.00 0.00 2470.0 0
09-01

2017- Himachal
2 3378.0 379.00 0.00 231.0 0
09-01 Pradesh

Jammu
2017-
3 and 1285.0 150.00 0.00 0.0 0
09-01
Kashmir

2017-
4 Punjab 3826.3 2697.65 77.65 2620.0 0
09-01

In [47]: most_freq = df['Power Station'].value_counts().head().index.tolist()

In [48]: temp_df = df[df['Power Station'].isin(most_freq)]

In [49]: encoder = LabelEncoder()


temp_df['Power Station'] = encoder.fit_transform(temp_df['Power Station'])

C:\Users\goura\AppData\Local\Temp\ipykernel_200\1815760082.py:2: SettingWithCopyW
arning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stabl


e/user_guide/indexing.html#returning-a-view-versus-a-copy
temp_df['Power Station'] = encoder.fit_transform(temp_df['Power Station'])

In [50]: fig, ax = plt.subplots(1,2, figsize=(12,5))

ax[0].scatter(temp_df['Monitored Cap.(MW)'],temp_df[
'Total Cap. Under Maintenace (MW)'],c=temp_df['Power Station'])
ax[0].axvline(temp_df['Monitored Cap.(MW)'].mean(),c='red')
ax[0].axhline(temp_df['Total Cap. Under Maintenace (MW)'].mean(),c='black')
ax[0].set_xlabel('Monitored Cap')
ax[0].set_ylabel('Total Cap under Maintaince')
ax[0].set_title('Monitored Vs Under Maintainence Cap')

ax[1].scatter(temp_df['Monitored Cap.(MW)'],temp_df['Actual(MU)'],
c=temp_df['Power Station'])
ax[1].axvline(temp_df['Monitored Cap.(MW)'].mean(),c='red')
ax[1].axhline(temp_df['Actual(MU)'].mean(),c='black')
ax[1].set_xlabel('Monitored Cap')
ax[1].set_ylabel('Actual(MU)')
ax[1].set_title('Monitored Vs Actual(MU)')

Out[50]: Text(0.5, 1.0, 'Monitored Vs Actual(MU)')

file:///C:/Users/goura/Downloads/15-Matplotlib.html 66/77
2/12/25, 3:38 PM 15-Matplotlib

In [51]: fig = plt.figure()

ax = plt.subplot(projection='3d')

ax.scatter3D(temp_df['Monitored Cap.(MW)'],
temp_df['Total Cap. Under Maintenace (MW)'],
temp_df['Forced Maintanence(MW)'],c=temp_df['Power Station'])
ax.set_xlabel('Monitored Cap.(MW)')
ax.set_ylabel('Total Cap. Under Maintenace (MW)')
ax.set_zlabel('Forced Maintanence(MW)')
ax.set_title('3D plot of various metrics')

plt.show()

In [52]: x = np.linspace(-10,10,100)
y = np.linspace(-10,10,100)

xx, yy = np.meshgrid(x,y)

file:///C:/Users/goura/Downloads/15-Matplotlib.html 67/77
2/12/25, 3:38 PM 15-Matplotlib

z = np.abs(xx) - np.abs(yy)

In [53]: fig = plt.figure(figsize=(10,10))

ax = plt.subplot(projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.plot_surface(xx,yy,z, cmap='viridis')
plt.show()

In [54]: # code here


fig = plt.figure(figsize=(10,10))

ax = plt.subplot()
ax.contour(xx,yy,z, cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 68/77
2/12/25, 3:38 PM 15-Matplotlib

In [55]: # code here


fig = plt.figure()

ax = plt.subplot()
ax.contourf(xx,yy,z, cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 69/77
2/12/25, 3:38 PM 15-Matplotlib

In [56]: nifty = pd.read_csv('https://docs.google.com/spreadsheets/d/e/2PACX-1vSbaY6bnKXI


nifty.head()

Out[56]: Prev
Date Symbol Series Open High Low Last Close VWAP Vol
Close

2000-
0 HDFCBANK EQ 157.40 166.00 170.00 166.00 170.00 170.00 169.52 3
01-03

2000-
1 HDFCBANK EQ 170.00 182.00 183.45 171.00 174.00 173.80 174.99 16
01-04

2000-
2 HDFCBANK EQ 173.80 170.00 173.90 165.00 168.00 166.95 169.20 15
01-05

2000-
3 HDFCBANK EQ 166.95 168.00 170.00 165.30 168.95 168.30 168.44 8
01-06

2000-
4 HDFCBANK EQ 168.30 162.15 171.00 162.15 170.75 168.35 166.79 8
01-07

In [57]: # code here


nifty['Date'] = pd.to_datetime(nifty['Date'])

In [58]: nifty.set_index('Date',inplace=True)

In [59]: temp_df = nifty[nifty.index.year == 2020]

In [61]: top_5 = temp_df.groupby('Symbol')['Turnover'].sum().sort_values(


ascending=False).head().index

file:///C:/Users/goura/Downloads/15-Matplotlib.html 70/77
2/12/25, 3:38 PM 15-Matplotlib

In [62]: tempm_df = temp_df[temp_df['Symbol'].isin(top_5)]

In [63]: ax = temp_df[temp_df.Symbol=='RELIANCE'].plot(y='Close', use_index=True)

for company in top_5[1:]:


temp_df[temp_df.Symbol==company].plot(y='Close', label=company,
use_index=True, ax=ax)

In [64]: # code here


temp_df = nifty[nifty.index.year == 2021]
top_5 = temp_df.groupby('Symbol')['Turnover'].sum().sort_values(ascending=False
).head().index
temp_df = temp_df[temp_df['Symbol'].isin(top_5)]

temp_df['Symbol'] = temp_df['Symbol'].astype('category')
temp_df.plot(kind='scatter', x='Close', y='Volume', c='Symbol',
colormap='viridis')

Out[64]: <Axes: xlabel='Close', ylabel='Volume'>

file:///C:/Users/goura/Downloads/15-Matplotlib.html 71/77
2/12/25, 3:38 PM 15-Matplotlib

In [65]: time = np.linspace(0, 10, 100)


x = np.sin(time)
y = np.cos(time)
z = time

# Create a DataFrame from the dataset


data = pd.DataFrame({'time': time, 'x': x, 'y': y, 'z': z})

data

Out[65]: time x y z

0 0.00000 0.000000 1.000000 0.00000

1 0.10101 0.100838 0.994903 0.10101

2 0.20202 0.200649 0.979663 0.20202

3 0.30303 0.298414 0.954437 0.30303

4 0.40404 0.393137 0.919480 0.40404

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

95 9.59596 -0.170347 -0.985384 9.59596

96 9.69697 -0.268843 -0.963184 9.69697

97 9.79798 -0.364599 -0.931165 9.79798

98 9.89899 -0.456637 -0.889653 9.89899

99 10.00000 -0.544021 -0.839072 10.00000

100 rows × 4 columns

file:///C:/Users/goura/Downloads/15-Matplotlib.html 72/77
2/12/25, 3:38 PM 15-Matplotlib

In [66]: fig = plt.figure()

ax = plt.axes(projection='3d')

ax.scatter(data['time'],data['x'],data['y'], c=data['z'])

ax.set_xlabel('time')
ax.set_ylabel('X - Axis')
ax.set_zlabel('Y - Axis')

plt.title('Vibration Characterstics of a car body')


plt.show()

In [67]: # code here


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

xx,yy = np.meshgrid(x,y)

z = np.sin(np.sqrt(xx**2 + yy**2))

In [68]: fig = plt.figure()


ax = plt.subplot(projection='3d')

ax.plot_surface(xx,yy,z,cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 73/77
2/12/25, 3:38 PM 15-Matplotlib

In [69]: fig = plt.figure()


ax = plt.subplot()

ax.contour(xx,yy,z,cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')

plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 74/77
2/12/25, 3:38 PM 15-Matplotlib

In [70]: fig = plt.figure()


ax = plt.subplot()

ax.contourf(xx,yy,z,cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')

plt.show()

In [71]: # code here


# code here
x = np.linspace(-10,10,100)
y = np.linspace(-10,10,100)

xx,yy = np.meshgrid(x,y)

z = np.tan(np.log2(xx**2 + yy**2))

In [72]: fig = plt.figure()


ax = plt.subplot(projection='3d')

ax.plot_surface(xx,yy,z,cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 75/77
2/12/25, 3:38 PM 15-Matplotlib

In [73]: fig = plt.figure()


ax = plt.subplot()

ax.contour(xx,yy,z,cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')

plt.show()

file:///C:/Users/goura/Downloads/15-Matplotlib.html 76/77
2/12/25, 3:38 PM 15-Matplotlib

In [74]: fig = plt.figure()


ax = plt.subplot()

ax.contourf(xx,yy,z,cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')

plt.show()

In [ ]:

file:///C:/Users/goura/Downloads/15-Matplotlib.html 77/77

You might also like