1
我用matplotlib绘制两行文字,并希望标记最大的改进。我用ax.annotate
,得到了以下不希望的结果,如何标记matplotlib中两行之间的百分比变化?
这里是源代码。
from __future__ import division
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2)
x = range(10)
y1 = range(10)
y2 = [2*i for i in range(10)]
# plot line graphs
ax1.plot(x, y1)
ax1.plot(x, y2)
# the 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'))
# for showing the expected result
ax2.plot(x, y1)
ax2.plot(x, y2)
plt.show()
是否有更好的方法,以纪念两行之间的百分比变化?