matplotlib條形圖

import matplotlib.pyplot as plt
import matplotlib
with open(r'C:\Users\Administrator\Desktop\newTest\we\zj.txt') as f:
# with open(r'C:\Users\Administrator\Desktop\newTest\we\lhq.txt') as f:
    datas = f.readlines()
    _9=_8=_7=_6 = o = 0
    for data in datas:
        data = float(data.strip())
        if data >= 0.9:
            _9 += 1
        elif data < 0.9 and data >= 0.8:
            _8 += 1
        elif data < 0.8 and data >= 0.7:
            _7 += 1
        elif data < 0.7 and data >= 0.6:
            _6 += 1
        else:
            o += 1
x = ['大於等於90%','大於等於80%','大於等於70%','大於等於60%','總共']
y = [_9,_8,_7,_6,o]
import seaborn as sns
sns.set(style='white', font_scale=1.2)
# 保證可以顯示中文字體
plt.rcParams['font.sans-serif']='simhei'
# 設置字體大小
font1 = {'family' : 'simhei',
'weight' : 'normal',
'size'   : 18,}
# 使用數據透視表
f, ax = plt.subplots(figsize=(12,6))
# 畫柱形圖
bar = plt.bar(x,y, color='dodgerblue')
bar[0].set_color('green')
# 給條形圖添加數據標註
for x, y in enumerate([_9,_8,_7,_6,o]):
    plt.text(x,y, "%s" %(str(y)))
#刪除所有邊框
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
# ax.set(title='重慶各區域二手房總價', xlabel='地區', ylabel='總價')
plt.tick_params(labelsize=14)
plt.xlabel('精度', font1)
plt.ylabel('數量', font1)
plt.title('模型B各個精度區間分佈', font1)
plt.waitforbuttonpress(0)
# f.savefig('1_1.png', bbox_inches='tight')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章