Matplotlib双面板分组柱状图偏移及X轴标签错乱问题排查
问题原因及解决方案
问题诊断
你的代码出现三个核心问题:
multiplier变量未重置:第一个面板绘制时multiplier累加至4,第二个面板继续使用这个值,导致柱子偏移量(offset = width * multiplier)过大,直接把柱子推到下一个类别区域,造成错位。- X轴标签无序:
combos使用了Python集合({}),集合是无序结构,Matplotlib读取时会打乱标签顺序,导致X轴标签混乱。 - 刻度位置计算错误:每个类别下有4个分组柱子,原代码把刻度放在
x + width位置,没有对准分组的中间,视觉上会出现错位。
修正后的代码
import matplotlib import matplotlib.pyplot as plt import numpy as np ### Create Plot ### matplotlib.rc('xtick', labelsize=22) matplotlib.rc('ytick', labelsize=22) # 改用列表保证标签顺序 combos = ['1 Product', '2 Product', '3 Product','4 Product', '5 Product', '6 Product','7 Product'] top30_rmse = {'RMSE Cold Season': [4.91726910421414,3.92792247654507,3.53717233977146,3.32461948965479,3.19029758875898,3.0975153495569,3.02950308430467], 'RMSE Warm Season': [4.436715943,3.767955663,3.516888357,3.384377437,3.302319607,3.246462314,3.205968468]} top30_corr = {'Correlation Cold Season': [0.639808613,0.71965916,0.75785035,0.780044305,0.794336664,0.80422278,0.811432104], 'Correlation Warm Season': [0.888086922,0.936554339,0.952068621,0.958842973,0.962550154,0.964870985,0.966455676]} depth_rmse = {'RMSE Cold Season': [3.564576325,3.000595232,2.787357328,2.6743701,2.60422583,2.556393944,2.521672889], 'RMSE Warm Season': [4.706464351,4.416909958,4.316077216,4.264766936,4.233682287,4.212831767,4.197875134]} depth_corr = {'Correlation Cold Season': [0.822695625,0.855445131,0.868612894,0.875592404,0.87985752,0.882709683,0.884740798], 'Correlation Warm Season': [0.858527248,0.880016071,0.887233404,0.890810139,0.892940312,0.894352646,0.89535723]} x = np.arange(len(combos)) # 基于列表长度生成x,适配元素数量变化 width = 0.2 # 调整宽度,4个柱子总宽度0.8,避免相邻类别重叠 fig, (ax1, ax3) = plt.subplots(nrows=2,ncols=1,sharex='all',figsize=(20,20)) ## Top-30cm ## multiplier = 0 # 每个面板开始前重置multiplier # RMSE 绘制 for stat, group in top30_rmse.items(): offset = width * multiplier rects = ax1.bar(x + offset, group, width, label=stat) multiplier += 1 # 相关性绘制 ax2 = ax1.twinx() for stat, group in top30_corr.items(): offset = width * multiplier rects = ax2.bar(x + offset, group, width, hatch='x', label=stat) multiplier += 1 # 设置X轴刻度到分组中心 ax1.set_xticks(x + width*1.5, combos) ax1.set_ylim(0,10) ax2.set_ylim(0.5,1) # 添加图例提升可读性 ax1.legend(loc='upper left') ax2.legend(loc='upper right') ## Depth ## multiplier = 0 # 重置multiplier # RMSE 绘制 for stat, group in depth_rmse.items(): offset = width * multiplier rects = ax3.bar(x + offset, group, width, label=stat) multiplier += 1 # 相关性绘制 ax4 = ax3.twinx() for stat, group in depth_corr.items(): offset = width * multiplier rects = ax4.bar(x + offset, group, width, hatch='x', label=stat) multiplier += 1 # 设置X轴刻度到分组中心 ax3.set_xticks(x + width*1.5, combos) ax3.set_ylim(0,10) ax4.set_ylim(0.5,1) # 添加图例 ax3.legend(loc='upper left') ax4.legend(loc='upper right') plt.tight_layout() plt.show()
关键修改点
- 将
combos从集合改为列表,确保X轴标签按1-7 Product的顺序显示。 - 每个面板绘制前重置
multiplier为0,保证第二个面板的柱子从正确的偏移量开始绘制。 - 调整柱子宽度为
0.2,4个柱子总宽度0.8,避免相邻类别重叠;同时将X轴刻度设置为x + width*1.5,对准每个分组的中心位置。 - 为每个轴添加图例,提升图表可读性。
内容的提问来源于stack exchange,提问作者arctic_climate_science
相关产品推荐
相关产品推荐

