笔记-科研画图

画图是难以避免的事情啊,很多时候都大费周章,算是造了几个小轮子,以后就在这里更新咯,先放个教程:

  1. Matplotlib可视化最有价值的50个图表(附完整Python源代码)

  2. (原)python中matplotlib的颜色及线条控制

MM2019笔记

热力图

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
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.colors as colors

def myplot(rating_path, prediction_path, i):
ax = axs[i]
ax.set_xticks([])
ax.set_yticks([])
if i == len(y_labels)-1:
ax.set_xlabel(x_label, fontsize=font_size)
ax.set_ylabel(ylabels[i], fontsize=font_size)
data = list()
with open(prediction_path, 'r') as pred:
with open(rating_path, 'r') as rating:
for pline, rline in zip(pred, rating):
plist = [int(_) for _ in pline.split(' ')]
rlist = [int(_) for _ in rline.split(' ')]
data_user = []
for item in rlist[:k]:
if item in plist[:k]:
data_user.append((k - plist.index(item)) / k)
else:
data_user.append(0)
data.append(data_user)

data = np.array(data[:N]).T
# average = np.average(data, axis=-1)
# averages.append(average)
im = ax.imshow(data, cmap=mymap)
return im

# averages = list()
clist = ['#ffffff', '#f7fcf0', '#e0f3db', '#ccebc5', '#a8ddb5', '#7bccc4', '#4eb3d3', '#2b8cbe', '#2c7bb6', '#0868ac']
y_labels = ['A', 'B', 'C']
mymap = colors.ListedColormap(clist, 'indexed')
k, N = 10, 100
x_label = '%d users' % N
font_size = 20

fig, axs = plt.subplots(len(y_labels), 1, figsize=(14, 7))
fig.subplots_adjust(
top=0.975,
bottom=0.028,
left=0.058,
right=0.917,
hspace=0.0,
wspace=0.2
)
im1 = myplot('rating_A.txt', 'prediction_A.txt', 0)
im2 = myplot('rating_B.txt', 'prediction_B.txt', 1)
im3 = myplot('rating_C.txt', 'prediction_C.txt', 2)
cb = fig.colorbar(im1, cax=fig.add_axes([0.93, 0.09, 0.02, 0.82]))
cb.ax.tick_params(labelsize=20)

# plt.savefig('heatmap.png', bbox_inches='tight', dpi=400)
plt.show()
# np.savetxt('averages_70.txt', np.array(averages))

折线图上的异常点

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
import numpy as np
from matplotlib import pyplot as plt
import math

def nor_plot(xs, ys, marker, hstyle, c, neg_c, label, neg_label):
nor_ys = (ys - np.min(ys)) / (np.max(ys) - np.min(ys))
pos_s, neg_s = 20, 70
for i, (x, nor_y) in enumerate(zip(xs, nor_ys)):
if i < 10:
plt.scatter(x, nor_y, c=c, s=pos_s)
else:
if nor_y > min(nor_ys[:10]):
neg = plt.scatter(x, nor_y, c=neg_c, marker=marker, s=neg_s, label=neg_label)
else:
plt.scatter(x, nor_y, c=c, s=pos_s)
line, = plt.plot(xs, nor_ys, c=c, label=label, linewidth=2.5)
plt.axhline(min(nor_ys[:10]), linestyle=hstyle, c=c)
return line, neg

n = 70
data = np.loadtxt('averages_70.txt')
# print(data)
x = [_ for _ in range(1, n + 1)]

plt.figure()
plt.subplots_adjust(
top=0.957,
bottom=0.081,
left=0.118,
right=0.963,
hspace=0.2,
wspace=0.2
)
plt.xlim((1, 70))
plt.xticks(fontsize=12)
plt.ylim((0, 1))
plt.yticks(fontsize=12)

plt.text(3.3, 0.92, "Positive\nsamples", fontsize=9)
plt.arrow(10, 0.9, -6, 0, width=0.01, head_width=0.02, head_length=0.4, overhang=0.7)
plt.text(10.4, 0.86, "Negative\nsamples", fontsize=9)
plt.arrow(10, 0.84, 6, 0, width=0.01, head_width=0.02, head_length=0.4, overhang=0.7)
plt.axvline(10, linestyle='--', c='black')
plt.ylabel('Normalized Rating', fontsize=16)
crgan, crgan_neg = nor_plot(x, data[0][:n], '*', '-.', '#1f77b4', 'r', 'CRGAN', 'Abnormal points of CRGAN')
rsvd, rsvd_neg = nor_plot(x, data[2][:n], '*', '-.', 'coral', 'b', 'RegularizedSVD', 'Abnormal points of RegularizedSVD')

