【问题标题】:matplotlib: adding padding/offset to polar plots tick labelsmatplotlib:向极坐标图刻度标签添加填充/偏移
【发布时间】:2013-06-14 02:58:35
【问题描述】:

有没有办法增加极坐标图刻度标签 (theta) 的填充/偏移?

import matplotlib
import numpy as np
from matplotlib.pyplot import figure, show, grid

# make a square figure
fig = figure(figsize=(2, 2))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')
ax.set_yticklabels([])

r = np.arange(0, 3.0, 0.01)
theta = 2*np.pi*r
ax.plot(theta, r, color='#ee8d18', lw=3)
ax.set_rmax(2.0)
show()

我想让 theta 刻度标签远离极坐标图,这样它们就不会重叠。

【问题讨论】:

    标签: matplotlib labels axis-labels radial


    【解决方案1】:

    首先;看到您如何将 figsize 指定为 (2,2) 并让 ax 占据宽度和高度的 80%,您有非常少 剩余空间来填充刻度标签。这可能会导致刻度标签在图形的边缘处被“切断”。这可以很容易地被“修复”

    • 指定更大的figsize
    • 使ax 在 (2,2) 大小的图形上占用更少的空间
    • 为刻度标签使用较小的字体

    或这些的任何组合。在我看来,另一个更好的解决方案是使用子图而不是指定 Axes 的边界;

    ax = fig.add_subplot(111, polar=True, axisbg='#d5de9c')
    

    因为这使得使用 tight_layout() 方法成为可能,该方法会自动配置图形布局以很好地包含所有元素。

    然后转到手头的真正问题;填充。在PolarAxes 上,您可以设置theta 刻度的径向位置等。这是通过指定极轴半径的分数来完成的,您希望在其中放置刻度标签作为PolarAxesset_thetagrids() 方法的frac 参数的参数。该参数应该是您希望放置刻度标签的轴半径的一部分。 IE。对于frac frac > 1,它们将放置在轴外。

    你的代码可能是这样的:

    import numpy as np
    from matplotlib.pyplot import figure, show, grid, tight_layout
    # make a square figure
    fig = figure(figsize=(2, 2))
    ax = fig.add_subplot(111, polar=True, axisbg='#d5de9c')
    ax.set_yticklabels([])
    
    r = np.arange(0, 3.0, 0.01)
    theta = 2*np.pi*r
    ax.plot(theta, r, color='#ee8d18', lw=3)
    ax.set_rmax(2.0)
    
    # tick locations
    thetaticks = np.arange(0,360,45)
    
    # set ticklabels location at 1.3 times the axes' radius
    ax.set_thetagrids(thetaticks, frac=1.3)
    
    tight_layout()
    
    show()
    

    您应该为frac 尝试不同的值,以找到最适合您需要的值。

    如果您没有为参数frac 指定值,即frac 具有默认值None,则代码输出如下图。请注意绘图的半径是如何变大的,因为刻度标签不会像上面的示例那样“占用尽可能多的空间”。

    【讨论】:

    • 你知道默认的frac是什么吗?
    • @Annan 从this 看来frac 已被弃用。 “[...] 因此,不再应用 .PolarAxes.set_thetagrids 的 frac 参数。可以使用 .Axes.tick_params 或 .Axis.set_tick_params 的 pad 参数修改刻度填充。”
    • @Annan 但要回答你的问题,它曾经是1.1
    • 对于当前版本的 matplotlib,以下工作:ax.xaxis.set_tick_params(pad=10)
    猜你喜欢
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多