我发现这个answer是一个问题,其中提供了极坐标图之外的比例尺方法(那里的add_scale
函数)。我也尝试将其应用于我的数据,并且几乎可以解决。如图所示,比例尺顶部存在问题。我如何去除秤的这一太长部分?
第二个问题是:如何将这个小数位的值四舍五入到两位数。我看过this问题,但没有解决。
我将把代码重新发布到这里,并全部归功于@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())
上一行用于定义所添加刻度上的最小值和最大值。