matplotlib学习笔记

matplotlib学习笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import random
import numpy as np

#中文字体
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")

#折线图
x = np.arange(120)
y = [random.uniform(20,35) for _ in range(120)]
plt.plot(x,y,color='green',linestyle='-',linewidth=2,alpha=0.5)
_xticks = [f"10点{i}分" for i in x if i < 60]
_xticks.extend([f'11点{i-60}分' for i in x if i >= 60])
plt.xticks(x[::5],_xticks[::5],rotation=90)
plt.xlabel('时间')
plt.ylabel('temperature')
plt.title('hello')
plt.show()

#多个折线图
plt.plot(range(len(x)), x, label='self', linestyle='--',color='red')
plt.plot(range(len(x)), y, label='others', linestyle='-', color='blue')
plt.legend(loc='upper right')
plt.legend(loc='upper left')

#保存图片
plt.savefig('./sig.png')

#网格
plt.grid(alpha=0.1)

#散点图
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]

x_3 = range(1,32)
x_10 = range(52,83)

plt.figure(figsize=(20,8))
plt.scatter(x_3, y_3, label='3月',color='r')
plt.scatter(x_10, y_10, label='10月',color='g')

x_ = list(x_3) + list(x_10)
xticks_ = [f'3月{i}号' for i in x_3]
xticks_.extend([f'10月{i-51}号' for i in x_10])
plt.xticks(x_[::3],xticks_[::3], rotation=45)
plt.xlabel('xlabel')
plt.legend(loc='best')
plt.show()

#条形图
a = ["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]

_x = range(len(a))
_bar_width=0.2
plt.bar(_x, b_14, label='9月14日',color='r',width=_bar_width)
plt.bar([i + _bar_width for i in _x], b_15, label='15',color='b', width=_bar_width )
plt.bar([i + 2 * _bar_width for i in _x], b_16, label='16',color='g',width=_bar_width)

plt.xlabel('电影')
plt.ylabel('票房')
plt.xticks(_x,a)
plt.legend()

#直方图
a=[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
bin_width = 3
num_bins = int((max(a)-min(a))/bin_width)
plt.hist(a, num_bins)

plt.xticks(list(range(min(a),max(a)))[::bin_width],rotation=90)
plt.grid(True, alpha=0.5, linestyle='--')
plt.show()

深度学习中的可视化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#在第一个valid batch中选出前几张图片展示
def im_convert(tensor):
image = tensor.to('cpu').clone().detach()
image = image.numpy().squeeze()
image = image.transpose(1,2,0)
image = image.clip(0,1)
return image

fig = plt.figure(figsize=(20,12))
columns = 4
rows = 2
X,y = next(iter(valid_iter))
for idx in range(columns*rows):
ax = fig.add_subplot(rows, columns, idx+1,xticks=[],yticks=[])
# ax.set_xticks([])
# ax.set_yticks([])
ax.set_title(num_to_class[int(y[idx])])
plt.imshow(im_convert(X[idx]))
plt.show()
1
2
3
4
5
6
7
8
9
10
11
12
#统计每个label出现的次数并绘图
def barw(ax):
for p in ax.patches:
val = p.get_width()
x = p.get_x() + p.get_width()
y = p.get_y() + p.get_height()
ax.annotate(round(val, 2),(x, y))

plt.figure(figsize=(15,30))
ax0=sns.countplot(y=labels_df['label'],order=labels_df['label'].value_counts().index)
barw(ax0)
plt.show()