我有两个pandas pandas rame对象,名为"freq"和"freq How to fix overlapping annotations / text0",每个对象都是5个数字的载体.我想将它们绘制在同一个图表上,并在图表上使用plt.text来显示y值."freq"和"freq How to fix overlapping annotations / text0"是从"pr"和"prHow to fix overlapping annotations / text0"构建的,两者都是通过极其冗长的过程生成的.为了避免plt.text重叠,我遵循链接How to fix overlapping annotations / text并安装了包adjust_text. 然而,它并没有像我想要的那样工作,而且我得到的图表与图表上的数字仍然有重叠,请参阅下图:
我的相关进口和代码如下
import pandas as pd
import matplotlib.pyplot as plt
from adjustText import adjust_text
freq = pd.DataFrame(np.average(pr, axis=0), index=pr.columns)
freq1000 = pd.DataFrame(np.average(pr1000, axis=0), index=pr1000.columns)
plt.plot(freq, label='n=500')
plt.plot(freq1000, label='n=1000')
plt.legend(loc='best')
plt.xlabel('x')
plt.ylabel('y')
plt.title('curve high vs low,gtype{}'.format(type_))
# plt.close()
texts=[]
for x, y in zip(freq.index, freq.values):
texts.append(plt.text(x, y,'%.3f' % y))
adjust_text(texts, only_move={'points':'y', 'texts':'xy'}, arrowprops=dict(arrowstyle="->", color='r', lw=0.5))
texts=[]
for x, y in zip(freq1000.index, freq1000.values):
texts.append(plt.text(x, y,'%.3f' % y))
adjust_text(texts, only_move={'points':'y', 'texts':'y'}, arrowprops=dict(arrowstyle="->", color='r', lw=0.5))
我应该如何修复代码来解决重叠问题?谢谢!