Matplotlib Flashcards

1
Q

Import Matplotlib

A

import matplotlib.pyplot as plt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the definition of a plot

A

It is an illustration that can be represented using a graph

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the definition of a figure

A

It is a diagram or a shape that can be formed by a collection of plots in different dimensions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create a plot on different set of axes - create two figures with each plot 1,1 and 1,2

A

plt.figure(1)
plt.plot([1,1])

plt.figure(2)
plt.plot([1,2])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Make a plot 1,2,1,2 and add a title and axes labels

A

plt.plot([1,2,1,2])
plt.title(‘GRID REPRESENTATION’)
plt.xlabel(‘X-axis’)
plt.ylabel(‘Y-axis’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Add a grid

A

plt.grid()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

readjust plots so nothing is overlapping

A

plt.tight_layout()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create two subplots for a subplot 2,1,1 and a plot 1,4 and 2,2

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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]

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Add a legend

A

plt.legend()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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].

A

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’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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]

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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]

A

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’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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]

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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]

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Make a histogram for the following data: days = [50,80,70,80,40,20,20,20,70,20,60,20,80,50,40,50,20,60,60,60]
bins = [0,10,20,40,50,60,70,80,90,100]

A

days = [50,80,70,80,40,20,20,20,70,20,60,20,80,50,40,50,20,60,60,60]
bins = [0,10,20,40,50,60,70,80,90,100]

plt.hist(days, bins, histtype=’stepfilled’, rwidth=0.88) #histtype can be bar/step
plt.xlabel(‘Days’)
plt.ylabel(‘kilometer count’)
plt.title(‘Team race details Histogram’)

17
Q

Move the legend to the bottom right of the graph

A

plt.legend(‘loc=lower right’)

18
Q

specify that I want a line width of 3 and a line style of - -

A

lw=3,ls=”- -“

19
Q

Select a column from a dataframe, and create a list to input into a plot.

A

Example: wash = df[‘facewash’].to_list()

20
Q

What is the method used to set the locations of the x-axis to correspond to the exact numbers (not showing any inbetween values)?

A

plt.xticks(insert variable here)

e.g.
plt.xticks(months)

21
Q

What is the code that shows the two bars side by side in a bar graph?

A

align = ‘edge’

22
Q

What is the code to add label names?

A
23
Q

How can I specify the bar width for two bars, to be the left and right of the x tick?

A

In the line of code plt.bar(months, facecream, width = -0.4, color = ‘violet’, label=’Facecream’, align=’edge’)
plt.bar(months, facewash, width = 0.4, color = ‘blue’, label=’Facewash’, align=’edge’)

It is width = -0.4 and width = 0.4

24
Q

Make a histogram for all of the series in the dataframe not using MatPlotLib. Use this dataframe: iris_data = pd.read_csv(r”C:\Users\User\Documents\CFG_DATA\data\iris.csv”)

A

iris_data.hist(edgecolor=’black’)