matplotlib—-1

Matplotlib

import matplotlib

1
2
import matplotlib.pyplot as plt
%matplotlib inline

style

1
2
3
4
5
6
7
plt.figure() # 创建图形
# 创建两个子图中的第一个,设置坐标轴
plt.subplot(2, 1, 1) # (行、列、子图编号)
plt.plot(x, np.sin(x))
# 创建两个子图中的第二个,设置坐标轴
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x));

dn2IFP.png

1
2
3
4
5
6
7
%matplotlib inline 
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np

fig = plt.figure()
ax = plt.axes()

dnW3gU.png

1
2
3
4
5
6
7
fig = plt.figure() 
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x))

#the same:
plt.plot(x, np.sin(x));

dnhEwj.png

1
2
plt.plot(x, np.sin(x)) 
plt.plot(x, np.cos(x));

dn4tbQ.png

1
2
3
4
5
6
plt.plot(x, np.sin(x - 0), color='blue') # 标准颜色名称
plt.plot(x, np.sin(x - 1), color='g') # 缩写颜色代码(rgbcmyk)
plt.plot(x, np.sin(x - 2), color='0.75') # 范围在0~1的灰度值
plt.plot(x, np.sin(x - 3), color='#FFDD44') # 十六进制(RRGGBB,00~FF)
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB元组,范围在0~1
plt.plot(x, np.sin(x - 5), color='chartreuse'); # HTML颜色名称

styles of curve

1
2
3
4
5
6
7
8
9
10
11
12
13
14
plt.plot(x, x + 0, linestyle='solid') 
plt.plot(x, x + 1, linestyle='dashed')
plt.plot(x, x + 2, linestyle='dashdot')
plt.plot(x, x + 3, linestyle='dotted');
# 你可以用下面的简写形式
plt.plot(x, x + 4, linestyle='-') # 实线
plt.plot(x, x + 5, linestyle='--') # 虚线
plt.plot(x, x + 6, linestyle='-.') # 点划线
plt.plot(x, x + 7, linestyle=':'); # 实点线

plt.plot(x, x + 0, '-g') # 绿色实线
plt.plot(x, x + 1, '--c') # 青色虚线
plt.plot(x, x + 2, '-.k') # 黑色点划线
plt.plot(x, x + 3, ':r'); # 红色实点线

limit the size

1
2
3
plt.plot(x, np.sin(x)) 
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5);

cut the blanket area

1
plt.axis('tight');

make the x-axis and y-aix to be 1 to 1

1
2
plt.plot(x, np.sin(x)) 
plt.axis('equal')

label

1
2
3
4
5
6
7
8
9
plt.plot(x, np.sin(x)) 
plt.title("A Sine Curve")
plt.xlabel("x") #x-axis label
plt.ylabel("sin(x)")

plt.plot(x, np.sin(x), '-g', label='sin(x)')
plt.plot(x, np.cos(x), ':b', label='cos(x)')
plt.axis('equal')
plt.legend()

scatter diagram

1
2
3
4
5
6
7
8
%matplotlib inline 
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np

x = np.linspace(0, 10, 30)
y = np.sin(x)
plt.plot(x, y, 'o', color='black') #"o" means to use the point

dG4t1g.png

different values can be represented by different symbol:

1
2
3
4
5
6
rng = np.random.RandomState(0) 
for marker in ['o', '.', ',', 'x', '+', 'v', '^', '<', '>', 's', 'd']:
plt.plot(rng.rand(5), rng.rand(5), marker,
label="marker='{0}'".format(marker))
plt.legend(numpoints=1)
plt.xlim(0, 1.8)

dG42jJ.png

other function

1
2
3
4
5
6
plt.plot(x, y, '-p', color='gray',  #"-" represented the curve
markersize=15, linewidth=4,
markerfacecolor='white',
markeredgecolor='gray',
markeredgewidth=2)
plt.ylim(-1.2, 1.2)

dGobgU.png

another way:scatter

1
plt.scatter(x, y, marker='o')

whent the random points have different size

1
2
3
4
5
6
7
rng = np.random.RandomState(0) 
x = rng.randn(100)
y = rng.randn(100)
colors = rng.rand(100)
sizes = 1000 * rng.rand(100)
plt.scatter(x, y, c=colors, s=sizes, alpha=0.3, cmap='viridis')
plt.colorbar() # 显示颜色条

dGLe91.png

plt.scatter will spend more on make the picture more beautiful and accurate,so it costs more resource

errrorbar

1
2
3
4
x = np.linspace(0, 10, 50) 
dy = 0.8
y = np.sin(x) + dy * np.random.randn(50)
plt.errorbar(x, y, yerr=dy, fmt='.k');

dGLo59.png

different style:

1
plt.errorbar(x, y, yerr=dy, fmt='o', color='black', ecolor='lightgray', elinewidth=3, capsize=0)

Continuous error

fill_between

1
2
3
4
5
plt.plot(xdata, ydata, 'or') 
plt.plot(xfit, yfit, '-', color='gray')
plt.fill_between(xfit, yfit - dyfit, yfit + dyfit,
color='gray', alpha=0.2)
plt.xlim(0, 10)

dGxs1A.png

histogram

1
2
data = np.random.randn(1000) 
plt.hist(data)

dGzngA.png

other factors:

1
2
3
plt.hist(data, bins=30, normed=True, alpha=0.5, #alpha descides the level of transparence
histtype='stepfilled', #histtype='stepfilled' makes the transparence change as dofferet data
color='steelblue', edgecolor='none')

multivariate Gaussian distribution

1
2
3
4
5
6
7
mean = [0, 0] 
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T

plt.hist2d(x, y, bins=30, cmap='Blues')
cb = plt.colorbar()
cb.set_label('counts in bin')

dJCYd0.png

1
2
plt.hexbin(x, y, gridsize=30, cmap='Blues')  #change the squres to hexagon
cb = plt.colorbar(label='count in bin')

dJCwz4.png

configuration

1
2
3
4
5
6
7
8
9
10
11
12
import matplotlib.pyplot as plt 
plt.style.use('classic')

%matplotlib inline
import numpy as np

x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), '-b', label='Sine')
ax.plot(x, np.cos(x), '--r', label='Cosine')
ax.axis('equal')
leg = ax.legend()

dJC6dx.png

1
2
3
ax.legend(frameon=False, loc='lower center', ncol=2) #frameon controls the transparence of the box of label,loc controls the location,ncol control the number of columns

ax.legend(fancybox=True, framealpha=1, shadow=True, borderpad=1) #fancybox:filleted corner