首页 > 代码库 > 用matplotlib绘制带误差的条形图及中英文字体设置

用matplotlib绘制带误差的条形图及中英文字体设置

  1 #!/usr/bin/env python3  2   3 ## 以下是一个带误差条的条形图的例子,演示了误差条形图的绘制及中英文字体设置  4 import numpy as np  5 import matplotlib as mpl  6 import matplotlib.pyplot as plt  7 from matplotlib.font_manager import FontProperties as FP  8   9 # %matplotlib inline 10 # %config InlineBackend.figure_format = ‘svg‘ 11  12 mpl.rcParams[text.usetex] = False 13 mpl.rcParams[figure.figsize] = (7.40, 5.55) # unit: inch 14 mpl.rcParams[figure.frameon] = False 15  16 ## 中文设置 17 # matplotlib默认不支持ttc,所以可以将ttc转换ttf先。 18 # 将Windows字体 simsun.ttc上传到 https://transfonter.org/ttc-unpack 在线转换成TTF, 19 # 得到simsun.ttf和nsimsun.ttf,将两个ttf文件放到PYTHON安装目录的 20 # Lib\site-packages\matplotlib\mpl-data\fonts\ttf 子目录下。 21 # 删除字体缓存以便重新生成字体缓存:$HOME/.matplotlib/fontList.py3k.cache 22  23  24 # 全局中文设置 25 mpl.rcParams[font.family] = sans-serif 26 mpl.rcParams[font.sans-serif] = NSimSun,Times New Roman 27  28 # 局部中文:设置分别为中文和英文设置两个FontProperties,以便局部切换中英文字体 29 cfp = FP(NSimSun, size=12) 30 efp = FP(Times New Roman, size=12) 31  32 fig,ax = plt.subplots() 33  34 xticklabels = (G1, G2, G3) 35 ylabel = d 36  37 male_means = (9, 10, 9) 38 male_std = (4.66, 3.52, 5.32) 39 female_means = (12, 14, 12) 40 female_std = (6.96, 5.46, 3.61) 41 title = XX实验结果 42  43 N=3  44 ind = np.arange(N)  # the x locations for the groups 45 width = 0.35  # the width of the bars 46 with plt.style.context((ggplot)): 47     rects1 = ax.bar( 48         ind - 0.02, female_means, width, color=darkgrey, yerr=female_std) 49     rects2 = ax.bar( 50         ind + 0.02 + width, 51         male_means, 52         width, 53         color=lightgrey, 54         yerr=male_std) 55  56     ax.set_ylabel(d, fontproperties=efp, rotation=0) 57     # 在label、title中可用参数‘fontproperties‘ 指定字体 58  59     ax.yaxis.set_label_coords(-0.05, 0.95) 60     ax.set_title(title) 61     ax.set_xticks(ind + width / 2) 62  63     ax.set_xticklabels(xticklabels, fontproperties=efp) 64  65     ax.legend((rects1[0], rects2[0]), (‘处理A, ‘处理B), prop=cfp, framealpha=0) 66     # 在legend中可用参数‘prop‘指定字体,注意不是‘fontproperties‘ 67  68     def autolabel(rects, yerr): 69         """ 70         Attach a text label above each bar displaying its height 71         """ 72         for i in range(0, N): 73             rect = rects[i] 74             height = rect.get_height() 75             ax.text( 76                 rect.get_x() + rect.get_width() / 2., 77                 1.05 * height, 78                 %0.2f % yerr[i], 79                 ha=left, 80                 va=bottom, 81                 family=Georgia, 82                 fontsize=9) 83             #在text函数中可用family和fontsize指定字体 84  85     autolabel(rects1, female_means) 86     autolabel(rects2, male_means) 87  88 ax.text( 89     1, 90    20, 91     Hello World, 92     color = b, 93     ha=left, 94     va=bottom, 95     fontproperties=efp) 96 # 在text函数中也可用fontproperties指定字体 97  98 fig.tight_layout() 99 fig.savefig(filename.svg, format=svg)100 # 保存为矢量图svg格式,如需插入word,可以用inkscape软件将其转换成emf格式

 

用matplotlib绘制带误差的条形图及中英文字体设置