Matplotlib饼图/圆环图注释文本大小

时间:2019-12-03 08:57:14

标签: python matplotlib pie-chart

对于下面给出的从matplotlib官方页面(https://matplotlib.org/3.1.1/gallery/pie_and_polar_charts/pie_and_donut_labels.html)创建甜甜圈图的代码

class Solution {
    public int calculateMinimumHP(int[][] dungeon) {    
        int rows = dungeon.length;
        int cols = dungeon[0].length;
        int[][] dp = new int[rows + 1][cols + 1];

         for (int[] level : dp) Arrays.fill(level, Integer.MAX_VALUE);


        dp[rows][cols - 1] = 1;  
        dp[rows - 1][cols] = 1;

        for(int i = rows - 1; i >= 0; i--){
            for(int j = cols - 1; j >= 0; j--){
                int min = Math.min(dp[i][j + 1] , dp[i + 1][j]) - dungeon[i][j];
                dp[i][j] = min <= 0 ? 1 : min; 
            }
        }

        return dp[0][0]; 
    }
}

我知道那条线


recipe = ["225 g flour",
          "90 g sugar",
          "1 egg",
          "60 g butter",
          "100 ml milk",
          "1/2 package of yeast"]

data = [225, 90, 50, 60, 100, 5]

wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)

bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
          bbox=bbox_props, zorder=0, va="center")

for i, p in enumerate(wedges):
    ang = (p.theta2 - p.theta1)/2. + p.theta1
    y = np.sin(np.deg2rad(ang))
    x = np.cos(np.deg2rad(ang))
    horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
    connectionstyle = "angle,angleA=0,angleB={}".format(ang)
    kw["arrowprops"].update({"connectionstyle": connectionstyle})
    ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
                horizontalalignment=horizontalalignment, **kw)

ax.set_title("Matplotlib bakery: A donut")

plt.show()

定义了注解,但是我无法理解如何控制注解文本的大小。

任何帮助将不胜感激。 提前致谢。

1 个答案:

答案 0 :(得分:1)

您正确地将该行标识为定义注释。从ax.annotate的文档中,我们可以看到额外的关键字被传递到matplotlib.Text对象。该对象接受一个fontsize kwarg,它可以是{size in points, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}之一。

例如:

ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
            horizontalalignment=horizontalalignment,
            fontsize=8, **kw)

输出:

Fontsize 8

或带有较大的文本:

ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
            horizontalalignment=horizontalalignment,
            fontsize=16, **kw)

输出:

enter image description here