Link to the matplotlib gallery at https://github.com/rasbt/matplotlib-gallery
In [1]:
%load_ext watermark
In [2]:
%watermark -u -v -d -p matplotlib,numpy
[More info](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) about the `%watermark` extension
In [3]:
%matplotlib inline
In [4]:
import numpy as np
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
fig, ax = plt.subplots(2)
for sp in ax:
sp.plot(x, y)
In [5]:
x = range(10)
y = range(10)
fig, ax = plt.subplots(nrows=2,ncols=2)
for row in ax:
for col in row:
col.plot(x, y)
plt.show()
In [6]:
fig, ax = plt.subplots(nrows=2,ncols=2)
plt.subplot(2,2,1)
plt.plot(x, y)
plt.subplot(2,2,2)
plt.plot(x, y)
plt.subplot(2,2,3)
plt.plot(x, y)
plt.subplot(2,2,4)
plt.plot(x, y)
plt.show()
In [14]:
x = range(10)
y = range(10)
fig, ax = plt.subplots(nrows=2,ncols=2, sharex=True, sharey=True)
for row in ax:
for col in row:
col.plot(x, y)
plt.show()
In [20]:
x = range(10)
y = range(10)
fig, ax = plt.subplots(nrows=2,ncols=2)
for row in ax:
for col in row:
col.plot(x, y)
col.set_title('title')
col.set_xlabel('x-axis')
col.set_ylabel('x-axis')
fig.tight_layout()
plt.show()
Matplotlib supports 3 different ways to encode colors, e.g, if we want to use the color blue, we can define colors as
(0.0, 0.0, 1.0)
'blue'
or 'b'
'#0000FF'
In [18]:
import matplotlib.pyplot as plt
samples = range(1,4)
for i, col in zip(samples, [(0.0, 0.0, 1.0), 'blue', '#0000FF']):
plt.plot([0, 10], [0, i], lw=3, color=col)
plt.legend(['RGB values: (0.0, 0.0, 1.0)',
"matplotlib names: 'blue'",
"HTML hex values: '#0000FF'"],
loc='upper left')
plt.title('3 alternatives to define the color blue')
plt.show()
The color names that are supported by matplotlib are
b: blue
g: green
r: red
c: cyan
m: magenta
y: yellow
k: black
w: white
where the first letter represents the shortcut version.
In [19]:
import matplotlib.pyplot as plt
cols = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'white']
samples = range(1, len(cols)+1)
for i, col in zip(samples, cols):
plt.plot([0, 10], [0, i], label=col, lw=3, color=col)
plt.legend(loc='upper left')
plt.title('matplotlib color names')
plt.show()
More color maps are available at http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps
In [4]:
import numpy as np
import matplotlib.pyplot as plt
fig, (ax0, ax1) = plt.subplots(1,2, figsize=(14, 7))
samples = range(1,16)
# Default Color Cycle
for i in samples:
ax0.plot([0, 10], [0, i], label=i, lw=3)
# Colormap
colormap = plt.cm.Paired
plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.9, len(samples))])
for i in samples:
ax1.plot([0, 10], [0, i], label=i, lw=3)
# Annotation
ax0.set_title('Default color cycle')
ax1.set_title('plt.cm.Paired colormap')
ax0.legend(loc='upper left')
ax1.legend(loc='upper left')
plt.show()
In [28]:
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(8,6))
samples = np.arange(0, 1.1, 0.1)
for i in samples:
plt.plot([0, 10], [0, i], label='gray-level %s'%i, lw=3,
color=str(i)) # ! gray level has to be parsed as string
plt.legend(loc='upper left')
plt.title('gray-levels')
plt.show()
In [58]:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10,10))
samples = np.random.randn(30,2)
ax[0][0].scatter(samples[:,0], samples[:,1],
color='red',
label='color="red"')
ax[1][0].scatter(samples[:,0], samples[:,1],
c='red',
label='c="red"')
ax[0][1].scatter(samples[:,0], samples[:,1],
edgecolor='white',
c='red',
label='c="red", edgecolor="white"')
ax[1][1].scatter(samples[:,0], samples[:,1],
edgecolor='0',
c='1',
label='color="1.0", edgecolor="0"')
for row in ax:
for col in row:
col.legend(loc='upper left')
plt.show()
In [4]:
import numpy as np
import matplotlib.pyplot as plt
markers = [
'.', # point
',', # pixel
'o', # circle
'v', # triangle down
'^', # triangle up
'<', # triangle_left
'>', # triangle_right
'1', # tri_down
'2', # tri_up
'3', # tri_left
'4', # tri_right
'8', # octagon
's', # square
'p', # pentagon
'*', # star
'h', # hexagon1
'H', # hexagon2
'+', # plus
'x', # x
'D', # diamond
'd', # thin_diamond
'|', # vline
]
plt.figure(figsize=(13, 10))
samples = range(len(markers))
for i in samples:
plt.plot([i-1, i, i+1], [i, i, i], label=markers[i], marker=markers[i], markersize=10)
# Annotation
plt.title('Matplotlib Marker styles', fontsize=20)
plt.ylim([-1, len(markers)+1])
plt.legend(loc='lower right')
plt.show()
In [13]:
import numpy as np
import matplotlib.pyplot as plt
linestyles = ['-.', '--', 'None', '-', ':']
plt.figure(figsize=(8, 5))
samples = range(len(linestyles))
for i in samples:
plt.plot([i-1, i, i+1], [i, i, i],
label='"%s"' %linestyles[i],
linestyle=linestyles[i],
lw=4
)
# Annotation
plt.title('Matplotlib line styles', fontsize=20)
plt.ylim([-1, len(linestyles)+1])
plt.legend(loc='lower right')
plt.show()
In [26]:
import numpy as np
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
fig = plt.gca()
plt.plot(x, y)
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.show()
In [29]:
import numpy as np
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
fig = plt.gca()
plt.plot(x, y)
# removing frame
fig.spines["top"].set_visible(False)
fig.spines["bottom"].set_visible(False)
fig.spines["right"].set_visible(False)
fig.spines["left"].set_visible(False)
# removing ticks
plt.tick_params(axis="both", which="both", bottom="off", top="off",
labelbottom="on", left="off", right="off", labelleft="on")
plt.show()