1.饼形图
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #解决中文乱码 labels=['a','b','c','d'] sizes=[5,6,7,8] colors = ['red','yellowgreen','lightskyblue','yellow'] #每块颜色定义 explode=0.1,0.1,0.1,0.1 patches,text1,text2 = plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.1f%%',shadow=True,startangle=90,pctdistance = 0.6) #patches饼图的返回值,texts1饼图外label的文本,texts2饼图内部的文本 # x,y轴刻度设置一致,保证饼图为圆形 plt.axis('equal') plt.show()
labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置
autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数
shadow,饼是否有阴影
startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
pctdistance,百分比的text离圆心的距离
2.柱状图
单列柱状图:
# -*- coding: utf-8 -*- import matplotlib.pyplot as plt name_list = ['Monday','Tuesday','Friday','Sunday'] num_list = [1.5,0.6,7.8,6] plt.bar(range(len(num_list)), num_list,color='rgb',tick_label=name_list) plt.show()
双列柱状图:
#-*- coding:utf-8 -*- import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决中文乱码 plt.figure() fontsize = 30 plt.title(u'初始数据模型效果', fontsize=fontsize) plt.xlabel(u'算法', fontsize=fontsize) plt.ylabel(u'准确率', fontsize=fontsize) plt.ylim(0.021, 0.0245) name_list = ['LR', 'RF', 'GBDT', 'XGBoost', 'LightGBM', 'XGB+GBM'] num_list = [0.0228, 0.022, 0.0237, 0.024, 0.0236, 0.0241] num_list1 = [0.0232, 0.0225, 0.024, 0.0243, 0.0242, 0.0243] # 每个x坐标的位置 x = [1,2.5,4,5.5,7,8.5] total_width, n = 1, 2 width = total_width / n # edgecolor参数设置图形边框颜色 plt.bar(x, num_list, width=width, label='Public Score', fc='gray',edgecolor='black') for i in range(len(x)): x[i] = x[i] + width # hatch可以将图形填充至条形图内 plt.bar(x, num_list1, width=width, label='Private Score', tick_label=name_list, fc='black', hatch='....') plt.xticks(rotation=0, fontsize=fontsize) plt.yticks(fontsize=fontsize) # 图例设置在左上角 plt.legend(loc='upper left', fontsize=fontsize) plt.show() plt.close()
3.折线图
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #解决中文乱码 plt.figure() x3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y3=[30,31,31,32,33,35,35,40,47,62] x4 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y4=[32,32,32,33,34,34,34,34,38,43] group_labels = ['64k', '128k','256k','512k','1024k','2048k','4096k','8M','16M','32M','64M','128M','256M','512M'] plt.title('broadcast(b) vs join(r)') plt.xlabel('data size') plt.ylabel('time(s)') plt.plot(x3, y3, label='broadcast') plt.plot(x4, y4,label='join') plt.xticks(x3, group_labels, rotation=0) # X轴与名称相对应,名字不进行旋转 plt.legend(bbox_to_anchor=[0.3, 1]) # 显示label名字 plt.grid(linestyle='--') # 画虚线网格 plt.show() plt.savefig("a.png")