# 导入必要的库
import pandas as pd # 用于数据处理
import matplotlib.pyplot as plt # 用于绘图
from PIL import Image # 用于图像处理
import numpy as np # 用于数值计算
import os # 用于文件操作
import cv2 # 导入 OpenCV,用于图像处理
# 自定义数据
data = {
'Score': np.random.randint(1, 15, size=5), # 随机生成5个分数,范围为1到14
'Comment': np.random.choice(['Fun', 'Okay', 'Nice', 'Bad', 'Cool'], size=5), # 随机生成5个评论
'Image': np.random.choice(['python', 'apple', 'banana'], size=5) # 随机生成5个图像名称
}
df = pd.DataFrame(data) # 将数据转换为DataFrame
# 修改Image列,增加前后缀拼出路径
df['Image'] = df['Image'].apply(lambda x: x + '.png' if '.png' not in x else x) # 确保文件名以.png结尾
df['Image'] = df['Image'].apply(lambda x: 'pic/' + x if 'pic/' not in x else x) # 确保路径以pic/开头
# 创建图片目录(如果不存在)
if not os.path.exists('pic'): # 检查pic目录是否存在
os.makedirs('pic') # 如果不存在,则创建pic目录
# 随机生成一些图片(仅用于演示,实际中请使用真实图片)
for img in df['Image']: # 遍历Image列中的每个图片路径
if not os.path.exists(img): # 如果图片不存在
img_array = np.random.randint(0, 256, size=(100, 100, 3), dtype=np.uint8) # 随机生成一个100x100的RGB图片
img_pil = Image.fromarray(img_array) # 将numpy数组转换为Pillow图像
img_pil.save(img) # 保存图片到指定路径
# 定义插入图片的函数
def insert_image(path, ax, x, y):
img = Image.open(path) # 打开图片
img.thumbnail((50, 50)) # 调整图片大小为50x50(保持宽高比)
img_array = np.array(img) # 将图片转换为numpy数组
ax.imshow(img_array, extent=(x - 0.25, x + 0.25, y - 0.25, y + 0.25)) # 在指定位置插入图片
# 定义插入圆形图片的函数
def insert_circled_image(path, ax, x, y):
img = Image.open(path) # 打开图片
img = img.resize((50, 50)) # 调整图片大小为50x50
img_array = np.array(img) # 将图片转换为numpy数组
mask = np.zeros(img_array.shape[:2], dtype=np.uint8) # 创建一个与图片大小相同的遮罩
center = (25, 25) # 圆形遮罩的中心点
radius = 25 # 圆形遮罩的半径
cv2.circle(mask, center, radius, (255), -1) # 在遮罩上绘制一个白色圆形
masked_img = cv2.bitwise_and(img_array, img_array, mask=mask) # 使用遮罩裁剪图片为圆形
ax.imshow(masked_img, extent=(x - 0.25, x + 0.25, y - 0.25, y + 0.25)) # 在指定位置插入圆形图片
# 绘制表格并插入图片
fig = plt.figure(figsize=(12, 6)) # 创建一个大小为12x6的图形
# 插入普通图片
plt.subplot(1, 2, 1) # 创建一个1x2的子图,当前激活的是第1个
plt.title('插入图片') # 设置子图标题
ax = plt.gca() # 获取当前子图的轴对象
ax.set_xlim(0, len(df.columns)) # 设置x轴范围
ax.set_ylim(0, len(df) + 1) # 设置y轴范围
ax.set_xticks(np.arange(len(df.columns)) + 0.5) # 设置x轴刻度位置
ax.set_yticks(np.arange(len(df)) + 0.5) # 设置y轴刻度位置
ax.set_xticklabels(df.columns, rotation=45, ha='right') # 设置x轴刻度标签为DataFrame的列名
ax.set_yticklabels(df.index) # 设置y轴刻度标签为DataFrame的索引
ax.grid(True, linestyle='--', linewidth=0.5) # 添加网格线
ax.xaxis.tick_top() # 将x轴刻度标签移到顶部
# 插入图片到表格
for i, row in df.iterrows(): # 遍历DataFrame的每一行
insert_image(row['Image'], ax, 0.5, len(df) - i - 0.5) # 在指定位置插入图片
# 插入圆形图片
plt.subplot(1, 2, 2) # 创建一个1x2的子图,当前激活的是第2个
plt.title('插入图片-圆形图像') # 设置子图标题
ax = plt.gca() # 获取当前子图的轴对象
ax.set_xlim(0, len(df.columns)) # 设置x轴范围
ax.set_ylim(0, len(df) + 1) # 设置y轴范围
ax.set_xticks(np.arange(len(df.columns)) + 0.5) # 设置x轴刻度位置
ax.set_yticks(np.arange(len(df)) + 0.5) # 设置y轴刻度位置
ax.set_xticklabels(df.columns, rotation=45, ha='right') # 设置x轴刻度标签为DataFrame的列名
ax.set_yticklabels(df.index) # 设置y轴刻度标签为DataFrame的索引
ax.grid(True, linestyle='--', linewidth=0.5) # 添加网格线
ax.xaxis.tick_top() # 将x轴刻度标签移到顶部
# 插入圆形图片到表格
for i, row in df.iterrows(): # 遍历DataFrame的每一行
insert_circled_image(row['Image'], ax, 0.5, len(df) - i - 0.5) # 在指定位置插入圆形图片
plt.tight_layout() # 自动调整子图布局
plt.show() # 显示图形
在表格中添加图标示意
最新推荐文章于 2025-04-29 15:28:57 发布