标准库系列:matplotlib

TODO

搬运整理:https://www.cnblogs.com/brillant-ordinary/p/11387018.html
颜色 https://blog.csdn.net/guoxinian/article/details/80242353
宋体配置 https://blog.csdn.net/Mr_sdhm/article/details/111498153?utm_medium=distribute.pc_relevant.none-task-blog-2defaultBlogCommendFromBaidudefault-7.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2defaultBlogCommendFromBaidudefault-7.control

官网 https://matplotlib.org/

画框框

x_corners = l / 2 * np.array([1,  1,  1,  1, -1, -1, -1, -1])
y_corners = w / 2 * np.array([1, -1, -1,  1,  1, -1, -1,  1])
z_corners = h / 2 * np.array([1,  1, -1, -1,  1,  1, -1, -1])
corners = np.vstack((x_corners, y_corners, z_corners))
rot_matrix = np.matrix([
    [cos(a),    -sin(a),    0],
    [sin(a),     cos(a),    0],
    [     0,          0,    1],
])
corners = np.dot(rot_matrix, corners)
corners[0, :] = corners[0, :] + x
corners[1, :] = corners[1, :] + y
corners[2, :] = corners[2, :] + z

成熟代码

普通绘图

import matplotlib.pyplot as plt
plt.figure(figsize=(10,10)) #画布大小
for iIndex in range(4):
    plt.subplot(2,2,iIndex+1)
    for jIndex in range(5):
        bite = data_dic[method_dic[jIndex]][video_dic[iIndex]][:,0]/1000
        EWPSNR = data_dic[method_dic[jIndex]][video_dic[iIndex]][:,1]
        plt.plot(bite, EWPSNR, c = color[jIndex], marker='x', label=method_dic[jIndex]) # 蓝色的线
    plt.subplots_adjust(wspace=0,hspace=0)# 调节多张图的间距
    plt.grid(True)
    plt.axis('tight')
    plt.xlabel('bites(kbps)')
    plt.ylabel('EWPSNR(dB)')
    plt.title(video_dic[iIndex])
    plt.legend(loc='best')
plt.show()
figname = f"{json_addr}/draw/{jIndex}.png"
plt.savefig(figname)# 图片存储到指定地址
plt.close()#一定记得close,否则会在同一个图上反复画

数据分割

from matplotlib import pyplot as plt
import numpy as np
def split(arr, cond):
    return [arr[cond], arr[~cond]]

def data_split(data_true,data_false,num,thre,side,drop_true=np.array([]),drop_false=np.array([])):
    if side == 'left':
        shift = 0
    elif side == 'right':
        shift = 1
    else:
        raise KeyError     

    ans_true, ans_false = split(data_true,data_true[:,num]<thre), split(data_false,data_false[:,num]<thre)
    print(f"FN    : {len(ans_true[1-shift])/len(data_true)}, FP    : {len(ans_false[shift])/len(data_false)}")
    if drop_true.size == 0:
        drop_true,drop_false=ans_true[1-shift], ans_false[1-shift]
    else:
        drop_true  = np.concatenate([drop_true ,ans_true [1-shift]],axis=0)
        drop_false = np.concatenate([drop_false,ans_false[1-shift]],axis=0)
    return ans_true[shift],ans_false[shift],drop_true,drop_false

num = 7
thre = 0.5
side = 'left'

data_true,data_false = ori_true,ori_false

data_true_tmp,data_false_tmp,drop_true_tmp,drop_false_tmp = data_split(data_true,data_false,num,thre,side)

plt.figure(figsize=(18, 12))  # 画布大小
# plt.hist(data_true[:, num], facecolor="blue", alpha=0.5)
# # plt.hist(data_false[:, num], facecolor="red", alpha=0.5)
# plt.show()
print(f"TP now: {len(data_true_tmp)/len(ori_true)}, FP now: {len(data_false_tmp)/len(ori_false)}")
for iIndex in range(12):
    plt.subplot(4, 6, iIndex + 1)
    plt.title(f"{iIndex}: {feat_name_list[iIndex]}")
    plt.hist(data_true_tmp[:, iIndex], facecolor="blue", alpha=0.5)
#     plt.hist(drop_false[:, iIndex], facecolor="red", alpha=0.5)
    plt.subplot(4, 6, iIndex + 13)
    plt.title(f"{iIndex}: {feat_name_list[iIndex]}")
    plt.hist(data_false_tmp[:, iIndex], facecolor="red", alpha=0.5)
#     plt.hist(drop_true[:, iIndex], facecolor="blue", alpha=0.5)
    plt.subplots_adjust(wspace=0.2, hspace=0.2)  # 调节多张图的间距

data_true = data_true_tmp
data_false = data_false_tmp
drop_true = drop_true_tmp
drop_false = drop_false_tmp

image

posted @ 2021-07-19 11:10  小康要好好学习  阅读(70)  评论(0编辑  收藏  举报