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

Matplotlib报错:FixedLocator位置数与刻度标签数不匹配排查及柱状图动物名称x轴标签添加方法

问题分析与解决方案

1. 为什么ax.set_xticklabels(x_labels)会破坏数值标注?

你的报错信息已经直接点明了核心原因:刻度位置的数量(11个,对应new_df的11行数据)和你传入的x_labels的数量(166个,对应原始全量数据)不匹配。

当Matplotlib遇到这种情况时,会直接抛出错误并中断后续的绘图流程——包括你后面调用的add_value_labels函数。因为轴的内部状态已经因为标签-刻度不匹配而混乱了,后续的补丁(ax.patches)处理、标注添加自然无法正常执行。简单说,这行代码直接导致整个图表的渲染逻辑崩溃了,数值标注功能当然也就失效了。

2. 如何同时保留数值标注并设置正确的x轴动物名称标签?

你不需要依赖原始数据的x_labels,因为你已经对数据做了筛选、排序,直接从处理后的new_df中提取对应动物名称作为标签即可。这里有两种更可靠的方式:

方法一:绘图时直接指定x轴列

在调用plot方法时,通过x参数指定用new_df的Animals列作为x轴的刻度标签,这样Matplotlib会自动匹配刻度和标签,不需要手动调用set_xticklabels:

# Add custom entries here
custom_df = dfAnimalRankings.loc[dfAnimalRankings['Animals'].isin(['Beetle'])]
# Create new df to show top 5, bottom 5, and Beetle
new_df = pd.concat([dfAnimalRankings[:5], dfAnimalRankings[-5:], custom_df])
new_df.sort_values(by=['Score'], inplace= True)
new_df.reset_index(drop=True, inplace= True)
# Plot the figure.
plt.figure(figsize=(12, 8))
# 修改这里:指定x为Animals列,y为Score列,自动生成正确的x轴标签
ax = new_df.plot(kind='bar', x='Animals', y='Score')
ax.set_title('Animal Rankings')
ax.set_xlabel('Animals')
ax.set_ylabel('Score')
# 移除错误的ax.set_xticklabels(x_labels)行
rects = ax.patches

def add_value_labels(ax, spacing=5):
    """Add labels to the end of each bar in a bar chart.
    Arguments:
        ax (matplotlib.axes.Axes): The matplotlib object containing the axes of the plot to annotate.
        spacing (int): The distance between the labels and the bars.
    """
    # For each bar: Place a label
    for rect in ax.patches:
        # Get X and Y placement of label from rect.
        y_value = rect.get_height()
        x_value = rect.get_x() + rect.get_width() / 2
        # Number of points between bar and label. Change to your liking.
        space = spacing
        # Vertical alignment for positive values
        va = 'bottom'
        # If value of bar is negative: Place label below bar
        if y_value < 0:
            # Invert space to place label below
            space *= -1
            # Vertically align label at top
            va = 'top'
        # Use Y value as label and format number with one decimal place
        label = "{:.1f}".format(y_value)
        # Create annotation
        ax.annotate(
            label,                      # Use `label` as label
            (x_value, y_value),         # Place label at end of the bar
            xytext=(0, space),          # Vertically shift label by `space`
            textcoords="offset points", # Interpret `xytext` as offset in points
            ha='center',                # Horizontally center label
            va=va)                      # Vertically align label differently for
                                        # positive and negative values.

# Call the function above. All the magic happens there.
add_value_labels(ax)
plt.savefig("image.png")

方法二:手动设置匹配的标签

如果你还是想手动设置x轴标签,确保使用new_df中的Animals列(和当前图表的11个刻度一一对应):

# 替换原来的ax.set_xticklabels(x_labels)为:
ax.set_xticklabels(new_df['Animals'], rotation=45) # rotation可选,防止标签重叠

额外提示

如果你的动物名称较长,建议添加rotation参数旋转标签,避免x轴标签重叠,提升可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 12:28:14