- 折线图
x=[2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021] y=[61.76,123.21,183.10,202.59,245.21,252.67,299.28,334.44,360.51,380.13,410.31] #ax1 = plt.subplots() # 设置图框的大小 fig = plt.figure(figsize=(10,5)) # 绘图--总体 plt.plot(x, # x轴数据 y, # y轴数据 linestyle = '-', # 折线类型 marker = 'o', # 点的形状 label = '总体', color = 'black', markeredgecolor='black',# 添加标签 ) # 添加标题和坐标轴标签 plt.ylabel('平均消费倾向(%)') plt.title('福建省数字普惠金融总指数发展') plt.xticks(x) # 显示图例 plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.4), ncol=3)# loc下方居中,bbox位置,ncol y的数量 for i in range(len(x)): plt.text(x[i]-0.2,y[i]+10,y[i],ha="center") # 显示图形 plt.show()
#导入库 import numpy as np import matplotlib.pyplot as plt import fontTools #windows设置字体 #plt.rcParams['font.sans-serif']=['SimHei'] #plt.rcParams['axes.unicode_minus']=False #macOS设置字体 plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] plt.rcParams['axes.unicode_minus'] = False x=[2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021] y=[77.54491018,79.93928927,77.96682062,75.65042219,74.20091324,73.04766734,70.71685304,70.44479843,71.07479784,67.5393796,69.94761307] z=[66.89284137,66.27339155,72.9892809,72.2739405,70.68369647,69.43410896,66.61367657,66.81940125,67.83428321,64.64588634,66.37074697] w=[74.09727759,74.26507475,87.55808856,87.39130435,86.71065033,86.07907194,85.72390572,83.85051344,83.2021668,78.25191571,83.04274829] # 设置图框的大小 fig = plt.figure(figsize=(6,3)) # 绘图--总体 plt.plot(x, # x轴数据 y, # y轴数据 linestyle = '-', # 折线类型 marker = 'o', # 点的形状 label = '总体', color = 'red', markeredgecolor='black') # 添加标签 # 绘图--城镇 plt.plot(x, # x轴数据 z, # y轴数据 linestyle = '--', # 折线类型 marker = 'o', # 点的形状 label = '城镇', color = 'black', markeredgecolor='black') # 添加标签 # 绘图--农村 plt.plot(x, # x轴数据 w, # y轴数据 linestyle = ':' ,# 折线类型 marker = 'o', # 点的形状 label = '农村', color = 'green', markeredgecolor='black') # 添加标签 # 添加标题和坐标轴标签 plt.ylabel('消费倾向值') plt.title('福建省城乡居民消费倾向') plt.xticks(x,color='blue',rotation=15) # 显示图例 plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.3), ncol=3)# loc下方居中,bbox位置,ncol y的数量 for i in range(len(x)): plt.text(x[i],53,round(y[i],3),ha="center",fontsize=9) plt.text(x[i],50,round(z[i],3),ha="center",fontsize=9) plt.text(x[i],47,round(w[i],3),ha="center",fontsize=9) plt.text(x[i]-11.5,53,'总体',ha="center",fontsize=12,fontweight='bold') #fontweight 字体粗细 plt.text(x[i]-11.5,50,'城镇',ha="center",fontsize=12,fontweight='bold') plt.text(x[i]-11.5,47,'农村',ha="center",fontsize=12,fontweight='bold') # 显示图形 plt.show()
- 柱形图
x=[1,2,3,4,5,6,7,8] y=[326.01,309.10,294.93,316.46,311.35,318.61,301.99,343.34] labels=['福州市','龙岩市','南平市','宁德市','莆田市','泉州市','三明市','厦门市'] plt.bar(x,y,align="center",width=0.6,tick_label=labels,ec='gray') plt.xlabel(u"城市") plt.ylabel(u"总指数/%") plt.title('2021年福建省各市数字普惠金融总指数分布') #展示数据,参数为x坐标,y坐标,值,位置 for i in range(len(x)): plt.text(x[i],y[i]+3,y[i],ha="center")
def drawHistogram_1(): listDate=[2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021] list1=[16661,18593,20564,22204,23520,25006,25980,28145,30946,30487,33942] list2=[6505,7402,9986,11055,11960,12911,14003,14943,16281,16339,19290] length = len(list1) x = np.arange(length) # 横坐标范围 plt.figure() total_width, n = 0.8, 2 # 柱状图总宽度,有几组数据 width = total_width / n # 单个柱状图的宽度 x1 = x - width / 2 # 第一组数据柱状图横坐标起始位置 x2 = x1 + width # 第二组数据柱状图横坐标起始位置 plt.title("福建省城乡居民人均消费支出") # 柱状图标题 # plt.xlabel("星期") # 横坐标label 此处可以不添加 plt.ylabel("支出(元)") # 纵坐标label plt.bar(x1, list1, width=width, label="城镇") plt.bar(x2, list2, width=width, label="农村") plt.xticks(x, listDate) # 用星期几替换横坐标x的值 plt.legend() # 给出图例 for a, b in zip(x1, list1): plt.text(a, b + 0.1, '%.0f' % b, ha='center', va='bottom', fontsize=7) for a, b in zip(x2, list2): plt.text(a, b + 0.1, '%.0f' % b, ha='center', va='bottom', fontsize=7) plt.show() if __name__ == '__main__': drawHistogram_1()
- 柱形图和折线图
x=[2012,2014,2016,2018] z=[99.69,179.75,230.41,300.21] y=[12035,15686,19969,22586] fig, ax1 = plt.subplots() ax2 = ax1.twinx() #使用twinx添加y轴的坐标轴 ax1.plot(x, y, linestyle = '-', # 折线类型 marker = 's', # 点的形状 label = '人均消费支出', color = 'black', markeredgecolor='black') ax2.bar(x, z,label='数字普惠金融总指数')#柱状图 plt.xticks(x) #设置刻度范围 ax1.set_ylim(0,25000) ax2.set_ylim(0,350) # 显示图例 ax1.legend(loc='lower center', bbox_to_anchor=(0.65, -0.15))# loc下方居中,bbox位置,ncol y的数量 ax2.legend(loc='lower center', bbox_to_anchor=(0.3, -0.15)) #展示数据,参数为x坐标,y坐标,值,位置 for i in range(len(x)): ax1.text(x[i]-0.2,y[i]+300,y[i],ha="center") ax2.text(x[i],z[i]+2,z[i],ha="center") # 设置网格线 ax1.grid(True, axis='y') plt.show()