Matplotlib Guide: Plotting Basics
Matplotlib Guide: Plotting Basics
Preliminaries
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
1
Build a line plot with Matplotlib and raw (x, y) data Build a multi-line plot from raw data in matplotlib
A single line plot in matplotlib A multi-line plot with markers and line-styles
# --- fake up some data # --- select a style
x = [Link](0, 4*[Link], 800) [Link]('classic')
y = [Link](x)
# --- get the Figure and Axes all at once
# --- Select a style fig, ax = [Link](figsize=(8,6))
[Link]('classic')
# --- plot some lines
# --- get an empty Figure and add an Axes N = 8 # the number of lines we will plot
fig = [Link](figsize=(8,4)) styles = ['-', '--', '-.', ':']
ax = fig.add_subplot(1,1,1) # row-col-num markers = list('+ox^psDv')
x = [Link](0, 100, 20)
# --- line plot data on the Axes for i in range(N): # add line-by-line
[Link](x, y, 'b-', linewidth=2, y = x + x/5*i + i
label=r'$y=\sin(x)$') s = styles[i % len(styles)]
m = markers[i % len(markers)]
# --- add title and axis labels [Link](x, y,
ax.set_title('The Sine Wave') label='Line '+str(i+1)+' '+s+m,
ax.set_ylabel(r'$y$', fontsize=16) marker=m, linewidth=2, linestyle=s)
ax.set_xlabel(r'$x$', fontsize=16)
# --- add grid, legend, title and save
# --- change the default plot limits [Link](True)
ax.set_xlim((-0.05, 4*[Link] + 0.05,)) [Link](loc='best', prop={'size':'large'})
ax.set_ylim((-1.05, 1.05)) ax.set_title('A Simple Line Plot')
fig.tight_layout(pad=1)
# --- plot a legend in the best location [Link]('[Link]', dpi=125)
[Link](loc='best') [Link]('all')
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
2
Scatter plots – using matplotlib's [Link]()
Use seaborn
import seaborn as sns
x = [Link](100)
y = x + [Link](100) + 10
df = DataFrame([x, y], index=['x', 'y']).T
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
3
Changing the marker symbol
Scatter plots – more options
# --- get the Figure and Axes classes
[Link]('classic')
Changing the marker size and colour fig, ax = [Link](figsize=(8,8))
# --- import a colour map
import [Link] as cm # --- add scatter plots
markers = list('ov^<>12348sphHdD+x*|_')
# --- get some data N = len(markers)
N = 100 for i, m in enumerate(markers):
x = [Link](N) x = [Link](N)
y = [Link](N) y = [Link](i+1, N)
size = (([Link](N) + 1) * 8) ** 2 [Link](x, y, marker=m, label=m,
colour = [Link](N) s=50, c='cornflowerblue')
ax.set_xlim((-0.05, 1.05))
ax.set_ylim((-0.05, 1.05))
fig.tight_layout(pad=1);
[Link]('[Link]', dpi=125)
[Link]('all')
Note: there are many colormaps to choose from.
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
4
Bar plots – using [Link]() and [Link]()
As nice as pie
# --- get some data
data = [Link]([5,3,4,6])
labels = ['bats', 'cats', 'gnats', 'rats']
explode = (0, 0.1, 0, 0) # explode cats
colrs=['khaki', 'goldenrod', 'tan', 'wheat']
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
6
Spines in the middle
Plot spines
# --- the data
x = [Link](-[Link], [Link], 800)
Hiding the top and right spines y = [Link](x)
# --- the data
x = [Link](-[Link], [Link], 800) # --- the plot
y = [Link](x) fig, ax = [Link](figsize=(8, 4))
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
7
Legend to the right of the plot
Legends
[Link]('classic')
fig, ax = [Link](figsize=(8, 4))
Legend within the plot
Use the 'loc' argument to place the legend x = [Link](N)
# --- get the empty Figure and Axes classes for j in range(5):
[Link]('classic') [Link](x, x*(j+1),
fig, ax = [Link](figsize=(8, 4)) label='Line '+str(j))
N = 5
x = [Link](N)
for j in range(5):
Legend slightly outside of the plot [Link](x, x*(j+1),
[Link]('classic') label='Line '+str(j))
fig, ax = [Link](figsize=(8, 4))
N = 5 fig.tight_layout(pad=1)
x = [Link](N) box = ax.get_position()
for j in range(5): ax.set_position([box.x0,
[Link](x, x*(j+1), box.y0 + [Link] * 0.15,
label='Line '+str(j)) [Link], [Link] * 0.85])
[Link](bbox_to_anchor=(0.5, -0.075),
[Link](bbox_to_anchor=(1.05, 1.05)) loc='upper center', ncol=N)
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
8
Using GridSpec layouts (like list slicing)
Multiple plots on a canvas
import [Link] as gs
gs = [Link](3, 3) # nrows, ncols
Using Axes to place a plot within a plot fig = [Link](figsize=(8,3))
fig = [Link](figsize=(8,3)) [Link](x=0.01, y=0.01, s='Figure',
[Link](x=0.01, y=0.01, s='Figure', color='#888888', ha='left',
color='#888888', ha='left', va='bottom', fontsize=20)
va='bottom', fontsize=20) ax1 = fig.add_subplot(gs[0, :]) # row,col
[Link](x=0.2,y=0.2,s='0, :', color='b')
# --- Main Axes ax2 = fig.add_subplot(gs[1,:-1])
ax = fig.add_axes([0.1,0.1,0.8,0.8]) [Link](x=0.2,y=0.2,s='1, :-1', color='b')
[Link](x=0.01, y=0.01, s='Main Axes', ax3 = fig.add_subplot(gs[1:, -1])
color='red', ha='left', va='bottom', [Link](x=0.2,y=0.2, s='1:, -1', color='b')
fontsize=20) ax4 = fig.add_subplot(gs[-1,0])
ax.set_xticks([]); ax.set_yticks([]) [Link](x=0.2,y=0.2, s='-1, :0', color='b')
ax5 = fig.add_subplot(gs[-1,-2])
# --- Insert Axes [Link](x=0.2,y=0.2, s='-1,:-2', color='b')
ax= fig.add_axes([0.15,0.65,0.2,0.2]) for a in fig.get_axes():
[Link](x=0.01, y=0.01, s='Insert Axes', a.set_xticks([])
color='blue', ha='left', va='bottom', a.set_yticks([])
fontsize=20)
ax.set_xticks([]); ax.set_yticks([]) [Link]('GridSpec Layout')
[Link]('An Axes within an Axes') [Link]('[Link]', dpi=125)
[Link]('[Link]', dpi=125) [Link]('all')
[Link]('all')
for i in range(4):
# fig.add_subplot(nrows, ncols, num)
ax = fig.add_subplot(2, 2, i+1)
[Link](x=0.01, y=0.01,
s='Subplot 2 2 '+str(i+1),
color='red', ha='left',
va='bottom', fontsize=20)
ax.set_xticks([]); ax.set_yticks([])
ax.set_xticks([]); ax.set_yticks([])
[Link]('Subplots')
[Link]('[Link]', dpi=125)
[Link]('all')
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
9
Style Roll your own style sheet
[Link]: -
[Link]: 0.5
[Link]: e7e7e7
[Link]: 0
[Link]: 0
[Link]: small
[Link]: 333333
[Link]: 0
[Link]: 0
[Link]: small
[Link]: 333333
[Link]: 8, 4 # inches
[Link]: white
[Link]: black
[Link]: white
[Link]: white
Which is saved as "[Link]"
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
10
Cautionary notes
Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
11
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
1
Introduct](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F1.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
2
Build a](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F2.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
3
Scatter](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F3.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
4
Scatter](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F4.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
5
Bar plo](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F5.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
6
Horizonta](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F6.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
7
Plot sp](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F7.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
8
Legends](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F8.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
9
Multipl](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F9.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)
![Version 19 May 2017 - [Draft – Mark Graph – mark dot the dot graph at gmail dot com – @Mark_Graph on twitter]
10
Style](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F376751091%2F10.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F376751091%2FMatplotlib-Notes&__type=image)