plt.legend(handles=[crgan, rsvd, crgan_neg, rsvd_neg], fontsize=12)
plt.show()
# plt.savefig('../abnormal_point.png', bbox_inches='tight', dpi=400)

seaborn柱状图,根据值指定颜色

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
import numpy as np
import matplotlib.colors as colors
import seaborn as sns
from matplotlib import pyplot as plt
import pandas as pd

mean_list = read_data()
clist = ['#ff0000', '#ff3333', '#ff6666', '#ff9999', '#ffcccc', '#ffffff', '#e5f0f9', '#cce2f2', '#b2d4ec', '#99c6e6'][::-1]
mymap = colors.ListedColormap(clist, 'indexed')

sns.set_style("whitegrid")
preferences = {
'epoch': np.array(epochs),
'mean of margin': np.array(mean_list)
}
preferences = pd.DataFrame(preferences)
plt.figure()
plt.xlim((0, 15))
pal = [
'#f7fcf0', '#e0f3db', '#ccebc5', '#a8ddb5',
'#7bccc4', '#4eb3d3', '#2b8cbe', '#2c7bb6',
'#2c7bb6', '#2c7bb6', '#2c7bb6', '#2c7bb6',
'#2c7bb6', '#2c7bb6', '#0868ac', '#0868ac',
]
rank = np.array(mean_list).argsort().argsort()
sns.barplot(x="epoch", y="mean of margin", data=preferences, palette=np.array(pal)[rank])
plt.show()

多折线图,单legend

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
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

models = ['CRGAN (Ours)', 'IRGAN', 'AltSVM', 'Global Ranking', 'Primal-CR', 'Primal-CR++', 'CoFiRank', 'RegularizedSVD', 'RankBasedSVD', 'LCR', 'MLP', 'GMF', 'NeuMF']

data = get_data()
x = [10 * _ for _ in range(7, 21)]
K = 10
metrics = [matric + str(K) for metric in ['HR@', "P@", "NDCG@", "AUC@", "MAP@", "MRR@"]]

linestyles = ['--', '-', '-.', ':'] * 3 + [':',]
markers = ['*', '+', 'p', 'v', '.', ','] * 2 + ['o', ]

m, n = 3, 2
font_size = 10
fig, axs = plt.subplots(m, n, figsize=(6, 9))
plt.subplots_adjust(
top=0.981,
bottom=0.204,
left=0.125,
right=0.953,
hspace=0.392,
wspace=0.432
)
titles = ['(%s) %s' % ( chr(ord('a') + _), metric) for (_, metric) in enumerate(metrics)]

for i in range(m):
for j in range(n):
ax = axs[i][j]
index = i * n + j
ax.grid(color='black', linestyle='--', linewidth=0.5, alpha=0.3, axis='y')
ax.set_xlabel('$M$',fontsize=font_size)
ax.set_ylabel(zhibiaos[index],fontsize=font_size)
ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: '%.2f' % x))
ax.set_title(titles[index], position=(0.5, -0.35), fontname="Times New Roman", fontweight="black")
lines, points = list(), list()
for _, model in enumerate(models):
y = data[model][index][5:]
line, = ax.plot(x, y, linewidth=2, linestyle=linestyles[_])
point = ax.scatter(x, y, s=13, marker=markers[_])
ax.set_xlim((x[0], x[-1]))
lines.append(line)
points.append(point)

fig.legend([ _ for _ in zip(points, lines)], models, loc='lower center', ncol=3, fontsize=font_size)

# plt.show()
fig.savefig('metrics.png', dpi=300, bbox_inches='tight')

Joypy画分布变化图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import joypy
from matplotlib import pyplot as plt
from matplotlib import cm
import matplotlib.colors as colors
import pandas as pd
import numpy as np

def new_cm():
# color_list = ['#FF8C00', '#FFDEAD']
color_list = ['#1E90FF', '#66E2DF']
return colors.LinearSegmentedColormap.from_list('cmap', color_list, 32)

len_epochs = 10
epochs = [i * 30 for i in range(len_epochs)]
data = pd.DataFrame(np.random.normal(0, 0.7, size=[100, len_epochs]) + np.random.uniform(-2.5, 2.5, size=[100, len_epochs])
)


fig, axes = joypy.joyplot(data, #kind="normalized_counts",
bins=30, range_style='all', overlap=1, linewidth=1, colormap=new_cm(), fade=True, grid=False, ylabelsize=0,linecolor='black', figsize=(4, 5), x_range=[-5, 5])
# plt.show()
plt.savefig('distribution.png', dpi=500)

这个也可以用tensorflow画,不过不推荐,参见笔记-tensorflow

网络结构图

推荐一个画网络结构图的网站,太爽啦:

http://alexlenail.me/NN-SVG/LeNet.html#

MM2020笔记

双参数对结果的影响

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
import pandas as pd
from matplotlib import pyplot as plt
import matplotlib.colors as colors
from math import floor


