python画的柱状图代码
画好后效果图
实现代码如下
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib import pyplot
from matplotlib.patches import ConnectionPatch
plt.style.use('seaborn-whitegrid')#风格
palette = pyplot.get_cmap('Set1')#颜色器
tits=['best_metric','avg_metric','worst_metric']
#柱状图数据
all_y=[[21-3,0,21-3,21-6,21-3,21-2],
[8,0,6,1,8,12],
[5,0,3,1,8,10]]
#每个指标画个图
for tit,y in zip(tits,all_y):
cell=['approach1','approach2','approach3','approach4','approach5','approach6']
index = np.arange(len(cell))
x=index
#可以通过下面方式指定bar的颜色,这里使用palette
# color = ['lightskyblue','yellow', 'lime', 'gold','violet','red']
color =[1,2,3,4,5,0]
for x1, y1, c1 in zip(x, y, color): #遍历以上三者,每一次生成一条bar
plt.bar(x1, y1, width=0.6, color=palette(c1))
plt.xticks(index, cell,fontsize=10)
#论文常用eps和pdf,下面的也可以保存为pdf
# plt.savefig('figs/5_1_'+tit+'bar.eps',format='eps',dpi=500,bbox_inches='tight', pad_inches = +0.1)
plt.show()