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

seaborn.objects分面图坐标轴与图例字体大小设置失效求助

使用Seaborn Objects调整分面堆叠柱状图坐标轴字体大小无效的解决方法

我在用Seaborn Objects创建分面堆叠柱状图时,尝试通过label()方法设置fontsize参数来增大坐标轴字体以便海报展示,但该操作没有效果。以下是相关数据集、原绘图代码及无效尝试:

数据集

{'storage': {0: '4 °C',
  1: '4 °C',
  2: 'fresh',
  3: 'fresh',
  4: 'frozen',
  5: 'frozen',
  6: '4 °C',
  7: '4 °C',
  8: 'fresh',
  9: 'fresh',
  10: 'frozen',
  11: 'frozen',
  12: '4 °C',
  13: '4 °C',
  14: 'fresh',
  15: 'fresh',
  21: 'fresh'},
 'time': {0: '4 h',
  1: '6 h',
  2: '4 h',
  3: '6 h',
  4: '4 h',
  5: '6 h',
  6: '4 h',
  7: '6 h',
  8: '4 h',
  9: '6 h',
  10: '4 h',
  11: '6 h',
  12: '4 h',
  13: '6 h',
  14: '4 h',
  15: '6 h',
  21: '6 h'},
 'Predicted Class': {0: 'resting',
  1: 'resting',
  2: 'resting',
  3: 'resting',
  4: 'resting',
  5: 'resting',
  6: 'swollen',
  7: 'swollen',
  8: 'swollen',
  9: 'swollen',
  10: 'swollen',
  11: 'swollen',
  12: 'germling',
  13: 'germling',
  14: 'germling',
  15: 'germling',
  21: 'hyphae'},
 '%': {0: 25.57,
  1: 4.87,
  2: 12.19,
  3: 4.87,
  4: 99.87,
  5: 99.77,
  6: 72.89,
  7: 60.23,
  8: 86.93,
  9: 42.83,
  10: 0.13,
  11: 0.23,
  12: 1.54,
  13: 34.9,
  14: 0.88,
  15: 51.4,
  21: 0.9}}

原绘图代码

import seaborn.objects as so

# Create the Plot object
plot = so.Plot(counts2_melted_df, x="storage", y="%", color="Predicted Class", text="%")

# Facet the plot by 'strain' with an increased height for each row
plot = plot.layout(size=(8,8)) # Adjust the scale factor for height

# Add the bar and stack
plot = plot.add(so.Bar(), so.Stack()).scale(color=color_palette)

plot = plot.label(x="Storage Condition", y="Percentage of Morphotype (%)")

plot= plot.theme({"axes.facecolor": "w", "axes.edgecolor": "slategray"})

plot = plot.facet(row="time")

#Render the plot to get the underlying matplotlib axes
plot.plot()

无效尝试代码

plot = plot.label(x="Storage Condition", fontsize=28)
plot = plot.label(y="Percentage of Morphotype (%)", fontsize=28)

解决方法

Seaborn Objects的label()方法并不支持直接传递fontsize参数,要调整坐标轴字体大小,有两种可行方案:

方案1:通过theme()全局设置字体大小

直接在theme()方法中添加Matplotlib的字体配置参数,一次性统一设置坐标轴标签、刻度的字体大小:

修改后的代码:

import seaborn.objects as so

# Create the Plot object
plot = so.Plot(counts2_melted_df, x="storage", y="%", color="Predicted Class", text="%")

plot = plot.layout(size=(8,8)) 

# Add the bar and stack
plot = plot.add(so.Bar(), so.Stack()).scale(color=color_palette)

plot = plot.label(x="Storage Condition", y="Percentage of Morphotype (%)")

# 更新theme,添加字体大小配置
plot= plot.theme({
    "axes.facecolor": "w", 
    "axes.edgecolor": "slategray",
    "axes.labelsize": 28,  # 坐标轴标签字体大小
    "xtick.labelsize": 24, # x轴刻度字体大小
    "ytick.labelsize": 24, # y轴刻度字体大小
    "legend.fontsize": 20, # 图例字体大小(按需添加)
    "figure.titlesize": 30 # 图表标题字体大小(按需添加)
})

plot = plot.facet(row="time")

plot.plot()

方案2:绘图后获取Axes对象手动设置

如果需要更精细地调整特定子图的字体,可以在调用plot()后获取Matplotlib的Figure和Axes对象,遍历子图逐个设置:

import seaborn.objects as so

# 原绘图代码部分不变
plot = so.Plot(counts2_melted_df, x="storage", y="%", color="Predicted Class", text="%")
plot = plot.layout(size=(8,8)) 
plot = plot.add(so.Bar(), so.Stack()).scale(color=color_palette)
plot = plot.label(x="Storage Condition", y="Percentage of Morphotype (%)")
plot= plot.theme({"axes.facecolor": "w", "axes.edgecolor": "slategray"})
plot = plot.facet(row="time")

# 绘制并获取figure对象
fig = plot.plot()

# 遍历所有子图的Axes,设置字体
for ax in fig.axes:
    ax.set_xlabel(ax.get_xlabel(), fontsize=28)
    ax.set_ylabel(ax.get_ylabel(), fontsize=28)
    ax.tick_params(axis='both', labelsize=24) # 设置刻度字体大小

这两种方法都能有效增大字体,满足海报展示的清晰度需求。


内容的提问来源于stack exchange,提问作者mr.raccoon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 13:27:15