首页 > 解决方案 > 有没有一种简单的方法可以让 matplotlib 文本框像图例一样默认浮动?

问题描述

在广泛阅读 matplotlib 文档后,我能够编写我想要的代码,但我很好奇是否有更简单的解决方案。我将在下面添加相关代码。

import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText

li_green = "#6BA755"
li_grey = "#D7D7D7" # rgb hex-code for the grey outline around the legend box
ax.plot(data_date, cumsum_income, ".-", label="Raw")
ax.plot(data_date, exp_func(months, *popt), label="Fit", color=li_green)

str_fit_eqn = '\n'.join(("Fit Equation:",
                         r"%.2f$e^{%.2fx}$%.2f" % (popt[0], popt[1], popt[2]),
                         r"$R^2$= %.2f" % (r_squared,)))
props = dict(boxstyle='round', facecolor="white", edgecolor=li_grey) # these are the text box parameters
anchored_text = AnchoredText(str_fit_eqn, loc=4, frameon=False, prop=dict(bbox=props))
ax.add_artist(anchored_text)

ax.legend(loc="best")
plt.show()

表达我的问题的另一种方式是,是否有一个参数可以完成这个文本框的轮廓,或者我应该使用 AnchoredText 以外的其他东西吗?

标签: pythonmatplotlibtextlegend

解决方案


推荐阅读