【发布时间】:2022-01-10 04:08:59
【问题描述】:
我正在尝试使用 figtext() 在下图中添加文本,但下面的书面代码不起作用。我很感激任何帮助。
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
x = np.array([0.00161, 0.00322, 0.00483, 0.00645, 0.00806])
y = np.array([0.005, 0.006, 0.007, 0.008, 0.013])
a, b = np.polyfit(x, y, 1)
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
slope = round(slope, 4)
intercept = round(intercept, 4)
r_value = r_value
r_squared = round(r_value ** 2, 4)
plt.scatter(x, y, color='purple')
plt.figtext(-25, 25, f"y={slope}x+{intercept}")
plt.figtext(-25, 25, f"R^2={r_squared}")
plt.text(1, 17, 'y = ' + '{:.2f}'.format(b) + ' + {:.2f}'.format(a) + 'x', size=14)
plt.xlabel("Concentration")
plt.ylabel("Absorbance")
plt.title("Phosphorus Calibration Curve")
plt.plot(x, a*x+b, color='steelblue', linestyle='--', linewidth=2)
plt.show()
【问题讨论】:
-
您是否尝试先阅读
figtext文档,特别是围绕x和y参数的预期?plt.text也是如此。 -
ax = plt.gca()和ax.text(...)可能更合适。
标签: python python-3.x numpy matplotlib scipy.stats