Python - Graphs

Matplotlib

Installation

using pip

pip install matplotlib

Import Matplotlib

using pip

import matplotlib

Plot

import pyplot from matplotlib

import matplotlib.pyplot as plt

first graph

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.show()
Matplotlib First Graph

Add Labels

plt.xlabel("x label")
plt.ylabel("y label")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.show()
Matplotlib Add Labels

Add Title

plt.title("title")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")

plt.show()
Matplotlib Add Labels

Add Title

plt.title("title")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")

plt.show()
Matplotlib >Add Title

Change Title Size

plt.title("title" , fontsize=24)

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title", fontsize=24)
plt.show()
Matplotlib >Change Title Font Size

Change Title Weight

plt.title("title", fontweight=780)

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title", fontweight=780)
plt.show()
Matplotlib >hange Title Font Weight

Change Title Color

plt.title("title", color='r')

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title", color='r')
plt.show()
Matplotlib >Change Title Color

Change Title Position

plt.title("title", pad='20.0', loc="left", y=0.85)

loc = text align {'center', 'left', 'right'}.
y - Vertical Axes loation for the title (1.0 is the top).
pad - The offset of the title from the top of the Axes, in points.

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title", pad='20.0', loc="left", y=0.85)
plt.show()
Matplotlib >Change Title Position

Change Font

plt.ylabel("y label", size=20, family='serif')

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label", size=20, family='serif')
plt.title("title")
plt.show()
Matplotlib >Change Change Font

Add Markers

plt.plot(ax, ay, marker='v')

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='v')
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")

plt.show()
Matplotlib Add Markers
Character Description
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle down marker
'^' triangle up marker
'<' triangle left marker
'>' triangle right
'1' tri down marker
'2' tri up marker
'3' tri left marker
'4' tri right marker
'8' octagon marker
's' square marker
'p' pentagon marker
'P' plus (filled) marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'X' x (filled) marker
'D' diamond marker
'd' thin diamond marker
'|' vline marker
'_' hline marker

Add Line Style

plt.plot(ax, ay, marker='|', linestyle='-.')

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='|', linestyle='-.')
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Matplotlib Add Markers
Linestyle Description
'-' or 'solid' solid line
'--' or 'dashed' dashed line
'-.' or 'dashdot' dash-dotted line
':' or 'dotted' dotted line
'None' or ' ' or '' draw nothing

Add Color

plt.plot(ax, ay, marker='|', color="black")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='|', color="#f313eb")
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Add Line Style
Character Color
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
#XXXXXX Hex Color Code

Add Draw Style

plt.plot(ax, ay, marker='|', drawstyle="steps-post")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='|', drawstyle="steps-post")
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Add Draw Style
Draw Style Description
'default' straight lines
'steps-pre' The step is at the beginning of the line segment
'steps-mid' The step is halfway between the points.
'steps-post' The step is at the end of the line segment
'steps' Equal to 'steps-pre' and is maintained for backward-compatibility.

Marker Size

plt.plot(ax, ay, marker='|', markersize="61")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='|', markersize="61")
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Marker Size

Add Marker Color

plt.plot(ax, ay, marker='o', markersize="61", markeredgecolor="cyan", markerfacecolor="y")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='o', markersize="61", markeredgecolor="cyan", markerfacecolor="y")
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Add Marker Color

Line Width

plt.plot(ax, ay, marker='o', markersize="15", linewidth="9")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='o', markersize="15", linewidth="9")
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Add Marker Color

Change Marker Edge Width

plt.plot(ax, ay, marker='o', markersize="5", markeredgewidth="20")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay, marker='o', markersize="5", markeredgewidth="20")
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Add Marker Color

Multiple Lines

plt.plot(ax_1, ay_1, marker='o')
plt.plot(ax_2, ay_2, marker='o')

Full Code

import matplotlib.pyplot as plt

ax_1 = [1, 2, 3, 4]
ay_1 = [1, 2, 2, 3]

ax_2 = [1, 2, 3, 4]
ay_2 = [4, 2, 9, 5]

plt.plot(ax_1, ay_1, marker='o')
plt.plot(ax_2, ay_2, marker='o')
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.show()
Add Marker Color

Add Grid Lines

plt.grid()

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.grid()
plt.show()
Add Grid Lines

Add Grid Horizontal

plt.grid(axis="y")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.grid(axis="y")
plt.show()
Add Grid Horizontal

Add Grid Vertical

plt.grid(axis="x")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.grid(axis="x")
plt.show()
Add Grid Vertical

Add Color To Grid

plt.grid(color="#00ff00")

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.grid(color="#00ff00")
plt.show()
Add Color To Grid

Add Line Style To Grid

plt.grid(linestyle='dashed')

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.grid(linestyle='dashed')
plt.show()
Add Line Style  To Grid
Linestyle Description
'-' or 'solid' solid line
'--' or 'dashed' dashed line
'-.' or 'dashdot' dash-dotted line
':' or 'dotted' dotted line
'None' or ' ' or '' draw nothing

Add Line Width To Grid

plt.grid(linewidth=3)

Full Code

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4]
ay = [1, 2, 2, 3]

plt.plot(ax, ay)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.grid(linestyle=':', linewidth=3)
plt.show()
Add Line Width To Grid

Multiple Plots

Subplot take row number ,column number and plot number.

plt.subplot(1, 2, 1)

Full Code

import matplotlib.pyplot as plt

ax_1 = [1, 2, 3, 4]
ay_1 = [1, 2, 2, 3]

ax_2 = [1, 2, 3, 4]
ay_2 = [7, 3, 4, 2]
plt.subplot(1, 2, 1)
plt.plot(ax_1, ay_1)
plt.subplot(1, 2, 2)
plt.plot(ax_2, ay_2)