def read_data(file_name):
pass


data = read_data('result.txt')
max_data, min_data = data.stack().max(), data.stack().min()

plt.figure(figsize=(8, 8))
left, bottom, width = 0.18, 0.18, 0.56
margin, colorbar_margin = 0.01, 0.08
bar_height, colorbar_width, title_hight = 0.2, 0.05, 0.04
font_size = 20
font_size_tick = 14
clist = ['#0868AC', '#2C7BB6', '#2B8CBE', '#4EB3D3', '#7BCCC4', '#A8DDB5', '#CCEBC5', '#E0F3DB', '#F7FCF0'][::-1]
mymap = colors.LinearSegmentedColormap.from_list('mymap', clist)

# 画热力图
ax = plt.axes([left, bottom, width, width])
heatmap = ax.imshow(data[:][::-1], cmap=mymap)
xticks = ['0.05', '0.10', '0.15', '0.20', '0.25']
ax.set_xticks(range(len(xticks)))
ax.set_xticklabels(xticks, fontsize=font_size_tick)
ax.set_xlabel('***', fontsize=font_size)
yticks = ['0.05', '0.10', '0.15', '0.20', '0.25'][::-1]
ax.set_yticks(range(len(yticks)))
ax.set_yticklabels(yticks, fontsize=font_size_tick)
ax.set_ylabel('***', fontsize=font_size)

# 画x柱状图
ax = plt.axes([left, bottom + width + margin, width, bar_height])
x = list(data.columns)
y = list(data.mean(axis=0))
ax.bar(x, y, 0.05, zorder=2,
color=[clist[int((num - min_data) / (max_data - min_data) * len(clist))] for num in y])
ax.grid(linestyle=':', linewidth=1.3, zorder=1)
ax.set_xticks([])
ax.set_xlim((0.025, 0.275))
ax.set_ylim((0.8075, 0.82))
ax.set_ylabel('***', fontsize=font_size)
ax.tick_params(axis='y', labelsize=font_size_tick)

# 画y柱状图
ax = plt.axes([left + width + margin, bottom, bar_height, width])
x = list(data.columns)
y = list(data.mean(axis=1))
ax.barh(x, y, 0.05, zorder=2,
color=[clist[int((num - min_data) / (max_data - min_data) * len(clist))] for num in y])
ax.grid(linestyle=':', linewidth=1.3, zorder=1)
ax.set_yticks([])
ax.set_ylim((0.025, 0.275))
ax.set_xlim((0.8075, 0.82))
ax.set_xlabel('***', fontsize=font_size)
ax.tick_params(axis='x', labelsize=font_size_tick)

# 画指示bar
cb = plt.colorbar(heatmap,
cax=plt.axes([left + width + colorbar_margin, bottom + width + margin, colorbar_width, bar_height]))
cb.ax.tick_params(labelsize=font_size_tick)

# 标题
ax = plt.axes([0.3, 0, 0.3, title_hight])
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.text(0.5, 0.5, '***', fontsize=font_size * 1.3, family='Times New Roman')

# plt.show()
plt.savefig('***.png', dpi=300)

双指标关系的折线图

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
import matplotlib.pyplot as plt

def get_data(file_name):
pass

dataset = 'ML100K'
font_size_label, font_size_tick, font_size_legend = 20, 16, 17
num_x = 30
xs = [ _ for _ in range(num_x)]
metric_1 = get_data('***')
metric_2 = get_data('***')

fig = plt.figure(figsize=(6.75, 4.5))
plt.subplots_adjust(
top=0.949,
bottom=0.166,
left=0.149,
right=0.865,
hspace=0.392,
wspace=0.432
)

# 画metric_1
ax1 = fig.add_subplot(111)
ax1.set_xlabel('***', fontsize=font_size_label)
ax1.tick_params(axis='x', labelsize=font_size_tick)
ax1.set_ylabel('***', fontsize=font_size_label)
ax1.tick_params(axis='y', labelsize=font_size_tick)
ax1.plot(xs, metric_1, label='***')
ax1.scatter(xs, metric_2, s=60, marker='.')
ax1.grid(axis='y', linestyle=':', linewidth=1.8)
ax1.set_ylim((0.697, 0.755))

# 画metric_2
ax2 = ax1.twinx()
ax2.plot(xs, metric_2, linewidth=1.8, label='***', linestyle='-', color='orange')
ax2.scatter(xs, metric_2, s=20, marker='^', color='orange')
ax2.tick_params(axis='y', labelsize=font_size_tick)
ax2.set_xlim((0, num_x))

lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines1 + lines2, labels1 + labels2, loc=0, fontsize=font_size_legend)

# plt.show()
plt.savefig('***.png', dpi=300)