import matplotlib import matplotlib.pyplot as plt import numpy as np def auto_text(rects): for rect in rects: ax.text(rect.get_x(), rect.get_height(), rect.get_height(), ha='left', va='bottom') labels = ['G1', 'G2', 'G3', 'G4', 'G5'] men_means = [20, 34, 30, 35, 27] women_means = [25, 32, 34, 20, 25] women2_means = [27, 21, 33, 29, 21] index = np.arange(len(labels)) width = 0.2 fig, ax = plt.subplots() rect1 = ax.bar(index - width / 3, men_means, color ='red', width=width, label ='Men') rect2 = ax.bar(index + width*2 / 3, women_means, color ='springgreen', width=width, label ='Women',hatch='...') rect3 = ax.bar(index + width*5 / 3, women2_means, color ='blue', width=width, label ='Women2',hatch='//') ax.set_title('Scores by gender') ax.set_xticks(ticks=index) ax.set_xticklabels(labels) ax.set_ylabel('Scores', fontsize=13) ax.set_ylim(0, 50) auto_text(rect1) auto_text(rect2) auto_text(rect3) ax.legend(loc='upper right', frameon=False) fig.tight_layout() plt.savefig('2.tif', dpi=300) plt.grid() plt.show()
法2:黑白图。
import matplotlib.pyplot as plt import pandas as pd labels = ['吉祥物公仔','校服','文化摆件','实用工具','纪念日产品','其他'] y1 = [19.3,19.3,17.96,24.93,16.09,2.41] y2 = [32.97,12.03,12.67,20.43,21.2,0.71] y3= [11.32,13.21,18.87,24.53,30.19,1.89] fig,ax = plt.subplots(1,1,figsize=(8,4.5)) x = np.arange(len(labels)) total_width, n = 0.8, 3 width = total_width / n x = x - (total_width - width) / 2 label_font = { 'weight':'bold', 'size':14, 'family':'simsun' } colors = ['#9999FF','#58C9B9','#CC33CC','#D1B6E1','#99FF99','#FF6666'] rects1 = ax.bar(x, y1, width, label='学生',ec='k',color='w',lw=.8, hatch='xxx') rects2 = ax.bar(x + width, y2, width, label='教师',ec='k',color='w', lw=.8,hatch='//') rects3 = ax.bar(x + width * 2, y3, width, label='校友',ec='k',color='w', lw=.8,hatch='---') # tick_params参数刻度线样式设置 # ax.tick_params(axis=‘x’, tickdir=‘in’, labelrotation=20)参数详解 # axis : 可选{‘x’, ‘y’, ‘both’} ,选择对哪个轴操作,默认是’both’ # which : 可选{‘major’, ‘minor’, ‘both’} 选择对主or副坐标轴进行操作 # direction/tickdir : 可选{‘in’, ‘out’, ‘inout’}刻度线的方向 # color : 刻度线的颜色,我一般用16进制字符串表示,eg:’#EE6363’ # width : float, 刻度线的宽度 # size/length : float, 刻度线的长度 # pad : float, 刻度线与刻度值之间的距离 # labelsize : float/str, 刻度值字体大小 # labelcolor : 刻度值颜色 # colors : 同时设置刻度线和刻度值的颜色 # bottom, top, left, right : bool, 分别表示上下左右四边,是否显示刻度线,True为显示 ax.tick_params(which='major',direction='in',length=5,width=1.5,labelsize=11,bottom=False) ax.tick_params(axis='x',labelsize=11,bottom=False,labelrotation=0) ax.set_xticks(range(len(labels))) ax.set_ylim(ymin = 0,ymax = 40) # 0 - 1800 ,200为一个间距 ax.set_yticks(np.arange(0,41,10)) ax.set_ylabel('(占比)',fontdict=label_font) ax.set_xticklabels(labels,fontdict=label_font) ax.legend(prop =label_font) ''' # 设置有边框和头部边框颜色为空right、top、bottom、left ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ''' # 上下左右边框线宽 linewidth = 2 for spine in ['top','bottom','left','right']: ax.spines[spine].set_linewidth(linewidth) # Add some text for labels, title and custom x-axis tick labels, etc. def autolabel(rects): for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), textcoords="offset points", ha='center', va='bottom') autolabel(rects1) autolabel(rects2) autolabel(rects3) fig.tight_layout()