python - How do I mark the percentage change between two lines in matplotlib? -
i plot 2 lines matplotlib , mark maximum improvements. use ax.annotate
, got following undesirable result,
here source code.
from __future__ import division import matplotlib.pyplot plt fig, (ax1, ax2) = plt.subplots(1, 2) x = range(10) y1 = range(10) y2 = [2*i in range(10)] # plot line graphs ax1.plot(x, y1) ax1.plot(x, y2) # maximum improvements x_max = max(x) y1_max = max(y1) y2_max = max(y2) improvements = "{:.2%}".format((y2_max-y1_max)/y1_max) # percentage ax1.annotate(improvements, xy=(x_max, y2_max), xycoords='data', xytext=(x_max, y1_max), textcoords='data', color='r', arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color='r')) # showing expected result ax2.plot(x, y1) ax2.plot(x, y2) plt.show()
is there better way mark percentage change between 2 lines?
i agree @hobenkr. have split annotation on 2 annotationa: first arrow , second text label. text may set bold
weight.
from __future__ import division import matplotlib.pyplot plt fig, (ax1, ax2) = plt.subplots(1, 2) x = range(10) y1 = range(10) y2 = [2*i in range(10)] # plot line graphs ax1.plot(x, y1) ax1.plot(x, y2) # maximum improvements x_max = max(x) y1_max = max(y1) y2_max = max(y2) improvements = "{:.2%}".format((y2_max-y1_max)/y1_max) # percentage # plot arrow ax1.annotate('', xy=(x_max, y2_max), xycoords='data', xytext=(x_max, y1_max), textcoords='data', arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color='r', linewidth=3.5)) # add annotate arrow ax1.annotate(improvements, xy=(x_max, y1_max + .3 * (y2_max-y1_max)), xycoords='data', xytext=(-50, 0), textcoords='offset points', color='r', weight='bold', size=10) # showing expected result ax2.plot(x, y1) ax2.plot(x, y2) plt.show()
Comments
Post a Comment