问题
I've found this answer to a question where an approach for a scale outside a polar plot is provided (the add_scale
function there). I tried to apply this to my data as well and it almost worked out. As shown in the picture, there is an issue at the top of the scale bar. How can I remove this little too long part of the scale?
Second question is: How can I round the values in this scale to two digits. I've had a look at this question, but it didn't work out.
I'll re-post the code here with all credits to @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
Edit:
I tried to round the values with the following line
scale_ax.set_yticks(round(float(ax.get_yticks()), 2))
instead of
scale_ax.set_yticks(ax.get_yticks())
but this didn't improve my scale as shown below, although my ticks
are rounded now:
回答1:
You need to define or change ylimit for your graph.
scale_ax.set_ylim(ax.get_rorigin(), ax.get_rmax())
Above line is used to define min and max value on the added scale.
来源:https://stackoverflow.com/questions/62000324/round-and-draw-external-scale-in-polar-plot