Python绘制百分比堆叠柱状图并填充图案

作者:HuyCui 时间:2023-01-25 00:49:06 

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

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

图形如下:

Python绘制百分比堆叠柱状图并填充图案

代码如下

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()

来源:https://blog.csdn.net/Horizonhui/article/details/123671358

标签:python,柱状图,填充
0
投稿

猜你喜欢

  • selenium python浏览器多窗口处理代码示例

    2023-11-20 07:09:29
  • update.where无索引导致MySQL死锁问题解决

    2024-01-28 01:21:25
  • 通过优化CSS代码 减小对系统资源的占用

    2010-08-03 12:33:00
  • Thinking XML: 创建 XML 的好建议

    2008-05-29 11:25:00
  • SQL Server数据库安装时常见问题解决方案集锦

    2024-01-19 05:05:57
  • python 从远程服务器下载日志文件的程序

    2021-03-06 01:39:15
  • PyTorch如何创建自己的数据集

    2022-10-17 05:22:17
  • mysql密码中有特殊字符&在命令行下登录的操作

    2024-01-17 22:29:18
  • python如何支持并发方法详解

    2021-05-29 16:50:17
  • 浅析Python打包时包含静态文件处理方法

    2023-05-29 01:12:10
  • 利用Python找回微信撤回信息

    2022-11-21 22:34:03
  • iscroll碰到Select无法选择下拉刷新的解决办法

    2024-05-11 09:33:35
  • 细数JavaScript 一个等号,两个等号,三个等号的区别

    2023-08-25 08:22:09
  • Vue.js 2.5新特性介绍(推荐)

    2024-05-13 09:09:00
  • linux 后台日志 mysql 错误异常的解释(推荐)

    2024-01-26 06:01:42
  • 精致的web设计

    2009-12-04 19:07:00
  • 仅允许指定的机器连接SQL Server服务器

    2010-07-22 19:54:00
  • 用Python实现斐波那契(Fibonacci)函数

    2023-06-29 19:59:07
  • 微信小程序如何实现radio单选框单击打勾和取消

    2024-06-17 20:41:19
  • python判断正负数方式

    2023-07-06 11:16:12
  • asp之家 网络编程 m.aspxhome.com