【问题标题】:Matplotlib - Move X-Axis label downwards, but not X-Axis TicksMatplotlib - 向下移动 X 轴标签,但不是 X 轴刻度
【发布时间】:2011-09-18 09:08:05
【问题描述】:

我正在使用 Matplotlib 绘制直方图。 使用我上一个问题的提示:Matplotlib - label each bin, 我或多或少地解决了问题。

最后一个问题 - 之前 - x 轴标签(“时间(以毫秒为单位)”)在 x 轴刻度线(0.00、0.04、0.08、0.12 等)下方呈现

使用 Joe Kingston 的建议(请参阅上面的问题),我尝试使用:

ax.tick_params(axis='x', pad=30)

但是,这会移动 x 轴刻度线(0.00、0.04、0.08、0.12 等)以及 x 轴标签(“时间(以毫秒为单位)”):

有没有办法只将x轴标签移动到三行数字的下方?

注意:您可能需要直接打开下面的 PNG - 右键单击​​图像,然后查看图像(在 FF 中),或在新选项卡中打开图像(Chrome)。 SO 完成的图像大小调整使它们几乎不可读

【问题讨论】:

    标签: python matplotlib graphing


    【解决方案1】:

    如果变量 ax.xaxis._autolabelpos = True,matplotlib 根据(部分摘录)在 axis.py 中的函数 _update_label_position 中设置标签位置:

        bboxes, bboxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
        bbox = mtransforms.Bbox.union(bboxes)
        bottom = bbox.y0
        x, y = self.label.get_position()
        self.label.set_position((x, bottom - self.labelpad * self.figure.dpi / 72.0))
    

    您可以使用以下方法独立于刻度设置标签位置:

        ax.xaxis.set_label_coords(x0, y0)
    

    将 _autolabelpos 设置为 False 或如上所述通过更改 labelpad 参数。

    【讨论】:

      【解决方案2】:

      使用labelpad参数:

      pl.xlabel("...", labelpad=20)
      

      或在之后设置:

      ax.xaxis.labelpad = 20
      

      【讨论】:

      • 如果我使用这个并保存图,图被裁剪,我再也看不到标签了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 2019-12-18
      • 2020-03-29
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多