100+ Python Matplotlib commands for EDA | Tips & Tricks

David Gladson
2 min readFeb 9, 2022

These commands help you save a lot of time while working on any kind of plots using Matplotlib

Photo by insung yoon on Unsplash
#shows graphs inline, turn on/off pretty_printing of lists
%matplotlib inline
%pprint
#SCATTER PLOTS
sns.regplot()
sns.regplot(x, y, marker = "+", fit_reg = False)
#Connected Scatter
plt.plot(x, y, linestyle = '-', marker = 'o')
#Area plot
plt.fill_between(x, y, color="skyblue", alpha = 0.3)
plt.plot(x, y)
#Stacked area plot
plt.stackplot(x,y, labels)
#Bubble plot
plt.scatter(x, y, s = z*100, alpha = 0.4)
#HISTOGRAM
sns.distplot()
sns.distplot(col, bins = 20, kde = False)
a, b, c = plt.hist(arr, num_bins)
n, bins, patches = plt.hist(arr, num_bins)
#BARPLOT
plt.bar(x, y, width, color)
#DENSITY PLOTS
sns.kdeplot()
sns.kdeplot(col, shade=True, bw=.05, color="olive")
#1D, 2D
sns.kdeplot(x, y, cmap, shade, shade_lowest)
#BOXPLOTS
sns.boxplot()
sns.boxplot(x, y, hue, data, palette)
#CORRELOGRAM
sns.pairplot(df, kind = "scatter")
sns.pairplot(df, kind = "reg")
sns.pairplot(df[[req_columns]], kind = "reg")
#HEATMAP
sns.heatmap()
sns.heatmap(df.corr(), annot = True)
#MATPLOTLIB OTHER FUNCTIONS
#Title
plt.title("Heading", loc = 'left', fontsize, fontweight, style = 'italic')
plt.title("Heading1\nHeading2")
plt.suptitle("Heading\n")
#X,Y Labels
plt.xlabel('title of xlabels')
plt.xlabel('title of xlabels', fontweight = 'bold', fontsize = 'large')
plt.ylabel('title of ylabels')
#X, Y Ticks
plt.xticks()
#rotate ticks
plt.xticks(rotation=45, ha='right')
plt.tick_params()
#X, Y Limits
plt.xlim()
plt.ylim()
#axis limits
plt.axis([x1, x2, y1, y2])
#Annotate
plt.annotate()
#More margin
plt.subplots_adjust(bottom = 0.4)
plt.subplots_adjust(top = 0.4)
#Figure size
plt.figure(figsize = (12,8))
#grid
plt.grid()
#export plot
plt.savefig('sample.png')
# box plot
# a data point that is located outside the whiskers of the box plot
# (Q1 - 1.5 * IQR or Q3 + 1.5 * IQR)
sns.boxplot(x="variable", y="value", data=pd.melt(df))
#fig subplots size
f, axs = plt.subplots(2,2,figsize=(15,15))
#Color palette
sns.color_palette()
#colors
color = 'tab:blue'
#Subplots
#1 row, 2 cols
plt.subplot(121) #1row, 2cols, 1st fig
plt.plot()
plt.subplot(122) #1row, 2cols, 1st fig
plt.plot()
#2 rows, 1 col
fig, axes = plt.subplot(nrows, ncols, sharex, sharey)
axes[0].plot()
axes[1].plot()
# Notes:
Rotate axis = plt.xticks(rotation = 90)
Remove output = plt();
#add background color
ax.set_facecolor((r, g, b))
#diagonal line
ax.plot([0, 1], [0, 1], transform=ax.transAxes, ls = "--")
#hline, vline (line patterns = '-' '--' '_.' ':')
plt.axhline(y = 10, ls = '-')
plt.axvline(x = 10, ls = '-')
# fill area b/w two lines
ax.axvspan(l1, l2, ymin = 0.1, ymax = 1, alpha = 0.6, color='red')
# text
plt.text(x, y, text)
############# Logarithmic Plots ############
plt.yscale('log',basey=2)
plt.yscale('log')
# STYLES GALLERY IN MATPLOTLIB
https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html
plt.style.use('fivethirtyeight')

Thank you! follow for more such articles :)

--

--