Python中的matplotlib绘制百分比堆叠柱状图,并为每一个类别设置不同的填充图案


Posted in Python onApril 20, 2022

通过Python中的matplotlib绘制百分比堆叠柱状图,并为每一个类别设置不同的填充图案。主要原因是有些论文打印出是黑白色的,不同类别之间区分不明显,所以做了这种方案。

存在一个问题:不知道如何根据填充图案设置图例,本文中可谓“曲线救国”,将图例的颜色块设置为了白色,所以如果有人知道如何根据hatching设置图例可以讨论,原始的legend方法中是未提供该类参数的。

图形如下:

Python中的matplotlib绘制百分比堆叠柱状图,并为每一个类别设置不同的填充图案

代码如下

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.ticker as mtick
from matplotlib.ticker import PercentFormatter

#设置填充的图案
marks = ['o','/','*','..','\\'] 
labels = [i for i in range(2010, 2021)]
#数据
first = [42.85,    41.15,39.41,35.35,35.53,30.45,29.81,31.85,32.41,30.42,31.49]
second = [23.20,26.40,27.77,29.02,32.30,35.40,36.42,35.95,35.45,34.00,31.93]
third = [14.08,12.99,12.51,11.54,11.70,12.27,12.69,11.81,10.63,9.98,9.95]
fourth = [16.14,16.17,17.34,21.53,17.66,19.36,18.40,17.83,19.15,23.09,24.10]
others = [3.73,3.28,2.98,2.57,2.81,2.53,2.67,2.57,2.36,2.51,2.54]
data = [first, second, third, fourth, others]

x = range(len(labels))

width = 0.35
# 将bottom_y元素都初始化为0
bottom_y = np.zeros(len(labels))
data = np.array(data)
# 为计算百分比做准备
sums = np.sum(data, axis=0)
j = 0
figsize = 8,6
figure, ax = plt.subplots(figsize=figsize)
plt.rcParams['font.sans-serif'] = ['SimHei']
for i in data:
    y = i / sums
    plt.bar(x, y, width, hatch=np.array(marks)[j], bottom=bottom_y, color='white', edgecolor='black')
    bottom_y = y + bottom_y
    plt.xticks(x, labels)
    #plt.yticks(range(1), ylabel)
    legend_labels = ['o legend1', '/ legend2', '* legend3', '· legend4',r'\ legend5']  
    color = ['white', 'white', 'white', 'white', 'white']
   
    patches = [mpatches.Patch(color=color[h],label="{:s}".format(legend_labels[h])) for h in range(len(legend_labels))]
    ax = plt.gca()
    box = ax.get_position()

    #纵轴设置为百分比
    plt.gca().yaxis.set_major_formatter(PercentFormatter(1))

    ax.legend(handles=patches,ncol=1, bbox_to_anchor=(1, 1), borderaxespad = 0.)  # 生成legend
    figure.subplots_adjust(right=0.7)
    j+=1
#绘制平行于x轴的虚线
for i in range(1, 11, 1):
    plt.axhline(y=i/10, linestyle='dashed', color='black', linewidth=0.5)
labels = ax.get_xticklabels() + ax.get_yticklabels()
#设置数字label字体
[label.set_fontname('Times New Roman') for label in labels]
plt.savefig(r'filename.svg', format='svg')
plt.show()

以上就是本文的全部内容,希望对大家的学习有所帮助。

Python 相关文章推荐
python中getaddrinfo()基本用法实例分析
Jun 28 Python
python脚本设置系统时间的两种方法
Feb 21 Python
Python的Flask框架标配模板引擎Jinja2的使用教程
Jul 12 Python
Python基于socket实现简单的即时通讯功能示例
Jan 16 Python
python实现二叉查找树实例代码
Feb 08 Python
Python实现平行坐标图的两种方法小结
Jul 04 Python
浅析PEP572: 海象运算符
Oct 15 Python
PyTorch加载预训练模型实例(pretrained)
Jan 17 Python
TensorFlow——Checkpoint为模型添加检查点的实例
Jan 21 Python
pip安装tensorflow的坑的解决
Apr 19 Python
Python return语句如何实现结果返回调用
Oct 15 Python
python实现Nao机器人的单目测距
Sep 04 Python
Pandas 数据编码的十种方法
Apr 20 #Python
Python读取和写入Excel数据
Python 的演示平台支持 WSGI 接口的应用
Apr 20 #Python
python​格式化字符串
Apr 20 #Python
Python编写冷笑话生成器
Apr 20 #Python
Python Django / Flask如何使用Elasticsearch
Apr 19 #Python
python中mongodb包操作数据库
Apr 19 #Python
You might like
PHP原理之异常机制深入分析
2010/08/08 PHP
php读取二进制流(C语言结构体struct数据文件)的深入解析
2013/06/13 PHP
浅析linux下apache服务器的配置和管理
2013/08/10 PHP
PHP实现的增强性mhash函数
2015/05/27 PHP
PHP代码判断设备是手机还是平板电脑(两种方法)
2015/10/19 PHP
laravel中的错误与日志用法详解
2016/07/26 PHP
php打开本地exe程序,js打开本地exe应用程序,并传递相关参数方法
2018/02/06 PHP
PHP如何通过带尾指针的链表实现'队列'
2020/10/22 PHP
让网页根据不同IE版本显示不同的内容
2009/02/08 Javascript
JavaScript定义类或函数的几种方式小结
2011/01/09 Javascript
JavaScript对象之间的转换 jQuery对象和原声DOM
2011/03/07 Javascript
jQuery JSON实现无刷新三级联动实例探讨
2013/05/28 Javascript
JS实现的打字机效果完整实例
2016/06/20 Javascript
JS中判断null的方法分析
2016/11/21 Javascript
jQuery实现遍历XML节点和属性的方法示例
2018/04/29 jQuery
详解基于vue的服务端渲染框架NUXT
2018/06/20 Javascript
vue权限路由实现的方法示例总结
2018/07/29 Javascript
vue实现简易图片左右旋转,上一张,下一张组件案例
2020/07/31 Javascript
PyQT5 QTableView显示绑定数据的实例详解
2019/06/25 Python
python 求某条线上特定x值或y值的点坐标方法
2019/07/09 Python
PyQt5实现简单的计算器
2020/05/30 Python
Keras模型转成tensorflow的.pb操作
2020/07/06 Python
基于PyTorch中view的用法说明
2021/03/03 Python
欧舒丹美国官网:L’Occitane美国
2018/02/23 全球购物
医校毕业生自我鉴定
2014/01/25 职场文书
中餐厅经理岗位职责
2014/04/11 职场文书
校庆标语集锦
2014/06/25 职场文书
厨师长岗位职责范本
2014/08/25 职场文书
2014迎国庆标语大全
2014/09/19 职场文书
婚前协议书范本两则
2014/10/16 职场文书
死亡赔偿协议书
2015/01/28 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
酒店总经理岗位职责
2015/04/01 职场文书
信用卡工作证明范本
2015/06/19 职场文书
2016教师读书思廉心得体会
2016/01/23 职场文书
《别在吃苦的年纪选择安逸》读后感3篇
2019/11/30 职场文书