Matplotlib双图例

Matplotlib双图例,matplotlib,legend,linear-regression,Matplotlib,Legend,Linear Regression,通过我的代码,我在图例中得到了两个相同的方程式。我不知道为什么会这样。我只想通过使它成为一个等式来纠正这个问题。我该怎么做?该方程是以下部分数据的直线拟合结果 提前谢谢 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,

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

提前谢谢

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()

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

#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')