plt.show()
Multiple Plots

Multiple Plots Add Title And Labels

import matplotlib.pyplot as plt

ax_1 = [1, 2, 3, 4]
ay_1 = [1, 2, 2, 3]

ax_2 = [1, 2, 3, 4]
ay_2 = [7, 3, 4, 2]
plt.subplot(1, 2, 1)
plt.plot(ax_1, ay_1)
plt.xlabel("x_1 label")
plt.ylabel("y_1 label")
plt.title("title_1")

plt.subplot(1, 2, 2)
plt.plot(ax_2, ay_2)
plt.xlabel("x_2 label")
plt.ylabel("y_2 label")
plt.title("title_2")

plt.show()

Multiple Plots

Add Super Title To Multiple Plots

plt.suptitle("title_top")

Full Code

import matplotlib.pyplot as plt

ax_1 = [1, 2, 3, 4]
ay_1 = [1, 2, 2, 3]

ax_2 = [1, 2, 3, 4]
ay_2 = [7, 3, 4, 2]
plt.subplot(1, 2, 1)
plt.plot(ax_1, ay_1)
plt.xlabel("x_1 label")
plt.ylabel("y_1 label")
plt.title("title_1")

plt.subplot(1, 2, 2)
plt.plot(ax_2, ay_2)
plt.xlabel("x_2 label")
plt.ylabel("y_2 label")
plt.title("title_2")
plt.suptitle("title_top")
plt.show()
Multiple Plots

Bars Graph

import matplotlib.pyplot as plt

ax = ["B1", "B2", "B3", "B4"]
ay = [1, 2, 2, 3]

plt.bar(ax,ay)
plt.show()
Bars Graph

Horizontal Bars

import matplotlib.pyplot as plt

ax = ["B1", "B2", "B3", "B4"]
ay = [1, 2, 2, 3]

plt.barh(ax, ay)
plt.show()
Horizontal Bars

Add Color To Bar

import matplotlib.pyplot as plt

ax = ["B1", "B2", "B3", "B4"]
ay = [1, 2, 2, 3]

plt.bar(ax, ay, color="red")
plt.show()
Add Color To Bar

Add Edge Color To Bar

import matplotlib.pyplot as plt

ax = ["B1", "B2", "B3", "B4"]
ay = [1, 2, 2, 3]

plt.bar(ax, ay, edgecolor="red")
plt.show()
Add Color To Bar

Change Bar Width

import matplotlib.pyplot as plt

ax = ["B1", "B2", "B3", "B4"]
ay = [1, 2, 2, 3]
plt.bar(ax, ay, width=[0.1, 0.2, 0.3, 0.4])
plt.show()
Change Bar Width

Histograms Graph

import matplotlib.pyplot as plt

ax = [5, 2, 2, 3, 2, 2, 3, 1]
plt.hist(ax)
plt.show()
Bars Histograms

Set Histograms Range

The lower and upper range of the bins. Lower and upper outliers are ignored.

import matplotlib.pyplot as plt

ax = [5, 2, 2, 3, 2, 2, 3, 1, 7]

plt.hist(ax, range=(2, 5))
plt.title("title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.show()
Bars Histograms

Horizontal Histogram

import matplotlib.pyplot as plt

ax = [5, 2, 2, 3, 2, 2, 3, 1, 7]

plt.hist(ax, orientation="horizontal")
plt.show()
Bars Histograms

Change Horizontal Width

import matplotlib.pyplot as plt

ax = [5, 2, 2, 3, 2, 2, 3, 1, 7]

plt.hist(ax, rwidth=0.5)
plt.show()
Change Bar Width

Change Color To Horizontal

import matplotlib.pyplot as plt

ax = [5, 2, 2, 3, 2, 2, 3, 1, 7]

plt.hist(ax, color="b")
plt.show()
Change Color To Horizontal

Pie Graph

import matplotlib.pyplot as plt

ax = [5, 2, 2, 3, 2, 2, 3, 1, 7]

plt.pie(ax)
plt.show()
Pie Graph

Add Labels

import matplotlib.pyplot as plt

ax = [5, 2, 2]
ax_labels = ["A", "B", "C"]

plt.pie(ax, labels=ax_labels)
plt.show()
Add Labels To Pie Graph

Add Explode

import matplotlib.pyplot as plt

ax = [5, 2, 2]
ax_labels = ["A", "B", "C"]
ax_explode = [0, 0.3, 0]

plt.pie(ax, labels=ax_labels, explode=ax_explode)
plt.show()
Add Explode To Pie Graph

Add Shadow

import matplotlib.pyplot as plt

ax = [5, 2, 2]
ax_labels = ["A", "B", "C"]
ax_explode = [0, 0.3, 0]

plt.pie(ax, labels=ax_labels, explode=ax_explode, shadow=True)
plt.show()
Add Shadow To Pie Graph

Add Color

import matplotlib.pyplot as plt

ax = [5, 2, 2]
ax_labels = ["A", "B", "C"]
ax_explode = [0, 0.3, 0]

plt.pie(ax, labels=ax_labels, colors=["r", "b", "green"])
plt.show()
Add Color To Pie Graph

Add Legend

import matplotlib.pyplot as plt

ax = [5, 2, 2]
ax_labels = ["A", "B", "C"]
plt.pie(ax)
plt.legend(labels=ax_labels, title="title")

plt.show()
Add Legend To Pie Graph

Stem Plot

import matplotlib.pyplot as plt

ax = [1, 2, 3, 4, 5, 6]
ay = [3, 6, 8, 4, 3, 6]
plt.stem(ax, ay)
plt.show()
Stem Plot