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

如何解决x轴刻度标签穿越x轴进入图表区域的问题?

问题

X轴刻度标签穿越X轴进入图表区域,同时X轴刻度本身不再显示。已尝试以下操作但未解决:

  • 设置刻度可见
  • 调整内边距(padding)
  • 尝试水平对齐选项

附代码:

my_figure = plt.figure(**{'dpi': 100.0,'edgecolor': 'black','facecolor': 'black','figsize': (8.0, 6.0),'linewidth': 1.0})
ax = my_figure.add_subplot(111)
ax.bxp(**{'boxprops': {'alpha': 0.8, 'facecolor': 'C0'},
          'bxpstats': [{'label': 'obs5','mean': 74.00224000000001,'med': 74.004,'q1': 73.996,'q3': 74.009,'whishi': 73.984,'whislo': 74.014}],
          'capprops': {'color': 'C0'},
          'patch_artist': True,
          'positions': [0],
          'showfliers': False,
          'vert': False,
          'whiskerprops': {'color': 'C0'},
          'widths': 0.5,
          'zorder': 1.0})
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.tick_params(axis='x', which='major', **{'bottom': False,'color': 'white','direction': 'inout','length': 3.0,'pad': 7.0,'top': False,'width': 1.0,'zorder': 1.0})
ax.tick_params(axis='x', which='major', labelbottom=True)
ax.set_xticks(ticks=ax.get_xticks(minor=False),
              labels=['73.980', '73.985', '73.990', '73.995', '74.000', '74.005', '74.010', '74.015', '74.020'],
              minor=False,
              **{'alpha': 1.0,'color': 'white','fontfamily': 'serif','fontsize': 'small','fontstretch': 'condensed','fontstyle': 'oblique','fontvariant': 'small-caps','fontweight': 'light','horizontalalignment': 'center','linespacing': 2.0,'multialignment': 'center','rotation': 45.0,'snap': True,'verticalalignment': 'center'})
ax.xaxis.set_minor_formatter(plt.NullFormatter())
plt.show()
解决步骤

1. 修复X轴刻度不显示的问题

代码里ax.tick_params设置了bottom: False,这会直接隐藏X轴的主刻度线,把它改成True即可恢复刻度显示,同时增大pad值给旋转后的标签留出更多垂直空间:

ax.tick_params(axis='x', which='major', **{'bottom': True,'color': 'white','direction': 'inout','length': 3.0,'pad': 15.0,'top': False,'width': 1.0,'zorder': 1.0})

2. 调整标签位置与底部边距

  • 将标签的horizontalalignment从center改为right,配合45度旋转,让标签末端对齐X轴刻度,减少垂直空间占用,避免进入图表区域
  • 在plt.show()前添加plt.subplots_adjust(bottom=0.2),增加子图底部空白,确保标签完全显示在图表外

修改后的完整代码

import matplotlib.pyplot as plt

my_figure = plt.figure(**{'dpi': 100.0,'edgecolor': 'black','facecolor': 'black','figsize': (8.0, 6.0),'linewidth': 1.0})
ax = my_figure.add_subplot(111)
ax.bxp(**{'boxprops': {'alpha': 0.8, 'facecolor': 'C0'},
          'bxpstats': [{'label': 'obs5','mean': 74.00224000000001,'med': 74.004,'q1': 73.996,'q3': 74.009,'whishi': 73.984,'whislo': 74.014}],
          'capprops': {'color': 'C0'},
          'patch_artist': True,
          'positions': [0],
          'showfliers': False,
          'vert': False,
          'whiskerprops': {'color': 'C0'},
          'widths': 0.5,
          'zorder': 1.0})
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(True)
# 修改bottom为True,增大pad值
ax.tick_params(axis='x', which='major', **{'bottom': True,'color': 'white','direction': 'inout','length': 3.0,'pad': 15.0,'top': False,'width': 1.0,'zorder': 1.0})
ax.tick_params(axis='x', which='major', labelbottom=True)
ax.set_xticks(ticks=ax.get_xticks(minor=False),
              labels=['73.980', '73.985', '73.990', '73.995', '74.000', '74.005', '74.010', '74.015', '74.020'],
              minor=False,
              **{'alpha': 1.0,'color': 'white','fontfamily': 'serif','fontsize': 'small','fontstretch': 'condensed','fontstyle': 'oblique','fontvariant': 'small-caps','fontweight': 'light','horizontalalignment': 'right','linespacing': 2.0,'multialignment': 'center','rotation': 45.0,'snap': True,'verticalalignment': 'center'})
ax.xaxis.set_minor_formatter(plt.NullFormatter())
# 增加底部边距
plt.subplots_adjust(bottom=0.2)
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 19:05:23