age_group = ['0~4 years', '5~9 years', '10~14 years', '15~19 years', '20~24 years', '25~29 years', '30~34 years', '35~39 years', '40~44 years', '45~49 years', '50~54 years', '55~59 years', '60~64 years', '65~69 years', '70~74 years', '75~79 years', '80~84 years', '85~89 years', '90~94 years', '95~99 years', '100 and over']
male = [805852, 1133426, 1186319, 1198911, 1681183, 1954718, 1759768, 1877724, 2038116, 2100431, 2243979, 2064943, 2016321, 1433783, 975415, 685723, 447622, 189457, 47148, 8149, 1056]
female = [764557, 1078946, 1118371, 1114388, 1542471, 1708409, 1572504, 1744623, 1943594, 2033792, 2233654, 2032973, 2081537, 1542824, 1114229, 898881, 731214, 428224, 161610, 35719, 5507]


# your code here
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize = (20, 12))
female_array = np.array(female)
plt.barh(age_group, male, label = "Male", color = "turquoise")
plt.barh(age_group, -female_array, label = "Female", color = "violet")
plt.xticks([])
plt.tick_params(axis = "y", color = "w")
plt.title("Population of Korea in 2021", fontsize = 25)

for index, value in enumerate(male):
    plt.text(value + 25000, index, format(round(value, -3), ",d"))
    
for index, value in enumerate(female_array):
    plt.text(-value - 250000, index, format(round(value, -3), ",d"))
    
plt.box(False)
plt.legend(fontsize = 13)

我写下了上面的代码,以复制我所附的同一张图.但是,由于女性指数和文本的y轴太接近,我想控制整个Barh图变窄.你能给我一些代码让我的PYTHON图形更接近图片吗?

enter image description here

推荐答案

因此,您可以轻松地添加填充您的标签填充使用tick_param. 在右边添加填充有点棘手,我用一种老套的方式在右边添加了一个空白地块. 您可以自定义任一侧的填充,使其显示为您喜欢的样子:

# change the width ratio here if you want more padding on the right
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize = (20, 10), gridspec_kw={'width_ratios': [30, 1]})

ax2.axis('off') # hide 2nd axis
female_array = np.array(female)
bar1 = ax1.barh(age_group, np.array(male), label = "Male", color = "turquoise")
bar2 = ax1.barh(age_group, -female_array, label = "Female", color = "violet")

ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['bottom'].set_visible(False)
ax1.spines['left'].set_visible(False)

ax1.tick_params(axis = "y", color = "w")
ax1.tick_params(axis='y', which='major', pad=55) # change the padding here
ax1.set_title("Population of Korea in 2021", fontsize = 25)

ax1.bar_label(bar1, labels=[f'{val:,}' for val in np.round(male, -3)])
ax1.bar_label(bar2, labels=[f'{val:,}' for val in np.round(female_array, -3)])
ax1.set_xticks([])
ax1.legend(fontsize = 13)
plt.show()

图像:

New Image

与您的原始图像相比:

Original Code's Image

Python相关问答推荐

Docker-compose:为不同项目创建相同的容器

如何编写一个正规表达式来查找序列中具有2个或更多相同辅音的所有单词

如何在矩阵上并行化简单循环?

在Python中管理多个OpenGVBO和VAO实例

Pandas :多索引组

将HTML输出转换为表格中的问题

GL pygame无法让缓冲区与vertextPointer和colorPointer一起可靠地工作

由于NEP 50,向uint 8添加-256的代码是否会在numpy 2中失败?

海运图:调整行和列标签

将图像拖到另一个图像

如何让程序打印新段落上的每一行?

从一个系列创建一个Dataframe,特别是如何重命名其中的列(例如:使用NAs/NaN)

有没有一种ONE—LINER的方法给一个框架的每一行一个由整数和字符串组成的唯一id?

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

在代码执行后关闭ChromeDriver窗口

如何从pandas DataFrame中获取. groupby()和. agg()之后的子列?

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

用两个字符串构建回文

如何根据rame中的列值分别分组值

使用SeleniumBase保存和加载Cookie时出现问题