首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >利用水平条形图探索实验室感染数量

利用水平条形图探索实验室感染数量

作者头像
HsuHeinrich
发布2025-07-28 21:18:38
发布2025-07-28 21:18:38
6200
代码可运行
举报
文章被收录于专栏:HsuHeinrichHsuHeinrich
运行总次数:0
代码可运行

利用水平条形图探索实验室感染数量

代码语言:javascript
代码运行次数:0
运行
复制
import numpy as np
import matplotlib.pyplot as plt

from matplotlib import lines
from matplotlib import patches
from matplotlib.patheffects import withStroke

数据探索

以下数据如果有需要的同学可关注公众号HsuHeinrich,回复【数据可视化】自动获取~

代码语言:javascript
代码运行次数:0
运行
复制
# 自定义数据(来源于原始图标推断的数据,并非原始数据计算或者随机自定义)
counts = [6, 7, 7, 9, 11, 15, 17, 18, 54]
names = [
    "Hantavirus", "Tularemia", "Dengue", "Ebola", "E. coli", 
    "Tuberculosis", "Salmonella", "Vaccinia", "Brucella"
]

# 条形图位置
y = [i * 0.9 for i in range(len(names))]

绘制基本条形图

代码语言:javascript
代码运行次数:0
运行
复制
# 定义颜色
BLUE = "#076fa2"
RED = "#E3120B"
BLACK = "#202020"
GREY = "#a2a2a2"
代码语言:javascript
代码运行次数:0
运行
复制
fig, ax = plt.subplots(figsize=(12, 7))

ax.barh(y, counts, height=0.55, align="edge", color=BLUE);
output_7_0
output_7_0

output_7_0

自定义布局

代码语言:javascript
代码运行次数:0
运行
复制
# 自定义x轴刻度
ax.xaxis.set_ticks([i * 5 for i in range(0, 12)]) # x刻度位置
ax.xaxis.set_ticklabels([i * 5 for i in range(0, 12)], size=16, fontfamily="Econ Sans Cnd", fontweight=100) # x刻度标签
ax.xaxis.set_tick_params(labelbottom=False, labeltop=True, length=0) # x刻度参数

# 指着轴范围
ax.set_xlim((0, 55.5))
ax.set_ylim((0, len(names) * 0.9 - 0.2))

# 设置网格线
ax.set_axisbelow(True) # 网格线绘制在图像和刻度标签之下
ax.grid(axis = "x", color="#A8BAC4", lw=1.2) # 指定x轴网格线的颜色和线宽
ax.spines["right"].set_visible(False) # 去掉右边框
ax.spines["top"].set_visible(False) # 去掉顶边框
ax.spines["bottom"].set_visible(False) # 去掉底边框
ax.spines["left"].set_lw(1.5) # 保留左边框,并设置线宽
ax.spines["left"].set_capstyle("butt") # 左脊线的线帽样式

# 隐藏y轴
ax.yaxis.set_visible(False)

fig
output_9_0
output_9_0

output_9_0

添加标签

代码语言:javascript
代码运行次数:0
运行
复制
PAD = 0.3
for name, count, y_pos in zip(names, counts, y):
    x = 0
    color = "white"
    path_effects = None
    if count < 8:
        x = count
        color = BLUE    
        path_effects=[withStroke(linewidth=6, foreground="white")]
    
    ax.text(
        x + PAD, y_pos + 0.5 / 2, name, 
        color=color, fontfamily="Econ Sans Cnd", fontsize=18, va="center",
        path_effects=path_effects
    ) 
fig 
output_11_0
output_11_0

output_11_0

添加必要注释信息

代码语言:javascript
代码运行次数:0
运行
复制
# 调整布局
fig.subplots_adjust(left=0.005, right=1, top=0.8, bottom=0.1)

# 标题
fig.text(
    0, 0.925, "Escape artists", 
    fontsize=22, fontweight="bold", fontfamily="Econ Sans Cnd"
)
# 副标题
fig.text(
    0, 0.875, "Number of laboratory-acquired infections, 1970-2021", 
    fontsize=20, fontfamily="Econ Sans Cnd"
)

# 说明
source = "Sources: Laboratory-Acquired Infection Database; American Biological Safety Association"
fig.text(
    0, 0.06, source, color=GREY, 
    fontsize=14, fontfamily="Econ Sans Cnd"
)

# 著作信息
fig.text(
    0, 0.005, "The Economist", color=GREY,
    fontsize=16, fontfamily="Lato"
)

# 添加顶部水平线
fig.add_artist(lines.Line2D([0, 1], [1, 1], lw=3, color=RED, solid_capstyle="butt"))
fig.add_artist(patches.Rectangle((0, 0.975), 0.05, 0.025, color=RED))

# 设置背景色
fig.set_facecolor("white")
fig
output_13_0
output_13_0

参考:Horizontal barplot with Matplotlib[1]

共勉~

参考资料

[1]

Horizontal barplot with Matplotlib: https://python-graph-gallery.com/web-horizontal-barplot-with-labels-the-economist/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-07-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 HsuHeinrich 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 利用水平条形图探索实验室感染数量
    • 数据探索
    • 绘制基本条形图
    • 自定义布局
    • 添加标签
    • 添加必要注释信息
      • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档