【Python_绘图】堆积柱形图

成图效果

【Python_绘图】堆积柱形图_第1张图片 堆积柱形图示例

 涉及内容

  • 数据堆积

#关键代码
y1 = (0,0,0,30.54,32.67,38.29,81.51)
y2 = (87.83,85.07,92.14,57.29,52.4,53.85,14.55)

d = []
for i in range(0, len(y2)):
    sum = y1[i] + y2[i]
    d.append(sum)
y3 = (12.17,14.93,7.86,12.17,14.93,7.86,3.94)
  • 横向底部图例

#关键代码
plt.legend((p1[0], p2[0], p3[0]), ('精确匹配', '粗匹配', '未匹配'),ncol=3,bbox_to_anchor=(0.5, -0.1), loc=8, borderaxespad=-3,frameon=False)
  • 图片显示不完整

#关键代码(通过保存解决)
fig.savefig('pipeilv.jpg',bbox_inches='tight')
  •  中文字体与英文字体

#关键代码(简便方法:折中找一个既像宋体,又像Times New Roman)
config = {
            "font.family": 'STSong',
            "font.size": 12,
            "mathtext.fontset": 'stix',
            "font.serif": ['STSong'],
         }
rcParams.update(config)

重要参考

Python 画简单堆叠柱状图 (数据可视化) - 程序员大本营 (pianshen.com)

完整代码

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from matplotlib import rcParams
config = {
            "font.family": 'STSong',
            "font.size": 12,
            "mathtext.fontset": 'stix',
            "font.serif": ['STSong'],
         }
rcParams.update(config)

fig=plt.figure(figsize=(8,4))#调整画布大小

N = 7
y1 = (15,17.22,19.63,30.24,34.49,40.84,70.5)
y2 = (17.14,14.26,15.71,25.08,22.85,23.61,23.5)

d = []
for i in range(0, len(y2)):
    sum = y1[i] + y2[i]
    d.append(sum)
y3 = (30.3,39.8,32.97,22.9,20,20.45,6)

d2 = []
for i in range(0, len(y2)):
    sum = y1[i] + y2[i] +y3[i]
    d2.append(sum)
y4=(28.36,19.82,24.49,12.98,12.94,10,0)

d3 = []
for i in range(0, len(y2)):
    sum = y1[i] + y2[i] +y3[i] +y4[i]
    d3.append(sum)
y5=(9.2,8.9,7.2,8.8,9.72,5.1,0)
# menStd = (2, 3, 4, 1, 2)
# womenStd = (3, 5, 2, 3, 3)
x = (1,2,3,4,5,6,7)  # the x locations for the groups
width = 0.35  # the width of the bars: can also be len(x) sequence

p1 = plt.barh(x, y1, width, color='#d62728')  # , yerr=menStd)
p2 = plt.barh(x, y2, width, left=y1)  # , yerr=womenStd)
p3 = plt.barh(x, y3, width, left=d)
p4=  plt.barh(x, y4, width, left=d2)
p5=  plt.barh(x, y5, width, left=d3,color='#7B7B7B')

plt.xlabel('位置精度区间分布(%)')
plt.ylabel('定位方式')
#plt.title('Scores by group and gender')
plt.yticks(x, ('百度 _G','腾讯 _G','高德 _G','百度  _S','腾讯  _S','高德  _S','所提方法'))
plt.xticks(np.arange(0, 110, 10))

plt.grid(which='major',axis='x',linestyle='dotted')

#plt.legend((p1[0], p2[0], p3[0],p4[0], p5[0]), ('[0,50 m]', '(50 m, 200 m]', '(200 m, 500 m]','(500 m, 1000 m]','(1000 m, ∞)'))
plt.legend((p1[0], p2[0], p3[0],p4[0], p5[0]), ('[0,50 m]', '(50 m, 200 m]', '(200 m, 500 m]','(500 m, 1000 m]','(1000 m, ∞)'),ncol=5,bbox_to_anchor=(0.5, -0.1), loc=8, borderaxespad=-3,frameon=False,handlelength=0.7)



plt.show()
fig.savefig('jingdu81.jpg',bbox_inches='tight')

你可能感兴趣的:(Python,python,作图)