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) # 為計算百分比做準備 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python數(shù)據(jù)分析必會的Pandas技巧匯總
用Python做數(shù)據(jù)分析光是掌握numpy和matplotlib可不夠,numpy雖然能夠幫我們處理處理數(shù)值型數(shù)據(jù),但很多時候,還有字符串,還有時間序列等,比如:我們通過爬蟲獲取到了存儲在數(shù)據(jù)庫中的數(shù)據(jù),一些Pandas必會的用法,讓你的數(shù)據(jù)分析水平更上一層樓2021-08-08pygame+opencv實現(xiàn)讀取視頻幀的方法示例
由于pygame.movie.Movie.play()只支持MPEG格式的視頻,所以決定使用與opencv讀取視頻幀的畫面,本文就詳細的介紹了pygame+opencv實現(xiàn)讀取視頻幀,感興趣的可以了解一下2021-12-12python數(shù)據(jù)結(jié)構(gòu)之棧、隊列及雙端隊列
在上一章的學習中,我們主要學習了怎么去衡量一個算法的好壞,比較常見的方式是使用大O記法,就是所謂的時間復雜度,這一章節(jié)我來學習基本的數(shù)據(jù)結(jié)構(gòu),如棧、隊列和雙端隊列等等。感興趣的小伙伴可以參考一下2021-12-12Python使用openpyxl批量處理數(shù)據(jù)
openpyxl 是一個用于處理 xlsx 格式Excel表格文件的第三方python庫,其支持Excel表格絕大多數(shù)基本操作。本文給大家介紹Python使用openpyxl批量處理數(shù)據(jù)的操作方法,感興趣的朋友一起看看吧2021-06-06Python基礎(chǔ)之循環(huán)語句用法示例【for、while循環(huán)】
這篇文章主要介紹了Python基礎(chǔ)之循環(huán)語句用法,結(jié)合實例形式分析了Python使用for、while循環(huán)及range、break和continue語句相關(guān)使用技巧,需要的朋友可以參考下2019-03-03PyMongo進行MongoDB查詢和插入操作的高效使用示例
這篇文章主要為大家介紹了PyMongo進行MongoDB查詢和插入操作的高效使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11