用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格式

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM