You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用等尺寸图片替换Matplotlib柱状图的Patch?

用等尺寸图片替换Matplotlib柱状图Patch的优雅方案

实现思路

核心是利用柱子的位置与尺寸参数,将图片精准匹配绘制到对应区域。先绘制透明的基础柱状图获取定位信息,再加载并缩放图片,最后把图片覆盖到每个柱子位置上。

完整代码示例

import matplotlib.pyplot as plt
import numpy as np

# 1. 准备数据
x = np.arange(5)
heights = [3, 5, 2, 4, 6]
width = 0.6  # 柱子宽度

# 2. 创建画布和轴对象
fig, ax = plt.subplots(figsize=(8, 5))

# 3. 绘制透明柱状图(仅用于获取柱子位置和尺寸)
bars = ax.bar(x, heights, width=width, color='none')

# 4. 加载图片(替换为你的图片路径)
img = plt.imread('your_image.png')

# 5. 遍历柱子,绘制匹配尺寸的图片
for bar in bars:
    # 获取柱子的坐标范围:[左, 右, 下, 上]
    x0 = bar.get_x()
    y0 = bar.get_y()
    x1 = x0 + bar.get_width()
    y1 = y0 + bar.get_height()
    # 用imshow绘制图片,extent参数控制位置与尺寸,aspect适配柱子比例
    ax.imshow(img, extent=[x0, x1, y0, y1], aspect='auto')

# 6. 调整坐标轴显示
ax.set_xticks(x)
ax.set_xticklabels(['A', 'B', 'C', 'D', 'E'])
ax.set_ylim(0, max(heights) + 0.5)

plt.tight_layout()
plt.show()

关键细节说明

  • 透明柱子定位:先绘制无填充色的柱子,直接获取每个柱子的坐标、宽高参数,避免手动计算坐标的误差。
  • 精准尺寸匹配:通过extent参数直接绑定图片的显示范围与柱子的坐标范围,保证图片完全覆盖柱子区域。
  • 比例适配:设置aspect='auto'让图片自动适配柱子的宽高比,避免拉伸变形。

进阶优化方案(用AnnotationBBox控制)

如果需要更灵活的位置微调,可结合OffsetImage和AnnotationBBox实现:

from matplotlib.offsetbox import OffsetImage, AnnotationBBox

def add_bar_image(ax, bar, img):
    # 获取柱子中心坐标
    center_x = bar.get_x() + bar.get_width()/2
    center_y = bar.get_y() + bar.get_height()/2
    # 计算图片缩放比例:匹配柱子宽度
    bar_width = bar.get_width()
    # 将轴坐标转屏幕坐标计算缩放值
    x0, _ = ax.transData.transform((bar.get_x(), 0))
    x1, _ = ax.transData.transform((bar.get_x()+bar_width, 0))
    zoom = (x1 - x0)/img.shape[1]
    # 创建图片对象并绑定到柱子中心
    oi = OffsetImage(img, zoom=zoom)
    ab = AnnotationBBox(oi, (center_x, center_y), frameon=False, pad=0)
    ax.add_artist(ab)

# 遍历柱子添加图片
for bar in bars:
    add_bar_image(ax, bar, img)

这种方式通过缩放比例精准控制图片宽度与柱子一致,同时保证图片原始比例,适配不同高度的柱子。

内容的提问来源于stack exchange,提问作者MichaelH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:45:51