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解释执行原理分析
Aug 22 Python
python Django批量导入数据
Mar 25 Python
Python实现从log日志中提取ip的方法【正则提取】
Mar 31 Python
python删除字符串中指定字符的方法
Aug 13 Python
python列表使用实现名字管理系统
Jan 30 Python
Python read函数按字节(字符)读取文件的实现
Jul 03 Python
pytorch获取模型某一层参数名及参数值方式
Dec 30 Python
Python使用configparser库读取配置文件
Feb 22 Python
python GUI库图形界面开发之PyQt5信号与槽基本操作
Feb 25 Python
Python调用shell cmd方法代码示例解析
Jun 18 Python
QT5 Designer 打不开的问题及解决方法
Aug 20 Python
Python实现加密的RAR文件解压的方法(密码已知)
Sep 11 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
Smarty+QUICKFORM小小演示
2007/02/25 PHP
PHP与Java进行通信的实现方法
2013/10/21 PHP
php使用curl通过代理获取数据的实现方法
2016/05/16 PHP
PHP实现求解最长公共子串问题的方法
2017/11/17 PHP
php给数组赋值的实例方法
2019/09/26 PHP
让html页面不缓存js的实现方法
2014/10/31 Javascript
推荐10个2014年最佳的jQuery视频插件
2014/11/12 Javascript
浅析创建javascript对象的方法
2016/05/13 Javascript
Bootstrap打造一个左侧折叠菜单的系统模板(一)
2016/05/17 Javascript
Bootstrap基本插件学习笔记之模态对话框(16)
2016/12/08 Javascript
微信小程序开发之相册选择和拍照详解及实例代码
2017/02/22 Javascript
vue toggle做一个点击切换class(实例讲解)
2018/03/13 Javascript
详解angular部署到iis出现404解决方案
2018/08/14 Javascript
React key值的作用和使用详解
2018/08/23 Javascript
微信公众号H5之微信分享常见错误和问题(小结)
2019/11/14 Javascript
微信小程序实现单个卡片左滑显示按钮并防止上下滑动干扰功能
2019/12/06 Javascript
JS实现移动端可折叠导航菜单(现代都市风)
2020/07/07 Javascript
使用js和canvas实现时钟效果
2020/09/08 Javascript
python实现计算倒数的方法
2015/07/11 Python
Python使用中文正则表达式匹配指定中文字符串的方法示例
2017/01/20 Python
Python栈算法的实现与简单应用示例
2017/11/01 Python
人机交互程序 python实现人机对话
2017/11/14 Python
Django中使用 Closure Table 储存无限分级数据
2019/06/06 Python
python开启debug模式的方法
2019/06/27 Python
选择Python写网络爬虫的优势和理由
2019/07/07 Python
Python自定义聚合函数merge与transform区别详解
2020/05/26 Python
python如何使用腾讯云发送短信
2020/09/17 Python
python开发一款翻译工具
2020/10/10 Python
自动化专业职业生涯规划书范文
2014/01/16 职场文书
工资收入证明
2014/10/07 职场文书
个人总结与自我评价
2015/02/14 职场文书
搬迁通知
2015/04/20 职场文书
开学第一天的感想
2015/08/10 职场文书
教师节感想
2015/08/11 职场文书
python执行js代码的方法
2021/05/13 Python
mysql5.7的安装及Navicate长久免费使用的实现过程
2021/11/17 MySQL