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,

enter image description here

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() 

enter image description here


Comments

Popular posts from this blog

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

wordpress - (T_ENDFOREACH) php error -

Using django-mptt to get only the categories that have items -