Matplotlib Data Visualization Notebook
Matplotlib Data Visualization Notebook
📊 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.
Types of Data
Numerical Data
Categorical Data
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
plt.plot(year,price)
file:///C:/Users/goura/Downloads/15-Matplotlib.html 2/77
2/12/25, 3:38 PM 15-Matplotlib
plt.plot(batsman['index'],batsman['V Kohli'])
file:///C:/Users/goura/Downloads/15-Matplotlib.html 3/77
2/12/25, 3:38 PM 15-Matplotlib
file:///C:/Users/goura/Downloads/15-Matplotlib.html 4/77
2/12/25, 3:38 PM 15-Matplotlib
file:///C:/Users/goura/Downloads/15-Matplotlib.html 5/77
2/12/25, 3:38 PM 15-Matplotlib
file:///C:/Users/goura/Downloads/15-Matplotlib.html 6/77
2/12/25, 3:38 PM 15-Matplotlib
file:///C:/Users/goura/Downloads/15-Matplotlib.html 7/77
2/12/25, 3:38 PM 15-Matplotlib
plt.legend(loc='upper right')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 8/77
2/12/25, 3:38 PM 15-Matplotlib
plt.plot(year,price)
plt.ylim(0,75000)
plt.xlim(2017,2019)
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.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.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
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
In [91]: plt.scatter(x,y)
file:///C:/Users/goura/Downloads/15-Matplotlib.html 13/77
2/12/25, 3:38 PM 15-Matplotlib
file:///C:/Users/goura/Downloads/15-Matplotlib.html 14/77
2/12/25, 3:38 PM 15-Matplotlib
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')
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)
file:///C:/Users/goura/Downloads/15-Matplotlib.html 16/77
2/12/25, 3:38 PM 15-Matplotlib
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
plt.bar(colors,children,color='black')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 18/77
2/12/25, 3:38 PM 15-Matplotlib
file:///C:/Users/goura/Downloads/15-Matplotlib.html 19/77
2/12/25, 3:38 PM 15-Matplotlib
plt.xticks(np.arange(df.shape[0]), df['batsman'])
plt.show()
In [100… np.arange(df.shape[0])
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
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
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
0 12 62
1 17 28
2 20 64
3 27 0
4 30 10
136 624 75
138 632 54
139 633 0
140 636 54
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
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
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
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()
file:///C:/Users/goura/Downloads/15-Matplotlib.html 26/77
2/12/25, 3:38 PM 15-Matplotlib
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')
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')
plt.style.use('default')
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
50% -<
2 2 3 India Gaya 2016 149 275 NaN
75%
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()
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 [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 [124… plt.pie(freq_ser,labels=freq_ser.index,autopct='%0.1f%%')
plt.show()
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()
file:///C:/Users/goura/Downloads/15-Matplotlib.html 33/77
2/12/25, 3:38 PM 15-Matplotlib
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
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 [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 = []
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')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 38/77
2/12/25, 3:38 PM 15-Matplotlib
Plot Size
In [139… plt.figure(figsize = (5,3))
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")
file:///C:/Users/goura/Downloads/15-Matplotlib.html 40/77
2/12/25, 3:38 PM 15-Matplotlib
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'})
file:///C:/Users/goura/Downloads/15-Matplotlib.html 42/77
2/12/25, 3:38 PM 15-Matplotlib
plt.axhline(110,color='red')
plt.axhline(130,color='green')
plt.axvline(30,color='red')
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
ax[1].set_title('Avg Vs Runs')
ax[1].set_ylabel('Runs')
ax[1].set_xlabel('Avg')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 44/77
2/12/25, 3:38 PM 15-Matplotlib
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
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>)
ax[1,1]
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')
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')
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
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 49/77
2/12/25, 3:38 PM 15-Matplotlib
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 6
4 import matplotlib.pyplot as plt
5 import warnings
----> 6 warnnings.filterwarnings("ignore")
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
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 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)
file:///C:/Users/goura/Downloads/15-Matplotlib.html 52/77
2/12/25, 3:38 PM 15-Matplotlib
plt.show()
file:///C:/Users/goura/Downloads/15-Matplotlib.html 53/77
2/12/25, 3:38 PM 15-Matplotlib
fig = plt.figure(figsize=(12,8))
ax = plt.subplot()
p = ax.contourf(xx,yy,z,cmap='viridis')
fig.colorbar(p)
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()
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
file:///C:/Users/goura/Downloads/15-Matplotlib.html 55/77
2/12/25, 3:38 PM 15-Matplotlib
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()
Pandas Plot()
In [27]: # on a series
s = pd.Series([1,2,3,4,5,6,7])
s.plot(kind='pie')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 56/77
2/12/25, 3:38 PM 15-Matplotlib
tips.head()
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')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 57/77
2/12/25, 3:38 PM 15-Matplotlib
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')
In [33]: stocks[['Date','AAPL','FB']].plot(kind='line',x='Date')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 59/77
2/12/25, 3:38 PM 15-Matplotlib
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')
In [37]: temp.plot(kind='bar')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 61/77
2/12/25, 3:38 PM 15-Matplotlib
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)
df.head()
0 Dhawan 120 0 50
1 Rohit 90 1 24
3 SKY 45 130 45
4 Pandya 12 34 10
In [41]: df['match1'].plot(kind='pie',labels=df['batsman'].values,autopct='%0.1f%%')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 63/77
2/12/25, 3:38 PM 15-Matplotlib
df[['match1','match2','match3']].plot(kind='pie',subplots=True,figsize=(15,8))
stocks.plot(kind='line',subplots=True)
file:///C:/Users/goura/Downloads/15-Matplotlib.html 64/77
2/12/25, 3:38 PM 15-Matplotlib
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)
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
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
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
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)')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 66/77
2/12/25, 3:38 PM 15-Matplotlib
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)
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()
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
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
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 [58]: nifty.set_index('Date',inplace=True)
file:///C:/Users/goura/Downloads/15-Matplotlib.html 70/77
2/12/25, 3:38 PM 15-Matplotlib
temp_df['Symbol'] = temp_df['Symbol'].astype('category')
temp_df.plot(kind='scatter', x='Close', y='Volume', c='Symbol',
colormap='viridis')
file:///C:/Users/goura/Downloads/15-Matplotlib.html 71/77
2/12/25, 3:38 PM 15-Matplotlib
data
Out[65]: time x y z
file:///C:/Users/goura/Downloads/15-Matplotlib.html 72/77
2/12/25, 3:38 PM 15-Matplotlib
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')
xx,yy = np.meshgrid(x,y)
z = np.sin(np.sqrt(xx**2 + yy**2))
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
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
ax.contourf(xx,yy,z,cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()
xx,yy = np.meshgrid(x,y)
z = np.tan(np.log2(xx**2 + yy**2))
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
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
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