0% found this document useful (0 votes)
20 views

matplotlib-cheat-sheet

This cheat sheet serves as a quick reference for essential matplotlib plotting functions, covering basic plot types like line, scatter, and bar charts, as well as advanced customization options. It also includes guidance on using pandas for visualizations and seaborn for enhanced graphics. The document aims to help users effectively present data and gain insights through various visualization techniques.

Uploaded by

vivak21iya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

matplotlib-cheat-sheet

This cheat sheet serves as a quick reference for essential matplotlib plotting functions, covering basic plot types like line, scatter, and bar charts, as well as advanced customization options. It also includes guidance on using pandas for visualizations and seaborn for enhanced graphics. The document aims to help users effectively present data and gain insights through various visualization techniques.

Uploaded by

vivak21iya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

matplotlib Cheat Sheet Table of Contents

This cheat sheet provides a quick reference for essential Importing Libraries
Advanced Plot Customization
plotting functions in matplotlib, helping you create and IMPORT
SUBPLOTS, SPINES, TICK_PARAMS, TICK_TOP

customize various types of visualizations. It covers SET_XTICKS, SET_XTICKLABELS, TEXT,


fundamental plot types—from line and scatter plots to Basic Plotting with matplotlib AXVLINE, axhline

histograms and bar charts—and includes advanced PLOT,SCATTER, BAR, BARH, HIST, AREA,
PIE
customization options like subplots, color mapping, and Pandas Visualization
annotations.

LINE (SERIES), LINE (DATAFRAME),


Customization SCATTER (DATAFRAME), HIST (SERIES),
BAR (SERIES), BARH (SERIES), BOXPLOT
You’ll also find guidance on using pandas Series and DataFrame TITLE, XLABEL, YLABEL, LEGEND, GRID,
SET_XTICKS, SET_YTICKS, TICKLABEL_FORMAT,
objects to quickly generate visualizations, perfect for exploring
COLORBAR, ANNOTATE
and understanding your data at a glance. These functions are Seaborn Visualizations
ideal for developing a data cleaning strategy and identifying RELPLOT, HEATMAP, PAIRPLOT,
key trends in your dataset early on.

Grid Charts VIOLINPLOT, JOINTPLOT

figure, SUBPLOT, Subplots


Designed to help you present data clearly and effectively, this
guide ensures you can easily leverage matplotlib’s powerful
features to gain insights and make data-driven decisions.

matplotlib Cheat Sheet Free resources at: dataquest.io/guide


Importing Libraries

Syntax for How to use Explained Syntax for How to use Explained

Import the pyplot


IMPORT import matplotlib.pyplot as plt PLOT plt.plot(x_values, y_values,
Customize line color and
submodule using an alias
color='blue', linestyle='--')
style
Import seaborn and set the plt.show()
import seaborn as sns

default theme
sns.set_theme()
SCATTER plt.scatter(x_values, y_values) Create a scatter plot of points
plt.show()
Import pandas using its
import pandas as pd
common alias
plt.scatter(x_values, y_values,
Customize point color and
Apply the fivethirtyeight size
import matplotlib.style as style c='red', s=100)

predefined style
style.use('fivethirtyeight') plt.show()

BAR plt.bar(x=x_values, height=heights) Create a vertical bar chart

Basic Plotting with matplotlib plt.show()

plt.bar(x, height, bottom=previous_heights) Create a stacked bar chart


Syntax for How to use Explained
plt.show()

PLOT plt.plot(x_values, y_values)


Plot a basic line graph
plt.show() BARH plt.barh(y=y_values, width=widths) Create a horizontal bar chart
plt.show()

plt.plot(x_values1, y_values1) Plot multiple graphs on the plt.barh(y, width, color='purple',


Customize bar colors and
plt.plot(x_values2, y_values2)
same figure edgecolor='black')
borders
plt.show() plt.show()

plt.hist(data_column)

Generate a histogram for a


plt.plot(x_values1, y_values1)
Plot graphs in separate HIST
dataset
plt.show()
figures plt.show()

plt.plot(x_values2, y_values2)

plt.show()

matplotlib Cheat Sheet Free resources at: dataquest.io/guide


Basic Plotting with matplotlib

Syntax for How to use Explained Syntax for How to use Explained

Customize bin count and


HIST plt.hist(data_column, bins=30,
LEGEND plt.plot(x_values1, y_values1,
Add a legend to the plot
color
color='orange')
label='Label 1')

plt.show() plt.plot(x_values2, y_values2,

label='Label 2')

Create an area plot shaded plt.legend()

AREA plt.fill_between(x, y1=lower, y2=upper)

plt.show() w
bet een y1 and y2 plt.show()

PIE plt.pie(sizes, labels=labels,


Create a pie chart with plt.legend(loc='upper right', fontsize=10)
Customize legend position

autopct='%1.1f%%')
percentages plt.show() and font size

plt.show()

GRID plt.grid(True) Add gridlines to the plot

Customization
Customize the tick labels on
SET_XTICKS plt.xticks(ticks=x_values, labels=labels)
the x-axis
Syntax for How to use Explained

plt.xticks(rotation=45) Rotate the x-axis tick labels


TITLE plt.title('Title') Add a title to the plot

Customize the tick labels on


SET_YTICKS plt.yticks(ticks=y_values, labels=labels)
plt.title(' Custom Title', fontsize=16,
Customize title size and color the y-axis
color='green')
plt.yticks(rotation=30) Rotate the y-axis tick labels

XLABEL plt.xlabel('X-axis Label') Add a label to the x-axis


TICKLABEL

