Matplotlib双重传奇

问题描述 投票:1回答:1

使用我的代码,我在图例中得到了两个相同的方程式。我不知道为什么会这样。我只想通过使它只有一个方程来纠正这个问题。我怎样才能做到这一点?该等式是以下一些数据的线拟合结果。

提前致谢!

import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py
import plotly.tools as tls
from sympy import S, symbols
import sympy

y = [2.7,2.3,1.9,1.5,1.3,1.0,0.8,0.6,0.5,0.4,0.2,0.1,0.0,0.0,-0.20,-0.2]

y = [i*10**(-16) for i in y]
x = [0,0.05,0.10,0.15,0.20,0.25,0.30,0.40,0.45,0.50,0.55,0.60,0.65,0.70,0.75,0.80]

e_y = [10**(-17)]* 16

e_x = [0.001] * 16

fig= plt.figure()
ax = fig.add_subplot(111)
ax.errorbar(x,y, yerr=e_y,xerr=0.001,fmt='-o')
ax.set_title('Current vs. Potential')
ax.set_xlabel('Retarding Potential')
ax.set_ylabel('Photocell Current')



x=x[:7]
y=y[:7]
e_y=e_y[:7]
e_x=e_x[:7]

#line fit:
fit=np.polyfit(x,y,1)
fit_fn = np.poly1d(fit)
a=symbols("x")


line = sum(S(format(v))*a**i for i, v in enumerate(fit[::-1]))

eq_latex = sympy.printing.latex(line)

plt.plot(x,y,x,fit_fn(x),label="${}$".format(eq_latex))
plt.legend(fontsize='small')

plt.show()
matplotlib legend linear-regression
1个回答
1
投票

我用以下方法解决了这个问题:

#import matplotlib.patches as mpatches
plt.plot(x,y,x,fit_fn(x))
eqn = mpatches.Patch(color='green',label="${}$".format(eq_latex))
plt.legend(handles=[eqn])

代替

plt.plot(x,y,x,fit_fn(x),label="${}$".format(eq_latex))
plt.legend(fontsize='small')
© www.soinside.com 2019 - 2024. All rights reserved.