Skip to content

Commit f999c91

Browse files
author
Mofan
committed
update matplotlib tutorials
1 parent 5c7ca08 commit f999c91

18 files changed

+722
-0
lines changed

matplotlibTUT/plt10_scatter.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 10 - scatter
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial reference:
11+
http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
17+
n = 1024 # data size
18+
X = np.random.normal(0, 1, n)
19+
Y = np.random.normal(0, 1, n)
20+
T = np.arctan2(Y, X) # for color later on
21+
22+
plt.scatter(X, Y, s=75, c=T, alpha=.5)
23+
24+
plt.xlim(-1.5, 1.5)
25+
plt.xticks(()) # ignore xticks
26+
plt.ylim(-1.5, 1.5)
27+
plt.yticks(()) # ignore yticks
28+
29+
plt.show()

matplotlibTUT/plt11_bar.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 11 - bar
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial reference:
11+
http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
17+
n = 12
18+
X = np.arange(n)
19+
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
20+
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
21+
22+
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
23+
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
24+
25+
for x, y in zip(X, Y1):
26+
# ha: horizontal alignment
27+
# va: vertical alignment
28+
plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
29+
30+
for x, y in zip(X, Y2):
31+
# ha: horizontal alignment
32+
# va: vertical alignment
33+
plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')
34+
35+
plt.xlim(-.5, n)
36+
plt.xticks(())
37+
plt.ylim(-1.25, 1.25)
38+
plt.yticks(())
39+
40+
plt.show()

matplotlibTUT/plt12_contours.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 12 - contours
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial reference:
11+
http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
17+
def f(x,y):
18+
# the height function
19+
return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)
20+
21+
n = 256
22+
x = np.linspace(-3, 3, n)
23+
y = np.linspace(-3, 3, n)
24+
X,Y = np.meshgrid(x, y)
25+
26+
# use plt.contourf to filling contours
27+
# X, Y and value for (X,Y) point
28+
plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot)
29+
30+
# use plt.contour to add contour lines
31+
C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
32+
# adding label
33+
plt.clabel(C, inline=True, fontsize=10)
34+
35+
plt.xticks(())
36+
plt.yticks(())
37+
plt.show()
38+

matplotlibTUT/plt13_image.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 13 - image
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
"""
11+
12+
import matplotlib.pyplot as plt
13+
import numpy as np
14+
15+
# image data
16+
a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
17+
0.365348418405, 0.439599930621, 0.525083754405,
18+
0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
19+
20+
"""
21+
for the value of "interpolation", check this:
22+
http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
23+
for the value of "origin"= ['upper', 'lower'], check this:
24+
http://matplotlib.org/examples/pylab_examples/image_origin.html
25+
"""
26+
plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')
27+
plt.colorbar(shrink=.92)
28+
29+
plt.xticks(())
30+
plt.yticks(())
31+
plt.show()
32+

matplotlibTUT/plt14_3d.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 14 - 3d
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial page:
11+
http://www.python-course.eu/matplotlib_multiple_figures.php
12+
"""
13+
14+
import numpy as np
15+
import matplotlib.pyplot as plt
16+
from mpl_toolkits.mplot3d import Axes3D
17+
18+
fig = plt.figure()
19+
ax = Axes3D(fig)
20+
# X, Y value
21+
X = np.arange(-4, 4, 0.25)
22+
Y = np.arange(-4, 4, 0.25)
23+
X, Y = np.meshgrid(X, Y)
24+
R = np.sqrt(X ** 2 + Y ** 2)
25+
# height value
26+
Z = np.sin(R)
27+
28+
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)
29+
"""
30+
============= ================================================
31+
Argument Description
32+
============= ================================================
33+
*X*, *Y*, *Z* Data values as 2D arrays
34+
*rstride* Array row stride (step size), defaults to 10
35+
*cstride* Array column stride (step size), defaults to 10
36+
*color* Color of the surface patches
37+
*cmap* A colormap for the surface patches.
38+
*facecolors* Face colors for the individual patches
39+
*norm* An instance of Normalize to map values to colors
40+
*vmin* Minimum value to map
41+
*vmax* Maximum value to map
42+
*shade* Whether to shade the facecolors
43+
============= ================================================
44+
"""
45+
46+
# I think this is different from plt12_contours
47+
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)
48+
"""
49+
========== ================================================
50+
Argument Description
51+
========== ================================================
52+
*X*, *Y*, Data values as numpy.arrays
53+
*Z*
54+
*zdir* The direction to use: x, y or z (default)
55+
*offset* If specified plot a projection of the filled contour
56+
on this position in plane normal to zdir
57+
========== ================================================
58+
"""
59+
60+
ax.set_zlim(-2, 2)
61+
62+
plt.show()
63+

