Matplotlib - Move X-Axis label downwards, but not X-Axis Ticks

北战南征 提交于 2019-11-26 10:21:13

问题


I\'m using Matplotlib to plot a histogram. Using tips from my previous question: Matplotlib - label each bin, I\'ve more or less go the kinks worked out.

There\'s one final issue - previously - the x-axis label (\"Time (in milliseconds)\") was being rendered underneath the x-axis tickmarks (0.00, 0.04, 0.08, 0.12 etc.)

\"No

Using the advice from Joe Kingston (see question above), I tried using:

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

However, this moves both the x-axis tickmarks (0.00, 0.04, 0.08, 0.12 etc.), as well as the x-axis label (\"Time (in milliseconds)\"):

\"30

Is there any way to move only the x-axis label to underneath the three rows of figures?

Nb: You may need to open the PNGs below directly - Right Click on the image, then View Image (in FF), or Open image in new tab (Chrome). The image resize done by SO has rendered them nigh unreadable


回答1:


use labelpad parameter:

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

or set it after:

ax.xaxis.labelpad = 20



回答2:


If the variable ax.xaxis._autolabelpos = True, matplotlib sets the label position in function _update_label_position in axis.py according to (some excerpts):

    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))

You can set the label position independently of the ticks by using:

    ax.xaxis.set_label_coords(x0, y0)

that sets _autolabelpos to False or as mentioned above by changing the labelpad parameter.



来源:https://stackoverflow.com/questions/6406368/matplotlib-move-x-axis-label-downwards-but-not-x-axis-ticks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!