轉自:https://www.cnblogs.com/alimin1987/p/8047833.html
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
x= np.arange(1,20,1)
plt.plot(x,x**2,label='Fast')#label為標簽
plt.plot(x,x*2,label='Mormal')#l
plt.legend(loc=0,ncol=2)#圖例及位置: 1右上角,2 左上角 loc函數可不寫 0為最優 ncol為標簽有幾列
此處若是不寫plt.legend,則不會顯示標簽
plt.show()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
x= np.arange(1,20,1)
plt.plot(x,x**2)#,label='Fast')#label為標簽
plt.plot(x,x*2)#,label='Mormal')#l
plt.legend(['Fast','Mormal'])#
plt.show()

另一種寫法 面向對象
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
x= np.arange(1,20,1)
fig=plt.figure()
ax=fig.add_subplot(111)
plt.plot(x,x*2,label='tset') #
ax.legend()
plt.show()

