欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python?matplotlib實現(xiàn)條形圖的填充效果

 更新時間:2022年04月21日 08:30:12   作者:東大梅西  
這篇文章主要為大家詳細(xì)介紹了python?matplotlib實現(xiàn)條形圖的填充效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python matplotlib實現(xiàn)條形圖填充效果的具體代碼,供大家參考,具體內(nèi)容如下

寫專利用的python里面的matplotlib畫的條形圖 ,最開始用的三種顏色來區(qū)分,如下圖:

然而被告知不行,只能用黑白的,其他顏色不能用,于是想到用灰度,如下圖:

然而又被告知,不行,不能用灰度,只能用條形框的填充格式進行區(qū)分,接近崩潰,百度了半天也沒看到相關(guān)的帖子,后來終于找到了,先來看一下效果,源碼貼在最后面。效果如下圖:

源碼如下:

import matplotlib.pyplot as plt?
import numpy as np
from pylab import mpl

mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']
Y2016 = [83.2, 85, 83.9]?
Y2017 = [80.4, 74.6, 86.5]
Y2018 = [85.8, 87.2, 89.1]
labels = ['準(zhǔn)確性', '敏感性', '特異性']?
bar_width = 0.25?
?
# 繪圖
plt.figure(figsize=(10,10))
plt.bar(np.arange(3), Y2016, label='TSVM', color='white', alpha=1, width=bar_width,edgecolor="k",hatch='/')?
plt.bar(np.arange(3) + bar_width, Y2017, label=u'協(xié)同訓(xùn)練半監(jiān)督', color='white', alpha=1, edgecolor="k",width=bar_width,hatch="***")
plt.bar(np.arange(3) + 2*bar_width, Y2018, label=u'結(jié)合TSVM和協(xié)同訓(xùn)練半監(jiān)督', color='white', alpha=1, edgecolor="k",width=bar_width,hatch="xxx")
?
# 添加刻度標(biāo)簽?
plt.xticks(np.arange(3) + bar_width, labels)
plt.tick_params(labelsize=20)
# 設(shè)置Y軸的刻度范圍?
plt.ylim([0, 100])?
?
# 為每個條形圖添加數(shù)值標(biāo)簽?
for x2016, y2016 in enumerate(Y2016):?
? ? plt.text(x2016, y2016 + 2, '%s' % y2016, ha='center',fontsize=20)?
?
for x2017, y2017 in enumerate(Y2017):?
? ? plt.text(x2017 + bar_width, y2017 + 2, '%s' % y2017, ha='center',fontsize=20)

for x2018, y2018 in enumerate(Y2018):?
? ? plt.text(x2018 + 2*bar_width, y2018 + 2, '%s' % y2018, ha='center',fontsize=20)
# 顯示圖例
plt.legend(bbox_to_anchor=(0.5,1), loc=3, borderaxespad=0,fontsize=17)
plt.savefig('foo.png')?
# 顯示圖形?
plt.show()?

其中,hatch這個參數(shù)的值就是改變填充效果的,具體的效果有:‘*oO/|±.’,填充密度根據(jù)你引用的符號數(shù)量變化,如hatch=‘/’肯定比hatch=’///'要密集對吧。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論