1.折线图常用的一些技巧:
(1)调整图片大小:
from matplotlib import pyplot as plt x = [i for i in range(0, 61)] y = [random.randint(2,16) for i in range(0, 61)] plt.figure(figsize=(6,4), dpi=100) plt.plot(x,y) plt.show()
figsize可以调整图像大小,dpi的值越大图像越清晰
(2)调整坐标轴显示的坐标数据密集度和坐标倾斜:
如图X坐标太过密集,可以通过plt.xticks()调整,rotation可以使坐标值倾斜
from matplotlib import pyplot as plt x = [i for i in range(0, 61,1)] y = [random.randint(2,16) for i in range(0, 61)] plt.xticks(x[::5], rotation = 45) plt.plot(x,y) plt.show()
(3)显示汉字,matplotlib默认不显示汉字,font_manager可以显示汉字:
from matplotlib import pyplot as plt from matplotlib import font_manager import random t = [random.randint(20,35) for i in range(120)] per_minute = [i for i in range(0, 120)] x_xticks = ["10点{}分".format(i) for i in range(60)] x_xticks += ["11点{}分".format(i) for i in range(60)] my_font = font_manager.FontProperties(fname = "C:/Windows/Fonts/simsun.ttc") plt.figure(figsize =(20, 8), dpi = 100) plt.plot(per_minute, t) x_ticks = plt.xticks(per_minute[::5], x_xticks[::5], rotation = 45, fontproperties=my_font)
(4)显示网格:plt.grid()
from matplotlib import pyplot as plt x = [i for i in range(0, 61,1)] y = [random.randint(2,16) for i in range(0, 61)] plt.xticks(x[::5], rotation = 45) plt.grid(alpha = 0.5) plt.plot(x,y) plt.show()
(5)显示多个折线:
from matplotlib import pyplot as plt import random from matplotlib import font_manager x = [i for i in range(31)] y1 = [random.randint(2,10) for i in range(31)] y2 = [random.randint(2,8) for i in range(31)] # 显示汉字 my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/simsun.ttc") # 调整图片大小和清晰度 plt.figure(figsize=(15,6), dpi = 60) # 显示网格 plt.grid(alpha = 0.5) plt.plot(x,y1, label = "y1的图例", color = "orange",linestyle = "--") plt.plot(x,y2, label = "y2的图例", color = "cyan",linestyle = "-.") # 添加图例,必须在plot的后面 plt.legend(prop = my_font, loc = "upper left") plt.show()
2.绘制散点图:
from matplotlib import pyplot as plt from matplotlib import font_manager y3 = [10, 15, 14, 16, 16, 17, 13, 18, 20, 18, 20, 21, 22, 22, 20, 22, 24, 25, 26, 23, 22, 26, 26, 27, 28, 25, 26, 24, 22, 26, 28] y10 = [35, 34, 33, 35, 32, 30, 32, 30, 28, 27, 24, 26, 23, 25, 28, 24, 23, 22, 22, 20, 22, 24, 20, 18, 19, 16, 16, 18, 15, 15, 12] x3 = [i for i in range(1,32)] x10 = [i for i in range(35, 66)] # 调整图像的大小和清晰度 plt.figure(figsize=(15, 6), dpi = 80) # 显示中文 my_font = font_manager.FontProperties(fname = "C:/Windows/Fonts/simsun.ttc") # 调整坐标轴标签 x_ticks = ["3月{}日".format(i) for i in range(1,32)] x_ticks += ["10月{}日".format(i) for i in range(1,32)] x = x3 + x10 plt.xticks(x[::3], x_ticks[::3], rotation = 45, FontProperties = my_font) # 绘制散点图 plt.scatter(x1, y3, label = "3月份", color = "cyan") plt.scatter(x10, y10, label = "10月份", color = "green") # 显示图例 plt.legend(prop = my_font, loc = "upper left") # 添加坐标轴的描述 plt.xlabel("时间", fontproperties = my_font) plt.ylabel("温度", fontproperties = my_font) # 展示绘制的图 plt.show()
4.绘制条形图
from matplotlib import pyplot as plt from matplotlib import font_manager a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"] b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23] # 显示汉字 my_font = font_manager.FontProperties(fname = "C:/Windows/Fonts/simsun.ttc") # 调整图片大小 plt.figure(figsize=(20,8), dpi = 80) # 画柱状图 plt.bar(range(len(a)), b, width = 0.3) # X轴标签 plt.xticks(range(len(a)), a, fontproperties = my_font, rotation = 90) plt.show()
由于汉字竖着不好看,所以,在汉字太长时,可以选择横向的条形图barh(),代码如下:
from matplotlib import pyplot as plt from matplotlib import font_manager a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"] b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23] # 显示汉字 my_font = font_manager.FontProperties(fname = "C:/Windows/Fonts/simsun.ttc") # 调整图片大小 plt.figure(figsize=(20,8), dpi = 80) # 画柱状图 plt.barh(range(len(a)), b, height=0.5, color = "cyan") # 画网格 plt.grid(alpha = 0.3) # 坐标轴描述 plt.xlabel("票房(亿)", fontproperties = my_font) plt.ylabel("电影", fontproperties = my_font) # X轴标签 plt.yticks(range(len(a)), a, fontproperties = my_font) plt.show()
5.绘制直方图
from matplotlib import pyplot as plt from matplotlib import font_manager a=[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150] bin_width = 3 num_bins = int((max(a)-min(a))/bin_width) plt.figure(figsize=(20,8), dpi = 80) plt.hist(a, num_bins, color = "cyan") plt.xticks(range(min(a),max(a)+ bin_width, bin_width)) plt.grid() plt.show()
更多图形可以观看echarts 或者ploty