散点图绘制
from matplotlib import pyplot as plt import random from matplotlib import font_manager myfont = font_manager.FontProperties(fname="C:WindowsFontssimkai.ttf") # x轴显示日期 x_3 = range(1,32) x_10 = range(51,82) # 设置一个0~40随机温度显示在y轴 y_3 = [random.randint(0,40) for i in range(1,32)] y_10 = [random.randint(0,40) for i in range(1,32)] # 设置图形大小 plt.figure(figsize=(20, 8) ,dpi=80)
# 散点图用scatter plt.scatter(x_3,y_3,label= "3月份") plt.scatter(x_10, y_10, label = "10月份") # 调整x的刻度 _x = list(x_3)+list(x_10) _xtick = ["3月{}日".format(i) for i in x_3] _xtick +=["10月{}日".format(i-50) for i in x_10] plt.xticks(_x[::3], _xtick[::3] , rotation= 40, fontproperties= myfont) # 设置标题 plt.xlabel("期日", fontproperties = myfont) plt.ylabel("温度", fontproperties = myfont) plt.title("三月份和十月份温度", fontproperties = myfont) # 设置图形lebel显示位置 plt.legend(loc = 2,prop = myfont) plt.show()
条形图绘制
from matplotlib import pyplot as plt import random from matplotlib import font_manager myfont = font_manager.FontProperties(fname="C:WindowsFontssimkai.ttf") a = ["我的女朋友","你的女朋友", "他的女朋友"] b = [random.randint(0,100) for i in range(3)] # 竖着 # plt.bar(range(len(a)),b,width=0.25) # 横着 plt.barh(range(len(a)),b,height= 0.25) plt.yticks(range(len(a)),a,fontproperties = myfont) plt.show()
多条条形统计图
from matplotlib import pyplot as plt from matplotlib import font_manager import random myfont = font_manager.FontProperties(fname="C:WindowsFontssimkai.ttf") a = ["我的女朋友","你的女朋友", "他的女朋友"] # 随机生成十个以内的女朋友 b_2017 = [random.randint(0,10) for i in range(3)] b_2018 = [random.randint(0,10) for i in range(3)] b_2019 = [random.randint(0,10) for i in range(3)] # 每个宽度0.2 bar_with = 0.2 # 设置2017年的x坐标 b_x2017 = list(range(len(b_2017))) # 设置2018年的x坐标 b_x2018 = [i + bar_with for i in b_x2017] # 设置2019年x的坐标 b_x2019 = [i + bar_with*2 for i in b_x2017] # 将x的坐标引入进去,不能一样否则会重叠 plt.bar(b_x2017, b_2017,width=bar_with, label = "2017年") plt.bar(b_x2018, b_2018,width=bar_with,label = "2018年") plt.bar(b_x2019, b_2019,width=bar_with,label = "2019年") plt.legend(prop= myfont) # 设置x的刻度 plt.xticks(b_x2018,a,fontproperties = myfont) plt.ylabel("单位:个",fontproperties = myfont,rotation = 0) plt.title("你我他每年的女朋友" , fontproperties = myfont) plt.show()