matplotlibTUT/plt15_subplot.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 15 - subplot
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial page:
11+
http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
16+
# example 1:
17+
###############################
18+
plt.figure(figsize=(6, 4))
19+
# plt.subplot(n_rows, n_cols, plot_num)
20+
plt.subplot(2, 2, 1)
21+
plt.plot([0, 1], [0, 1])
22+
23+
plt.subplot(222)
24+
plt.plot([0, 1], [0, 2])
25+
26+
plt.subplot(223)
27+
plt.plot([0, 1], [0, 3])
28+
29+
plt.subplot(224)
30+
plt.plot([0, 1], [0, 4])
31+
32+
plt.tight_layout()
33+
34+
# example 2:
35+
###############################
36+
plt.figure(figsize=(6, 4))
37+
# plt.subplot(n_rows, n_cols, plot_num)
38+
plt.subplot(2, 1, 1)
39+
# figure splits into 2 rows, 1 col, plot to the 1st sub-fig
40+
plt.plot([0, 1], [0, 1])
41+
42+
plt.subplot(234)
43+
# figure splits into 2 rows, 3 col, plot to the 4th sub-fig
44+
plt.plot([0, 1], [0, 2])
45+
46+
plt.subplot(235)
47+
# figure splits into 2 rows, 3 col, plot to the 5th sub-fig
48+
plt.plot([0, 1], [0, 3])
49+
50+
plt.subplot(236)
51+
# figure splits into 2 rows, 3 col, plot to the 6th sub-fig
52+
plt.plot([0, 1], [0, 4])
53+
54+
55+
plt.tight_layout()
56+
plt.show()

matplotlibTUT/plt16_grid_subplot.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 16 - grid
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial page:
11+
http://matplotlib.org/users/gridspec.html
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
import matplotlib.gridspec as gridspec
16+
17+
# method 1: subplot2grid
18+
##########################
19+
plt.figure()
20+
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) # stands for axes
21+
ax1.plot([1, 2], [1, 2])
22+
ax1.set_title('ax1_title')
23+
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
24+
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
25+
ax4 = plt.subplot2grid((3, 3), (2, 0))
26+
ax4.scatter([1, 2], [2, 2])
27+
ax4.set_xlabel('ax4_x')
28+
ax4.set_ylabel('ax4_y')
29+
ax5 = plt.subplot2grid((3, 3), (2, 1))
30+
31+
# method 2: gridspec
32+
#########################
33+
plt.figure()
34+
gs = gridspec.GridSpec(3, 3)
35+
# use index from 0
36+
ax6 = plt.subplot(gs[0, :])
37+
ax7 = plt.subplot(gs[1, :2])
38+
ax8 = plt.subplot(gs[1:, 2])
39+
ax9 = plt.subplot(gs[-1, 0])
40+
ax10 = plt.subplot(gs[-1, -2])
41+
42+
# method 3: easy to define structure
43+
####################################
44+
f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
45+
ax11.scatter([1,2], [1,2])
46+
47+
plt.tight_layout()
48+
plt.show()

matplotlibTUT/plt17_plot_in_plot.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 17 - plot in plot
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial page:
11+
http://www.python-course.eu/matplotlib_multiple_figures.php
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
import matplotlib.gridspec as gridspec
16+
17+
fig = plt.figure()
18+
x = [1, 2, 3, 4, 5, 6, 7]
19+
y = [1, 3, 4, 2, 5, 8, 6]
20+
21+
# below are all percentage
22+
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
23+
ax1 = fig.add_axes([left, bottom, width, height]) # main axes
24+
ax1.plot(x, y, 'r')
25+
ax1.set_xlabel('x')
26+
ax1.set_ylabel('y')
27+
ax1.set_title('title')
28+
29+
ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25]) # inside axes
30+
ax2.plot(y, x, 'b')
31+
ax2.set_xlabel('x')
32+
ax2.set_ylabel('y')
33+
ax2.set_title('title inside 1')
34+
35+
36+
# different method to add axes
37+
####################################
38+
plt.axes([0.6, 0.2, 0.25, 0.25])
39+
plt.plot(y[::-1], x, 'g')
40+
plt.xlabel('x')
41+
plt.ylabel('y')
42+
plt.title('title inside 2')
43+
44+
plt.show()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# View more python tutorials on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
# 18 - secondary y axis
7+
"""
8+
Please note, this script is for python3+.
9+
If you are using python2+, please modify it accordingly.
10+
Tutorial page:
11+
http://www.python-course.eu/matplotlib_multiple_figures.php
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
17+
x = np.arange(0, 10, 0.1)
18+
y1 = 0.05 * x**2
19+
y2 = -1 *y1
20+
21+
fig, ax1 = plt.subplots()
22+
23+
ax2 = ax1.twinx() # mirror the ax1
24+
ax1.plot(x, y1, 'g-')
25+
ax2.plot(x, y2, 'b-')
26+
27+
ax1.set_xlabel('X data')
28+
ax1.set_ylabel('Y1 data', color='g')
29+
ax2.set_ylabel('Y2 data', color='b')
30+
31+
plt.show()

0 commit comments

Comments
 (0)