Matplotlib Flashcards
Import the pyplot module
import matplotlib.pyplot as plt
Allow plots to display in Jupyter Notebook cell
%matplotlib inline
Plot and display the visual
plt. plot()
plt. show()
Create a figure sized 12 wide and 5 high containing 2 rows and 3 columns of subplots
fig, axs = plt.subplots(2, 3, figsize=(12, 5) )
Create a line chart
ax.plot(x, y)
Add a super title to a figure
fig.suptitle(‘Main Title for Figure’)
Create a title for the top right corner axes of a 2x2 grid of subplots
axs[0,1].set_title(‘Subplot Title’)
Create a figure with two vertically stacked subplots that share the same x & y axis range and scale
fig, axes = plt.subplots(2, sharex=True, sharey=True)
Generate a vertical bar plot
ax.bar( bar_positions, bar_heights, width_of_bars)
Set x major tick marks and y minor tick mark locations
ax. set_xticks( locations_list)
ax. setyticks(locations_list, minor=True)
Set x tick labels rotated 90 degrees
Should be only used after ax.set_xticks() to ensure correct position
ax.set_xticklabels( labels_list, rotation=90)
Create a scatter plot
ax.scatter(x, y)
Create a histogram with 20 bins
ax.hist( x, bins = 20)
Create boxplots for columnA and columnB of dataframe
ax.boxplot( df[ [‘columnA’, ‘columnB’].values )
Set a subplot’s y-axis view limit range to 0-50
ax.set_ylim(0,50)