Python繪制百分比堆疊柱狀圖并填充圖案
通過Python中的matplotlib繪制百分比堆疊柱狀圖,并為每一個類別設(shè)置不同的填充圖案。主要原因是有些論文打印出是黑白色的,不同類別之間區(qū)分不明顯,所以做了這種方案。
存在一個問題:不知道如何根據(jù)填充圖案設(shè)置圖例,本文中可謂“曲線救國”,將圖例的顏色塊設(shè)置為了白色,所以如果有人知道如何根據(jù)hatching設(shè)置圖例可以討論,原始的legend方法中是未提供該類參數(shù)的。
圖形如下:

代碼如下
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.ticker as mtick
from matplotlib.ticker import PercentFormatter
#設(shè)置填充的圖案
marks = ['o','/','*','..','\\']?
labels = [i for i in range(2010, 2021)]
#數(shù)據(jù)
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)
# 為計算百分比做準(zhǔn)備
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()
? ? #縱軸設(shè)置為百分比
? ? 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()
#設(shè)置數(shù)字label字體
[label.set_fontname('Times New Roman') for label in labels]
plt.savefig(r'filename.svg', format='svg')
plt.show()以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch::Dataloader中的迭代器和生成器應(yīng)用詳解
這篇文章主要介紹了pytorch::Dataloader中的迭代器和生成器應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Python中的http.server庫用法詳細(xì)介紹
這篇文章主要給大家介紹了關(guān)于Python中http.server庫用法的相關(guān)資料,http.server是Python標(biāo)準(zhǔn)庫中的一個模塊,用于創(chuàng)建基本的HTTP服務(wù)器,它提供了處理HTTP請求的基本框架和核心類,需要的朋友可以參考下2024-11-11
在Python中畫圖(基于Jupyter notebook的魔法函數(shù))
這篇文章主要介紹了在Python中畫圖(基于Jupyter notebook的魔法函數(shù)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
通過Python實現(xiàn)自動填寫調(diào)查問卷
這篇文章主要介紹了通過Python實現(xiàn)自動填寫調(diào)查問卷的相關(guān)資料,需要的朋友可以參考下2017-09-09
python?pandas分割DataFrame中的字符串及元組的方法實現(xiàn)
本文主要介紹了python?pandas分割DataFrame中的字符串及元組的方法實現(xiàn),主要介紹了3種方法,具有一定的參考價值,感興趣的可以了解一下2022-03-03

