python 百分比堆积图

数据组织格式如下:

1.导入数据:

# -*- coding: utf-8 -*-
"""
Created on Sun Jan  7 22:12:06 2024

@author: Administrator
"""
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.pyplot import MultipleLocator
from matplotlib.ticker import FixedLocator
# In[]
data = pd.read_excel(r"I:\111.xlsx")
a = np.array(list(data.iloc[1])[1:])*100
b = np.array(list(data.iloc[2])[1:])*100
c = np.array(list(data.iloc[3])[1:])*100
d = np.array(list(data.iloc[4])[1:])*100
e = np.array(list(data.iloc[5])[1:])*100
all_data = [a,b,c,d,e]

2.设置坐标轴

#设置显示情况
# In[]
fig, ax = plt.subplots(figsize = (16,9), dpi = 300)    #图像横纵比例为12:6,分辨率为500dpi
# plt.rcParams['font.sans-serif']=['Times New Roman']    #设置显示字体为新罗马
plt.margins(x= 0)    #设置横坐标刻度起始位置,起始位置为0
plt.ylim(0,100,20)    #设置纵坐标最大值与间隔,最大值为100,间隔为20
plt.xlim(0,41.5,13)    #设置横坐标最大值与间隔,最大值为21,间隔为1
plt.rcParams["ytick.direction"] = "in"   #设置纵坐标刻度朝向
ax.xaxis.set_major_locator(MultipleLocator(15))
ax.xaxis.set_minor_locator(FixedLocator([0,9,17,25,33,41.5]))   #指定横坐标次坐标的刻度值
ax.tick_params(axis="x", direction="out", which="minor", length=40, width = 1)    #设置次坐标朝向、长度、宽度
# ax.xaxis.set_minor_locator(FixedLocator([0,1.5]))   #指定横坐标次坐标的刻度值
# ax.tick_params(axis="x", direction="out", which="minor", length=10, width = 1)    #设置次坐标朝向、长度、宽度
ax.tick_params(axis="x", direction="in", which="major", length=0.1)   #缩小柱坐标刻度长度
x_tick = [i+2 for i in range(40) if i % 2==0]
x_label = ["1","2","3","4"]*5
plt.yticks(ticks = [0,20,40,60,80,100], labels = ["0%","20%","40%","60%","80%","100%"],fontsize = 20,family='Times New Roman')#设置纵坐标刻度标签
plt.xticks(ticks = x_tick, labels = x_label,fontsize = 20)   #设置横坐标刻度标签
plt.grid(axis='y',alpha=0.7,ls='--')

3.绘制堆积图

# In[绘制堆积图]
###########比例图标
y_loc = [2,10,45,87,95]
num = 0
for i in all_data:
    k = 0
    for j in i:
        ax.text(x_tick[k]-0.83,y_loc[num],str(round(j, 1))+'%',fontsize=16)
        k = k + 1
    num = num + 1
###########年份
width = 1.5
ax.text(4,-10, '$\mathrm{2003}$年',fontsize = 20)
ax.text(12,-10, '$\mathrm{2004}$年',fontsize = 20)
ax.text(20,-10, '$\mathrm{2005}$年',fontsize = 20)
ax.text(28,-10, '$\mathrm{2006}$年',fontsize = 20)
ax.text(36,-10, '$\mathrm{2007}$年',fontsize = 20)
ax.bar(x_tick, a, width,color='paleturquoise', label = "1")
ax.bar(x_tick, b, width,color='darkturquoise',bottom = a, label = "2")
ax.bar(x_tick, c ,width,color='lightpink',bottom =a + b, label = "3")
ax.bar(x_tick, d, width,color='orangered',bottom =a + b + c, label = "4")
ax.bar(x_tick, e, width,color='darkorange',bottom =a + b + c +d, label = "5")
ax.set_xlabel("\n (a) 百分比堆积图",fontsize = "20")    #设置横坐标名称
ax.set_ylabel("占比",fontsize = "22")    #设置纵坐标名称
#设置图例格式:框外(1,1)的位置,边框不显示,间隔为1.5,图例条长度为0.5
ax.legend(bbox_to_anchor = (1,1),fontsize = "20",frameon = False,labelspacing = 1.5, handlelength = 0.75)
plt.show()

效果如下:

标题
  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值