折线图能直观的看出数据的变化
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 # 折线图 两地的气温变化 x = range(2, 26, 2) y_1 = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22, 18, 15] y_2 = [18, 15, 16, 17, 23, 25, 26, 27, 22, 27, 18, 18] plt.figure(figsize=(12, 8), dpi=80) plt.plot(x, y_1, label="广州") plt.plot(x, y_2, label="苏州") plt.legend(loc='best') plt.xlabel("时间") plt.ylabel("温度") plt.title("天气") plt.savefig("./sig.png") plt.show()
效果如下:
散点图反映出数据的分布
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 散点图 """ 三月份与十月份的天气""" y_3 = [11, 17, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 21, 14, 15, 15, 15, 18, 21, 22, 22, 22, 23, 23] y_10 = [26, 27, 28, 19, 21, 17, 16, 19, 18, 20, 19, 22, 23, 17, 20, 21, 22, 17, 11, 15, 5, 13, 17, 10, 11, 12, 12, 11, 12, 12, 6] x_3 = range(1, 32) x_10 = range(51, 82) # 设置图形大小 plt.figure(figsize=(20, 8), dpi=80) 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_labels = ["3月{}日".format(i) for i in x_3] _xtick_labels += ["10月{}日".format(i - 50) for i in x_10] plt.xticks(_x[::3], _xtick_labels[::3], rotation=45) # 添加图例 plt.legend(loc="upper left") # 添加描述信息 plt.xlabel("时间") plt.ylabel("温度") plt.title("标题") # 展示 plt.show()
效果如下:
条形统计图的特点:
- 能够使人们一眼看出各个数据的大小。
- 易于比较数据之间的差别。
- 能清楚的表示出数量的多少。
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 绘制条形图(竖型) x = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5\n:最后的骑士", "摔跤吧!爸爸", "加勒比海盗5\n:死无对证", "金刚:骷髅岛", "极限特工\n:终极回归", "生化危机6\n:终章", "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺", "金刚狼3\n:殊死一战", "蜘蛛侠\n:英雄归来", "悟空传", "银河护卫队2", "情圣", "新木乃伊"] y = [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] plt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(x)),y,width=0.3) #设置字符串到X轴 plt.xticks(range(len(x)),x,rotation = 90) plt.show()
效果如下:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 绘制条形图(横型) x = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5:最后的骑士", "摔跤吧!爸爸", "加勒比海盗5:死无对证", "金刚:骷髅岛", "极限特工:终极回归", "生化危机6:终章", "乘风破浪", "神偷奶爸3", "智取威虎山", "大闹天竺", "金刚狼3:殊死一战", "蜘蛛侠:英雄归来", "悟空传", "银河护卫队2", "情圣", "新木乃伊"] y = [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] plt.figure(figsize=(20, 8), dpi=80) plt.barh(range(len(x)), y, height=0.3, color = "orange") # 设置字符串到X轴 plt.yticks(range(len(x)), x) #绘制网格 plt.grid(alpha=0.3) plt.show()
效果如下:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 绘制多次条形图,电影三天的票房 a = ["猩球崛起3:终极之战", "敦刻尔克", "蜘蛛侠:英雄归来", "战狼2"] b_16 = [15754, 312, 4497, 319] b_15 = [12357, 156, 2045, 168] b_14 = [2358, 399, 2358, 362] bar_width = 0.2 x_14 = list(range(len(a))) x_15 = [i + bar_width for i in x_14] x_16 = [i + bar_width * 2 for i in x_14] # 设置图形大小 plt.figure(figsize=(20, 8), dpi=80) plt.bar(range(len(a)), b_14, width=bar_width, label="9月14日") plt.bar(x_15, b_15, width=bar_width, label="9月15日") plt.bar(x_16, b_16, width=bar_width, label="9月15日") # 设置图例 plt.legend() # 设置X轴刻度 plt.xticks(x_15, a) plt.show()
效果如下:
绘制直方图
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 绘制直方图 250部电影的时长 未经过统计的数据可绘制直方图 data = [172, 145, 175, 138, 161, 108, 108, 187, 140, 121, 158, 132, 150, 117, 181, 199, 185, 176, 113, 106, 169, 109, 134, 132, 150, 187, 166, 104, 104, 128, 148, 112, 176, 178, 148, 114, 136, 147, 126, 165, 163, 136, 111, 157, 196, 134, 180, 153, 175, 157, 104, 107, 156, 124, 120, 128, 142, 125, 104, 133, 183, 173, 180, 155, 164, 130, 168, 195, 151, 131, 149, 199, 197, 102, 110, 144, 125, 191, 112, 176, 117, 110, 178, 198, 100, 141, 196, 106, 106, 141, 114, 148, 167, 109, 166, 158, 138, 156, 169, 150, 186, 177, 144, 135, 135, 117, 181, 118, 189, 195, 153, 177, 113, 165, 119, 177, 151, 132, 189, 114, 116, 140, 199, 164, 142, 172, 119, 160, 101, 152, 196, 191, 189, 149, 133, 184, 133, 168, 174, 140, 116, 145, 117, 188, 124, 105, 179, 125, 136, 143, 143, 119, 144, 186, 142, 158, 125, 190, 168, 183, 188, 123, 124, 187, 197, 104, 190, 121, 170, 172, 175, 158, 194, 155, 126, 109, 182, 143, 198, 142, 119, 184, 100, 119, 182, 120, 185, 147, 148, 152, 158, 105, 148, 172, 179, 112, 131, 166, 133, 120, 154, 198, 191, 186, 113, 141, 138, 168, 143, 145, 107, 141, 101, 150, 128, 150, 169, 103, 151, 112, 155, 142, 164, 174, 149, 103, 169, 119, 163, 146, 149, 162, 196, 105, 192, 152, 195, 120, 198, 142, 100, 114, 122, 130, 200, 166, 168, 107, 197, 120] # 计算组距 d = 5 print(max(data) - min(data)) num_bins = (max(data) - min(data)) // d # 整除 要不然会出现错位 plt.figure(figsize=(20, 8), dpi=80) # num_bins平均分组,要是划分的组距不同,可以传一个组距的列表 # plt.hist(data, num_bins) # 频数分布直方图 plt.hist(data, num_bins, density=True) # 绘制频率分布直方图 # 设置x轴刻度 plt.xticks(range(min(data), max(data) + d, d)) plt.grid() plt.show()
频率直方图效果如下:
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 经过整理的数据想要绘制成直方图,用条形图代替 # x轴的数据 interval = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 60, 90] # 组距 width = [5, 5, 5, 5, 5, 5, 5, 5, 5, 15, 30, 60] quantity = [836, 2737, 3723, 3926, 3596, 1438, 3273, 642, 824, 613, 215, 47] plt.figure(figsize=(20, 8), dpi=80) plt.bar(range(len(interval)), quantity, width=1) #设置x轴刻度 _x = [i-0.5 for i in range(len(interval)+ 1)] _xtick_labels = interval + [150] #最后的一个间距是60, 90+60 plt.xticks(_x,_xtick_labels) plt.grid() plt.show()
效果如下:
更多绘图参考:https://matplotlib.org/stable/gallery/index.html