在极坐标图中舍入并绘制外部刻度

Round and draw external scale in polar plot

我发现这个 answer 是一个问题,其中提供了极坐标图之外的比例方法(那里的 add_scale 函数)。我也尝试将其应用于我的数据,并且几乎成功了。如图所示,比例尺顶部存在问题。我怎样才能去掉这个太长的部分呢?

第二个问题是:如何将此比例中的值四舍五入为两位数。我看过 问题,但没有解决。

我将在此处重新post 代码,所有功劳都归功于 @lostin

# to control how far the scale is from the plot (axes coordinates)    
def add_scale(ax, X_OFF, Y_OFF):
    # add extra axes for the scale
    X_OFFSET = X_OFF
    Y_OFFSET = Y_OFF
    rect = ax.get_position()
    rect = (rect.xmin-X_OFFSET, rect.ymin+rect.height/2-Y_OFFSET, # x, y
            rect.width, rect.height/2) # width, height
    scale_ax = ax.figure.add_axes(rect)
   # if (X_OFFSET >= 0):
        # hide most elements of the new axes
    for loc in ['right', 'top', 'bottom']:
        scale_ax.spines[loc].set_visible(False)
#    else:
#        for loc in ['right', 'top', 'bottom']:
#            scale_ax.spines[loc].set_visible(False)
    scale_ax.tick_params(bottom=False, labelbottom=False)
    scale_ax.patch.set_visible(False) # hide white background
    # adjust the scale
    scale_ax.spines['left'].set_bounds(*ax.get_ylim())
    # scale_ax.spines['left'].set_bounds(0, ax.get_rmax()) # mpl < 2.2.3
    scale_ax.set_yticks(ax.get_yticks())
    scale_ax.set_ylim(ax.get_rorigin(), ax.get_rmax())
    # scale_ax.set_ylim(ax.get_ylim()) # Matplotlib < 2.2.3

编辑:

我尝试使用以下行对值进行舍入

scale_ax.set_yticks(round(float(ax.get_yticks()), 2))

而不是

scale_ax.set_yticks(ax.get_yticks())

但这并没有改善我的比例,如下所示,尽管我的 ticks 现在是四舍五入的:

您需要为图表定义或更改 ylimit。

scale_ax.set_ylim(ax.get_rorigin(), ax.get_rmax())

上一行用于定义添加比例的最小值和最大值。