plt.ticklabel_format(axis='both',
Change scientific notation to
_FORMAT
style='plain') plain text
YLABEL plt.ylabel('Y-axis Label') Add a label to the y-axis

matplotlib Cheat Sheet Free resources at: dataquest.io/guide


Customization

Syntax for How to use Explained Syntax for How to use Explained

COLORBAR plt.scatter(x, y, c=values,


Use a colormap in the scatter SUBPLOTs fig, axes = plt.subplots(nrows=4, ncols=1) Create a grid of 4 vertically
cmap='viridis')
plot stacked subplots
plt.colorbar()

fig, axes = plt.subplots(nrows=2, ncols=2,


Create a 2x2 grid of subplots
plt.scatter(x, y, c=values,
Use a different colormap figsize=(10, 8))
and assign plots to specific
cmap='coolwarm')
axes[0, 0].plot(x_values1, y_values1)
axes
plt.colorbar() axes[1, 1].plot(x_values2, y_values2)

plt.show()
ANNOTATE plt.annotate(
Add text and an arrow
'Text', xy=(x, y),
annotation on the plot
xytext=(x_offset, y_offset),

arrowprops=dict(facecolor='black',
Advanced Plot Customization
shrink=0.05)

Syntax for How to use Explained


plt.show()

Grid Charts SUBPLOTs fig, ax = plt.subplots(figsize=(10, 8))

ax.bar(x, height)

Create a bar chart using the


object-oriented approach
plt.show()
Syntax for How to use Explained

figure plt.figure(figsize=(8, 3))


Create two subplots in a 1- SPINES for location in ['left', 'right',
Remove all borders (spines)
plt.subplot(1, 2, 1)
row, 2-column grid 'bottom', 'top']:
from the plot
plt.subplot(1, 2, 2)
ax.spines[location].set_visible(False)
plt.show()

SUBPLOT Create a 3-row, 2-column grid TICK_PARAMS ax.tick_params(top=False, left=False)


Hide specific ticks and
plt.figure(figsize=(10, 12))

for i in range(1, 7):


of subplots ax.tick_params(axis='x', colors='grey') change tick colors
plt.subplot(3, 2, i)

plt.plot(x_values, y_values)

plt.show()

matplotlib Cheat Sheet Free resources at: dataquest.io/guide


Advanced Plot Customization
Syntax for How to use Explained Syntax for How to use Explained

TICK_TOP Move x-tick labels to the top


ax.xaxis.tick_top() LINE (SERIES) Series.plot.line(color='green',
Create a line plot from a
of the plot
linestyle='--')
Series object
Set custom tick locations on plt.show()
SET_XTICKS ax.set_xticks([0, 150, 300])
the x-axis
LINE
DataFrame.plot.line(x='column1',
Create a line plot from a
SET_XTICK
ax.set_xticklabels(['0', '150', '300']) Set custom tick labels on the (DATAFRAME) y='column2')
DataFrame object
LABELS x-axis plt.show()

TEXT SCATTER
DataFrame.plot.scatter(x='column1',
Create a scatter plot from a
ax.text(x, y, 'Sample Text', fontsize=12,
Add custom text to a specific
position on the plot
(DATAFRAME) y='column2')
DataFrame object
color='blue')
plt.show()

Add a vertical line at a


AXVLINE ax.axvline(x=5, color='red', linewidth=2) Customize scatter plot points
specified x-position with DataFrame.plot.scatter(x='col1', y='col2',

customization color='red', s=50)

plt.show()
AXHLINE Add a horizontal line at a
ax.axhline(y=3, color='green',

specified y-position with HIST (SERIES) Series.plot.hist(bins=20)

Generate a histogram with


linestyle='--')
customization custom bin count from a
plt.show()
Pandas Series

Pandas Visualization Series.plot.hist(cumulative=True, bins=30)


Create a cumulative
plt.show() histogram

Syntax for How to use Explained Create a vertical bar chart


BAR (SERIES) Series.plot.bar()

from a Pandas Series


LINE (SERIES) Create a line plot from a plt.show()
Series.plot.line()

Series object
plt.show()

matplotlib Cheat Sheet Free resources at: dataquest.io/guide


Pandas Visualization
Syntax for How to use Explained Syntax for How to use Explained

Create a horizontal bar chart Create a heatmap with


BARH (SERIES) Series.plot.barh()
HEATMAP sns.heatmap(data, annot=True,

from a Series object annotations


plt.show() cmap='coolwarm')

Customize bar colors and


Series.plot.barh(color='orange',
Create a heatmap with line
borders sns.heatmap(data, annot=True,

edgecolor='black')
spacing and custom colors
linewidths=0.5, cmap='Blues')
plt.show()

Create a boxplot to visualize


BOXPLOT DataFrame.plot.box()
Create pair plots for all
data distributions PAIRPLOT sns.pairplot(data)
plt.show() combinations of features

VIOLINPLOT sns.violinplot(x='x_var', y='y_var',


Create a violin plot to visualize
data distribution
Seaborn Visualizations
data=data)

JOINTPLOT sns.jointplot(x='x_var', y='y_var',


Create a joint plot to visualize
Syntax for How to use Explained bivariate data
data=data, kind='reg')

Create a relational plot with


RELPLOT sns.relplot(data=data, x='x_var', y='y_var',

multiple attributes
hue='hue_var', size='size_var',

style='style_var')

plt.show()

Create subplots for relational


sns.relplot(data=data, x='x_var', y='y_var',

plots based on a column


hue='hue_var', col='col_var')

plt.show()

matplotlib Cheat Sheet Free resources at: dataquest.io/guide

You might also like