【339】matplotlib based on python3

Ref: python3 的 matplotlib绘图库的使用

Ref: python matplotlib绘图设置坐标轴刻度、文本

Ref: python中matplotlib的颜色及线条控制

Ref: 图文并茂的Python散点图教程

举例:机器学习实战教程(一):K-近邻算法(史诗级干货长文)

from matplotlib.font_manager import FontProperties
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
import numpy as np

def file2matrix(fileName):
    fr = open(fileName)
    arrayOfLines = fr.readlines()
    numberOfLines = len(arrayOfLines)
    returnMat = np.zeros((numberOfLines, 3))
    classLabelVector = []
    index = 0
    
    for line in arrayOfLines:
        line = line.strip()
        listFromLine = line.split('	')
        # 不包括3,所以就是前三个
        returnMat[index, :] = listFromLine[0:3]
        if listFromLine[-1] == 'didntLike':
            classLabelVector.append(1)
        elif listFromLine[-1] == 'smallDoses':
            classLabelVector.append(2)
        elif listFromLine[-1] == 'largeDoses':
            classLabelVector.append(3)
        index += 1
    return returnMat, classLabelVector

def showData(datingDatMat, datingLabels):
    
    fig, axs = plt.subplots(nrows=2, ncols=2, sharex=False, sharey=False, figsize=(13, 8))
    
    numberOfLabels = len(datingLabels)
    LabelsColors = []
    
    for i in datingLabels:
        if i == 1:
            LabelsColors.append('green')
        if i == 2:
            LabelsColors.append('blue')
        if i == 3:
            LabelsColors.append('red')
    
    # fig 1
    # 颜色是一个 list,对应每一组数据有一个颜色对应
    axs[0][0].scatter(x=datingDatMat[:,0], y=datingDatMat[:,1], color=LabelsColors, s=15, alpha=0.5)
    
    axs0_title_text = axs[0][0].set_title('Fight Hours & Video Game Percentage')
    axs0_xlabel_text = axs[0][0].set_xlabel('Flight Hours')
    axs0_ylabel_text = axs[0][0].set_ylabel('Video Game Percentage')
    
    plt.setp(axs0_title_text, size=14, color='red')
    plt.setp(axs0_xlabel_text, size=10, color='brown')
    plt.setp(axs0_ylabel_text, size=10, color= 'brown')
    
    # fig 2
    axs[0][1].scatter(x=datingDatMat[:,0], y=datingDatMat[:,2], color=LabelsColors, s=15, alpha=0.5)
    
    axs1_title_text = axs[0][1].set_title('Fight Hours & Ice Cream Weight')
    axs1_xlabel_text = axs[0][1].set_xlabel('Flight Hours')
    axs1_ylabel_text = axs[0][1].set_ylabel('Ice Cream Weight')
    
    plt.setp(axs1_title_text, size=14,  color='red')
    plt.setp(axs1_xlabel_text, size=10, color='brown')
    plt.setp(axs1_ylabel_text, size=10, color= 'brown')
    
    # fig 3
    axs[1][0].scatter(x=datingDatMat[:,1], y=datingDatMat[:,2], color=LabelsColors, s=15, alpha=0.5)
    
    axs2_title_text = axs[1][0].set_title('Video Game Percentage & Ice Cream Weight')
    axs2_xlabel_text = axs[1][0].set_xlabel('Video Game Percentage')
    axs2_ylabel_text = axs[1][0].set_ylabel('Ice Cream Weight')
    
    plt.setp(axs2_title_text, size=14,  color='red')
    plt.setp(axs2_xlabel_text, size=10, color='brown')
    plt.setp(axs2_ylabel_text, size=10, color= 'brown')
    
    # legend
    didntlike = mlines.Line2D([], [], color='green', marker='.', markersize=6, label='didntLike')
    smallDoses = mlines.Line2D([], [], color='blue', marker='.', markersize=6, label='smallDoses')
    largeDoses = mlines.Line2D([], [], color='red', marker='.', markersize=6, label='largeDoses')
    
    # Add legend
    axs[0][0].legend(handles=[didntlike, smallDoses, largeDoses])
    axs[0][1].legend(handles=[didntlike, smallDoses, largeDoses])
    axs[1][0].legend(handles=[didntlike, smallDoses, largeDoses])

    plt.tight_layout()
    plt.show()
    
if __name__ == '__main__':
    fileName = 'datingTestSet.txt'
    datingDataMat, datingLabels = file2matrix(fileName)
    showData(datingDataMat, datingLabels)

原文地址:https://www.cnblogs.com/alex-bn-lee/p/9952195.html