Fork me on GitHub

matplotlib画scatter图(绘画dice系数散点图)

import re
import math
import matplotlib.pyplot as plt


def set_chinese():
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False


def get_cor():
    x = []  # 第几张图
    y = []  # dice系数是多少
    with open("results.txt", "r", encoding="utf-8") as f:
        for line in f:  # 遍历每一行
            number = re.findall(r'\d+(?:\.\d+)?', line)
            x.append(int(number[0]))
            y.append(float(number[1]))
    f.close()
    return x, y


def draw_pic(x, dice):
    set_chinese()
    plt.title("dice系数在样本中的分布图")
    plt.ylabel("dice index")
    plt.xlabel("第几个样本")
    plt.scatter(x, dice, s=50)
    plt.savefig("dice index.png", dpi=200)
    plt.show()


def run():
    x, dice = get_cor()
    draw_pic(x, dice)


if __name__ == '__main__':
    run()
pass

posted @ 2020-05-08 12:51  WalterJ726  阅读(176)  评论(0编辑  收藏  举报