You are on page 1of 3

7.

Subplots
Mutilple plots within a single figure are called Subplots.

import matplotlib.pyplot as plt


import pandas as pd
import numpy as np

plt.style.use('classic') # Selecting a Chart style

# plt.subplots()

# plt.subplots (nrows=2, ncols=2)

fig, ax = plt.subplots (nrows=3, ncols=2) # Unpacking a tuple

# Pie Plot

mob_sales=[60,50, 30, 20,15,40] # Count of mobile units sold in a day


brand= ['Oppo', 'Vivo', 'Samsung', 'Nokia', 'Apple', 'MI'] # Different brands sold in the shop

# Stack Plot

days = [1,2,3,4,5,6,7] # Days of a week


sleeping=[6,7,7,8,6,8,10] # Hours spent on Sleeping
working=[8,8,8,8,8,8,0] # Hours spent on working
Travel=[3,3,3,3,3,3,8] # Hours spent on travelling
eating=[2,3,2,3,2,3,5] # Hours spent on eating and on mobile
label=['sleep', 'work', 'Travel', 'Eat']

# Histogram

X = [41, 35, 58, 45, 81, 56, 73, 63, 20, 0, 38, 22, 42, 31, 15] # Scores of players in a T20 game
bins=[0,10,20,30,40,50,60,70,80,90,100,110,120]

# Verical Bar Chart

df = pd. read_csv('class.csv')
group=df.groupby (['Age'])
df1=group.mean()
df1

width=.40

# Line Chart

Years= [0,1,2,3,4,5] # Years of Experience

Mumbai_Salary=[5,6.4,7.2,8.5,10,13.7] # Salary for Engineers in Mumbai

Bangalore_Salary=[3,4,5.2,6.5,9,11.7] # Salary for Engineers in Bengaluru

fig, ax = plt.subplots (nrows=3, ncols=2, figsize=(10, 10)) # Use figsize=(10, 10) to change the figure size
# Pie Plot

ax[0,0].set_title("Pie Chart of Mobile Sales in a day", fontsize=12)

ax[0,0].pie(mob_sales, colors=['#339900', '#0099FF', '#0000FF', '#6600CC', '#F5F5F5','#ff8000'], \


labels=brand,explode=[0.4,0.1,0.1,0.1,0.4,0.1],startangle=90, autopct='%1.1f%%', shadow=True)

# Stack Plot

ax[0,1].stackplot (days, sleeping, working, Travel, eating, labels=label, colors=['#ff8000', '#00cc00', '#ffff00'

ax[0,1].set_title("StackChart of your Daily Routine", fontsize=12)


ax[0,1].set_xlabel('Days in a Week', fontsize=10)
ax[0,1].set_ylabel('Hours in a days', fontsize=10)

# Histogram

ax[1,0].hist(X,bins, histtype='bar', color='#ffff00',label='Scores') # Use rwidth=.8, to separate the bars

ax[1,0].set_title("Histogram of Player's Score", fontsize=12)


ax[1,0].set_xlabel('Score Bins', fontsize=10)
ax[1,0].set_ylabel('Counts', fontsize=10)

# Vertical Bar Chart

ax[1,1]. bar(df1.index-width,df1['Height'],color='#00cc00',width=.35,label='Mean Height') # Mean Height


ax[1,1].bar(df1.index,df1['Weight' ], color='#ff8000',width=.35, label='Mean Weight') # Mean Weight bar

# ax[1,1].set_xticklabels(df1.index, rotation=90) # Rotate the labels of a Bar Chart by 90 degrees

ax[1,1].set_title("Bar chart of Mean Height and Weight", fontsize=12)


ax[1,1].set_xlabel('Age Group', fontsize=10)
ax[1,1].set_ylabel('Mean', fontsize=10)

for x, y in zip(df1.index,df1['Height']):
ax[1,1].text(x, y, str(int(y)), color='Black', fontweight='bold')
for x, y in zip(df1.index,df1['Weight']):
ax[1,1].text(x, y, str(int(y)), color='Black', fontweight='bold')

# Scatter Plot

ax[2,0].scatter(df["Height"],df["Weight"],color='b',marker='*',label='Height vs Weight',s=100)

ax[2,0].set_title("Scatter Plot of Height Vs Weight", fontsize=12)


ax[2,0].set_xlabel('Height',fontsize=10)
ax[2,0].set_ylabel('Weight',fontsize=10)

# Line Chart

ax[2,1].plot(Years, Mumbai_Salary, color='#ff8000', marker='8', linestyle='--', label='Mumbai Salary')


ax[2,1].plot(Years, Bangalore_Salary, color='#0000e6',marker='s', linestyle='-.',label='Bangalore Salary')

ax[2,1].set_title("Years Vs Salary graph", fontsize=12)


ax[2,1].set_xlabel("Experience (in Years)", fontsize=10)
ax[2,1].set_ylabel("Salary growth (in Lakhs)", fontsize=10)

fig. tight_layout (pad=3) # Using pad= option will add gaps between the images
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like