柱狀圖代碼如下:
import numpy as np
import matplotlib.pyplot as plt
name_list = ['DBLP', 'YELP']
num_list = [0.8533, 0.5411]
num_list1 = [0.4965, 0.4359]
num_list2 = [0.5486, 0.5673]
x_width = 0.33 # 調節橫寬度
x = [0, x_width]
total_width, n = 0.26, 3
width = total_width / n
fontsize = 8 # 字體大小
fig = plt.figure(figsize=(5.3, 4.3), dpi=80, facecolor='white', edgecolor="white") # figsize長、寬
axes = plt.subplot(111)
axes.bar(x, num_list, width=width, label='Esim', hatch='\\\\\\', color='white',
edgecolor='aquamarine') # hatch:填充圖案,edgecolor:填充圖案顏色,color:柱形圖顏色
# 給柱形圖加上values
for a, b in zip(x, num_list):
"""
x,y:表示座標值上的值
string:表示說明文字
fontsize:表示字體大小
verticalalignment:垂直對齊方式 ,參數:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]
horizontalalignment:水平對齊方式 ,參數:[ ‘center’ | ‘right’ | ‘left’ ]
xycoords選擇指定的座標軸系統:
figure points:圖左下角的點
figure pixels:圖左下角的像素
figure fraction:圖的左下部分
axes points:座標軸左下角的點
axes pixels:座標軸左下角的像素
axes fraction:左下軸的分數
data:使用被註釋對象的座標系統(默認)
polar(theta,r):if not native ‘data’ coordinates t
arrowprops #箭頭參數,參數類型爲字典dict
width:箭頭的寬度(以點爲單位)
headwidth:箭頭底部以點爲單位的寬度
headlength:箭頭的長度(以點爲單位)
shrink:總長度的一部分,從兩端“收縮”
facecolor:箭頭顏色
bbox給標題增加外框 ,常用參數如下:
boxstyle:方框外形
facecolor:(簡寫fc)背景顏色
edgecolor:(簡寫ec)邊框線條顏色
edgewidth:邊框線條大小
"""
axes.text(a, b + 0.028, '%.4f' % b, ha='center', verticalalignment="top", fontsize=fontsize)
for i in range(len(x)):
x[i] = x[i] + width
axes.bar(x, num_list1, width=width, label='HIN2Vec', tick_label=name_list, hatch='///', color="white",
edgecolor='lightgreen')
for a, b in zip(x, num_list1):
axes.text(a, b + 0.028, '%.4f' % b, ha='center', verticalalignment="top", fontsize=fontsize)
for i in range(len(x)):
x[i] = x[i] + width
axes.bar(x, num_list2, width=width, label='HINGAN', tick_label=name_list, hatch='//', color="white",
edgecolor='mediumpurple')
for a, b in zip(x, num_list2):
axes.text(a, b + 0.028, '%.4f' % b, ha='center', verticalalignment="top", fontsize=fontsize)
plt.xticks(np.asarray([0, x_width]) + total_width / 3, name_list) # 調節橫座標不居中
font1 = {
# 'family': 'Times New Roman', # 字體
# 'weight': 'normal',
'size': fontsize # 字體大小
}
plt.tick_params(labelsize=fontsize)
plt.legend(loc="upper right", prop=font1) # 圖例位置、大小
plt.ylabel('AP', font1) # 縱座標大小、字體
plt.show()
效果圖如下: