Matplotlib Flashcards
Import Matplotlib
import matplotlib.pyplot as plt
What is the definition of a plot
It is an illustration that can be represented using a graph
What is the definition of a figure
It is a diagram or a shape that can be formed by a collection of plots in different dimensions
Create a plot on different set of axes - create two figures with each plot 1,1 and 1,2
plt.figure(1)
plt.plot([1,1])
plt.figure(2)
plt.plot([1,2])
Make a plot 1,2,1,2 and add a title and axes labels
plt.plot([1,2,1,2])
plt.title(‘GRID REPRESENTATION’)
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)
Add a grid
plt.grid()
readjust plots so nothing is overlapping
plt.tight_layout()
Create two subplots for a subplot 2,1,1 and a plot 1,4 and 2,2
plt.subplot(2,1,1) # initialize the subplot 2,1 how many rows(2) and cols(1) and last is which you want to plot on
plt.plot([1,4])
plt.title(“subplot1”)
plt.subplot(2,1,2)
plt.plot([2,2])
plt.title(“subplot2”)
plt.xlabel(“xaxis”)
Plot a line plot for the following data: x = [1,2,3,4,5]
y1 = [50,40,70,80,20]
y2 = [75,30,30,60,70]
y3 = [70,20,60,40,60]
y4 = [80,20,20,50,60]
x = [1,2,3,4,5]
y1 = [50,40,70,80,20]
y2 = [75,30,30,60,70]
y3 = [70,20,60,40,60]
y4 = [80,20,20,50,60]
plt.plot(x,y1,’green’,label=’Team A’, linewidth=3, linestyle=”–”)
plt.plot(x,y2,’blue’,label=’Team B’,linewidth=3, alpha=0.1)
plt.plot(x,y3,’hotpink’,label=’Team C’,linewidth=3)
plt.plot(x,y4,’orange’,label=’Tead D’,linewidth=3)
plt.title(‘Team race details in line plot’)
plt.ylabel(‘ Distance in kms’)
plt.xlabel(‘Days’)
plt.legend()
Add a legend
plt.legend()
Make a bar chart with the following data: Team A is [0.25,1.25,2.25,3.25,4.25],height=[50,40,70,80,20] and team B is [0.25,1.25,2.25,3.25,4.25],height=[80,20,20,50,60].
plt.bar([0.25,1.25,2.25,3.25,4.25],height=[50,40,70,80,20], #yvariable is known as the height variable
# label=’Team A’,width=.5)
plt.bar([0.25,1.25,2.25,3.25,4.25],height=[80,20,20,50,60],
# label=”Team B”, color=’r’,width=.5)
plt.bar([.75,1.75,2.75,3.75,4.75],height=[70,20,60,40,60],
label=”Team C”, color=’y’,width=.5)
plt.bar([.75,1.75,2.75,3.75,4.75],height=[80,20,20,50,60],
label=”Team D”, color=’g’,width=.5)
plt.legend()
plt.xlabel(‘Days’)
plt.ylabel(‘Distance (kms)’)
plt.title(‘Team Race details in BAR PLOTTING’)
Make an area plot for the following data: days = [1,2,3,4,5]
TeamA =[50,40,70,80,20]
TeamB = [80,20,20,50,60]
TeamC =[70,20,60,40,60]
TeamD = [80,20,20,50,60]
days = [1,2,3,4,5]
TeamA =[50,40,70,80,20]
TeamB = [80,20,20,50,60]
TeamC =[70,20,60,40,60]
TeamD = [80,20,20,50,60]
plt.plot([],[],color=’bisque’, label=’TeamA’, linewidth=3)
plt.plot([],[],color=’turquoise’, label=’TeamB’, linewidth=3)
plt.plot([],[],color=’blueviolet’, label=’TeamC’, linewidth=3)
plt.plot([],[],color=’lightcoral’, label=’TeamD’, linewidth=3)
plt.stackplot(days, TeamA, TeamB, TeamC, TeamD, colors=[‘bisque’,’turquoise’,’blueviolet’,’lightcoral’])
plt.xlabel(‘Days’)
plt.ylabel(‘Distance in kms’)
plt.title(‘Team race deatils in area plot’)
plt.legend()
Make a pie plot for the following data: days = [1,2,3,4,5]
TeamA =[50,40,70,80,20]
TeamB = [80,20,20,50,60]
TeamC =[70,20,60,40,60]
TeamD = [80,20,20,50,60]
plotting pie chart
days = [1,2,3,4,5]
TeamA =[50,40,70,80,20]
TeamB = [80,20,20,50,60]
TeamC =[70,20,60,40,60]
TeamD = [80,20,20,50,60]
slices = [8,5,5,6]
labels_list = [‘TeamA’,’TeamB’,’TeamC’,’TeamD’]
color_list = [‘c’,’g’,’y’,’b’]
plt.pie(
slices,
labels=labels_list,
colors=color_list,
startangle=90,
shadow= True,
explode=(0,0.1,0,0),
autopct=’%1.1f%%’
)
plt.title(‘Race details in Pie Plot’)
Make a scatter plot for the following data: days = [1, 2, 3, 4, 5]
Y1 = [50, 40, 70, 80, 20]
Y2=[80, 20, 20, 50, 60]
Y3=[70, 20, 60, 40, 60]
Y4=[80, 20, 20, 50, 60]
days = [1, 2, 3, 4, 5]
Y1 = [50, 40, 70, 80, 20]
Y2=[80, 20, 20, 50, 60]
Y3=[70, 20, 60, 40, 60]
Y4=[80, 20, 20, 50, 60]
plt.scatter(days,Y1, label=’TeamA’,color=’r’)
plt.scatter(days,Y2,label=’TeamB’,color=’b’)
plt.scatter(days,Y3,label=’TeamC’,color=’y’)
plt.scatter(days,Y4,label=’TeamD’,color=’k’)
plt.xlabel(‘Days’)
plt.ylabel(‘Distance in kms’)
plt.title(‘ Race details in Scatter Plot’)
plt.legend()
Make a 3D plot for the following data: x = [1,2,3,4,5]
y1 = [50,40,70,80,20]
y2 = [80,20,20,50,60]
y3 = [70,20,60,40,60]
y4 = [80,20,20,50,60]
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # library for 3D
fig = plt.figure()
ax = fig.add_subplot(111, projection=’3d’)
x = [1,2,3,4,5]
y1 = [50,40,70,80,20]
y2 = [80,20,20,50,60]
y3 = [70,20,60,40,60]
y4 = [80,20,20,50,60]
plt.plot(x,y1,’g’,label=’TeamA’, linewidth=5)
plt.plot(x,y2,’c’,label=’TeamB’,linewidth=5)
plt.plot(x,y3,’k’,label=’TeamC’,linewidth=5)
plt.plot(x,y4,’y’,label=’TeamD’,linewidth=5)
plt.title(‘Race details in 3D plot’)
plt.ylabel(‘ Distance in kms’)
plt.xlabel(‘Days’)
plt.legend()