如何为条形图批注创建可变字体大小

2024-04-25 11:21:14 发布

您现在位置:Python中文网/ 问答频道 /正文

如何根据以下条件选择条形图条形图内文本批注的字体大小:

  • 文本将完全覆盖矩形条区域

请仔细阅读图表和代码,以便更清楚地了解问题

enter image description here

因此,要求仅为:字体大小应与条形图中的条形图相对应

代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

# Plot styles
mpl.style.use("ggplot")

# data
fruits = pd.Series(index = ["Apples", "Oranges", "Watermelon"], data = [324,518, 258])


# Bar graph for Fruits

# figure
plt.figure(figsize = (7,5))

# bar graph
fruits.plot(kind = "bar", color = ["red", "orange", "green"], alpha = 0.6, width = 0.5, )

# percentage of each fruit type
categories = list(fruits.index)
categories_percent = [100*(value/fruits.sum()) for value in fruits ]

# categories annotations coordinates
ax = plt.gca() # get current axes
rects = ax.patches # rectangles axes of bars in the graph

# annotations
for i in range(len(categories)):
    plt.annotate(f"{categories[i]} - {categories_percent[i] : 0.2f}%",
                 xy = (rects[i].get_x() + rects[i].get_width()/2, 
                       rects[i].get_y() + (ax.get_yticks()[1] - ax.get_yticks()[0])*.2),
                 fontsize = [20,28,12][i], # Chosen by hit and trial for adjustment
                 color = "white",
                 ha = "center",
                 rotation = 90,
                 )
    
plt.ylabel("# Counts", fontsize = 15,)
plt.title("Distribution of Fruits", fontsize = 25, fontname = "Monospace", alpha = .6)
plt.xticks([])
plt.tight_layout(rect=[0, 0, 1, 1])
plt.show()

如何处理这行代码fontsize = [20,28,12][i], # Chosen by hit and trial for adjustment以根据条形图区域动态调整字体大小


Tags: of代码importforgetaspltax
1条回答
网友
1楼 · 发布于 2024-04-25 11:21:14
  • 使用可调整的fontsize更新现有注释
    • 从逻辑角度看,地物大小“y作为高度的比例因子
    • .get_height视为图形的相对高度
    • 实际高度是y比例因子乘以.get_height
    • 关于包含宽度,我们可以包含相对宽度,它只是.get_width(不是get_width*x),但是它只是一个常量,因为它是相对宽度
    • 我们不能包括实际宽度,因为字体会对y轴进行不适当的调整
x,y=15,15
plt.figure(figsize = (x,y))


for i in range(len(categories)):
    txt="{} - {: 0.2f} %".format(categories[i],categories_percent[i])
    plt.annotate(txt,
                 xy = (rects[i].get_x() + rects[i].get_width()/2, 
                       rects[i].get_y() + (ax.get_yticks()[1] - ax.get_yticks()[0])*.2),
                 fontsize = (rects[i].get_height())*y*.2/len(txt), # Chosen by hit and trial for adjustment
                 color = "white",
                 ha = "center",
                 rotation = 90,
                 )
  • 整个代码可以编写得更清晰,如下所示
# data
fruits = pd.Series(index = ["Apples", "Oranges", "Watermelon"], data=[324,518, 258])

# calculate percent
per = fruits.div(fruits.sum()).mul(100).round(2)

# bar graph
y = 5
ax = fruits.plot(kind="bar", color=["red", "orange", "green"], alpha=0.6, width=0.5, figsize=(7, y), rot=0)

labels = [f'{fruit} - {per[fruit]}%' for fruit in fruits.index]

# annotations:
for label, p in zip(labels, ax.patches):
    left, bottom, width, height = p.get_bbox().bounds
    fs = height * y * 0.18 / len(label)
    ax.annotate(label, xy=(left+width/2, bottom+height/2), ha='center', va='center', rotation=90, fontsize=fs)

plt.ylabel("# Counts", fontsize=15,)
plt.title("Distribution of Fruits", fontsize=25, fontname="Monospace", alpha=.6)
plt.xticks([])
plt.tight_layout(rect=[0, 0, 1, 1])
plt.show()

对于figsize=(15,15):

enter image description here

对于figsize=(8,8):

enter image description here

对于figsize=(7,5):

enter image description here

相关问题 更多 >