01柱状图
from pylab import * # 这两行代码,专门用来显示汉子问题 mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 这两行代码,专门用来显示汉子问题 x1 = np.arange(2004, 2017) y1 = [970279, 1259308, 1127571, 1163959, 1169540, 1076938, 991350, 953275, 951508, 904434, 889381, 864015, 836236, ] y1 = np.array(y1) # y2 = [1435, 3402, 3339, 3669, 2802, 3783, 3000, 2840, 2662, 2576, 2240, 2280, 2465] plt.figure(1) plt.bar(x1, y1 / 10000, 0.6, color="coral") plt.title("发病人数与时间的关系") plt.xlabel(u"年份") plt.ylabel(u"发病人数") plt.text(2001.5, 127, "单位:万人") # 在图表任何位置添加一些注释或者说明 plt.show()
02折线图
# """ # @author:Zhao # @ide:PyCharm # @createTime:2019-05-26 # """ import numpy as np from pylab import * # 这两行代码,专门用来显示汉子问题 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 这两行代码,专门用来显示汉子问题 x1 = np.arange(2004, 2017) x2 = np.arange(2004, 2020) y1 = [970279, 1259308, 1127571, 1163959, 1169540, 1076938, 991350, 953275, 951508, 904434, 889381, 864015, 836236, ] y1 = np.array(y1) y2 = [1041613.1, 1144841.3, 1148142.4, 1155784.4, 1163656.7, 1120077.3, 1049225.1, 992511.8, 965732.8, 931802.3, 906496.6, 882183.2, 856137.6, 807201.4, 776392.1, 743850.4] y2 = np.array(y2) y3 = [970280, 1228788, 1187444, 1146911, 1107177, 1068200, 1029999, 992514, 955770, 919738, 884407, 849761, 815788, 749800, 717800, 686360] y3 = np.array(y3) plt.figure(1) plt.plot(x1, y1 / 10000, label='实际发病人数', c='r') plt.scatter(x1, y1 / 10000, c='r') plt.plot(x2, y2 / 10000, label='三次指数平滑预测发病人数') plt.scatter(x2, y2 / 10000) plt.plot(x2, y3 / 10000, label='灰色模型预测发病人数', c='g') plt.scatter(x2, y3 / 10000, c='g') plt.title("发病人数与时间的变化趋势") plt.xlabel(u"年份") plt.ylabel(u"发病人数") plt.legend() plt.xticks(range(2004, 2021, 2)) plt.text(2001, 127, "单位:万人") plt.show()
03饼状图
import pandas as pd import numpy as np from collections import Counter from pylab import * # 这两行代码,专门用来显示汉子问题 mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 这两行代码,专门用来显示汉子问题 import heapq def plot_pie(x, y): colors = 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'red', 'orange', 'blue', 'yellow', 'green', explode = 0, 0, 0, 0, 0, 0, 0, 0, 0, plt.pie(y, explode=explode, labels=x, colors=colors, autopct='%1.1f%%', shadow=False, startangle=50) plt.axis('equal') plt.show() if __name__ == '__main__': result = [586320., 88228., 50480., 44760., 40888., 32516., 24806., 22952., 47127] x = '农民', '家务及待业', '工人', '学生', '离退人员', '其他', '民工', '不详', '其余职业' plot_pie(x, result)
04双坐标轴的折线图
import numpy as np import matplotlib.pyplot as plt from matplotlib import rc rc('mathtext', default='regular') plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 这两行代码,专门用来显示汉子问题 guangDong = [5664.37, 9485.99, 16153.25, 24834.65, 31551.37] heNan = [5633.24, 9333.4, 15967.61, 24791.83, 32665.38] siChuan = [1677.8, 2884.11, 4602.16, 8086.86, 11776.73] guangDong = np.array(guangDong) heNan = np.array(heNan) siChuan = np.array(siChuan) guangDongFB = [42883.0, 66049.0, 56753.0, 59090.0, 51157.0] heNanFB = [59744.0, 64005.0, 48479.0, 48045.0, 43712.0] siChuanFB = [46728.0, 63842.0, 49049.0, 46783.0, 46117.0] guangDongFB = np.array(guangDongFB) heNanFB = np.array(heNanFB) siChuanFB = np.array(siChuanFB) x = np.arange(2004, 2017, 3) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, guangDong / 10000, c='red', label='湖南GDP') ax.plot(x, heNan / 10000, c='blue', label='湖北GDP') ax.plot(x, siChuan / 10000, c='orange', label='贵州GDP') ax2 = ax.twinx() ax2.plot(x, guangDongFB / 10000, '--', c="red", label='湖南发病人数') ax2.plot(x, heNanFB / 10000, '--', c="blue", label='湖北发病人数') ax2.plot(x, siChuanFB / 10000, '--', c="orange", label='贵州发病人数') ax.legend(loc=0) ax.grid() ax.set_xlabel("时间") ax.set_ylabel("GDP") ax2.set_ylabel("发病人数") # ax.set_ylim(-20, 100) ax2.legend(loc=0) # plt.savefig('0.png') plt.text(2001.5, 6.65, "单位:万亿元") plt.text(2016.6, 6.65, "单位:万人") plt.show()