【转】matplotlib制图——图例legend
转自: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()