绘制组合图:
组合图就是将多个形状,组合到⼀个图形中,主要作⽤是节约作图的空间,节省读者的时间,从⽽提⾼
信息传达的效率。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import pandas as pd import numpy as np import matplotlib.pyplot as plt def plot_combination1(): sale = pd.read_excel( './data/每月目标销售额和实际销售额.xlsx' ,header = 0 ,index_col = 0 ) # 设置正常显示中文标签 plt.rcParams[ 'font.sans-serif' ] = [ 'SimHei' ] # 正常显示负号 plt.rcParams[ 'axes.unicode_minus' ] = False # 设置字体大小 plt.rcParams.update({ 'font.size' : 16 }) # 提取数据 x = np.arange( 12 ) + 1 y1 = sale.目标销售额 y2 = sale.实际销售额 # 计算目标完成率 y3 = y2 / y1 # float # print(y3) 1月 1.120000 2月 0.887500 3月 1.118182 4月 1.150000 """ 第一种方式:是⽤两个不同颜⾊的柱⼦,分别展示每个⽉的实际销售额和⽬标销售额, ⽤折线图展示⽬标完成率。 左边的主坐标轴是柱形图对应的数据,右边的次坐标轴是折线图对应的 数据,下边的横坐标轴表示细分的维度,⽐如时间、地区、渠道等。 """ plt.figure(figsize = ( 16 , 8 )) plt.subplot( 111 ) # 柱形宽度 bar_width = 0.35 # 在主坐标轴绘制柱形图 plt.bar(x,y1,bar_width,label = '目标销售额' ) plt.bar(x + bar_width,y2,bar_width,label = '实际销售额' ) # 设置坐标轴的取值范围,避免柱子过高而与图例重叠 plt.ylim( 0 ,max(y1.max(),y2.max()) * 1.2 ) # 设置图例 plt.legend(loc = 'upper left' ) # 设置横坐标的标签 plt.xticks(x) # plt.set_xticklabels(sale.index) # 在次坐标轴上绘制折线图 plt.twinx() # ls:线的类型,lw:宽度,o:在顶点处实心圈 plt.plot(x,y3,ls = '-' ,lw = 2 ,color = 'r' ,marker = 'o' ,label = '目标完成率' ) # 设置次坐标轴的取值范围,避免折线图波动过大 plt.ylim( 0 , 1.35 ) # 设置图例 plt.legend() # 定义显示百分号的函数 def to_percent(number, position = 0 ): return '%.f' % (number * 100 ) + '%' # 次坐标轴的标签显示百分号 FuncFormatter:自定义格式函数包 from matplotlib.ticker import FuncFormatter plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent)) # 设置标题 plt.title( '\n每月销售目标达成情况\n' ,fontsize = 36 ,loc = 'center' ,color = 'k' ) plt.show() def plot_combination2(): """ 第二种方式:是⽤两条不同颜⾊的折线,分别展示每个⽉的实际销售额和⽬标销售额,再⽤两种不同颜 ⾊的柱形图展示实际与⽬标的差额,绿⾊代表完成⽬标,红⾊代表没有完成⽬标, 这种组合图不需要⽤到两个纵坐标轴, """ import pandas as pd import numpy as np import matplotlib.pyplot as plt # 设置正常显示中⽂标签 plt.rcParams[ 'font.sans-serif' ] = [ 'SimHei' ] # 正常显示负号 plt.rcParams[ 'axes.unicode_minus' ] = False # 设置字体⼤⼩ plt.rcParams.update({ 'font.size' : 16 }) # 从 Excel ⽂件中读取数据,第⼀列设置为索引 sale = pd.read_excel( './data/每月目标销售额和实际销售额.xlsx' , index_col = 0 ) # 提取数据 # print('index') x = sale.index # Index(['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'], dtype='object', name='month') # print(x) y1 = sale.目标销售额 y2 = sale.实际销售额 # 计算差额 y3 = y2 - y1 # 绘制折线图 plt.figure(figsize = ( 16 , 8 )) plt.subplot( 111 ) plt.plot(x, y1, ls = '-' , lw = 2 , label = '目标销售额' ) plt.plot(x, y2, ls = '--' , lw = 2 , label = '实际销售额' ) # ⽤列表推导式定义柱⼦的颜⾊,绿⾊代表完成⽬标, 红⾊代表没有完成⽬标 color = [ 'g' if i > 0 else '#dc5034' for i in y3] # 绘制柱形图 plt.bar(x, y3, color = color, label = '差额' ) # 设置图例 plt.legend(loc = 'upper left' ) # 设置标题 title = '\n每月销售目标达成情况\n' plt.title(title, fontsize = 36 , loc = 'center' , color = 'k' ) plt.show() if __name__ = = '__main__' : plot_combination1() plot_combination2() |
绘制结果:
第一种
第二种:
参考书目:
以上就是Python绘制组合图的示例的详细内容,更多关于Python绘制组合图的资料请关注自学编程网其它相关文章!
- 本文固定链接: https://zxbcw.cn/post/195880/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)