plt.legend(loc='best',frameon=False)#去掉图例边框plt.legend(loc='best',edgecolor='blue')#设置图例边框颜色plt.legend(loc='best',facecolor='blue')#设置图例背景颜色,若无边框,参数无效 对于边框还可以采用面向对象方式: legend = plt.legend(["First","Second"]) frame=legend.get_frame() frame.set...
l1,= ax1.plot(x, x*x,'r') #这里关键哦 l2,= ax2.plot(x, x*x,'b') # 注意 plt.legend([l1, l2], ['first','second'], loc ='upper right') #其中,loc表示位置的; plt.show() 在legend的参数中, loc参数设置图例的显示位置的: 'best':0, (only implementedforaxes legends)(自适应...
plt.legend([p3, p4], ['label', 'label1'], loc='lower right',scatterpoints=1) # Add l1 as a separate artist to the axes plt.gca().add_artist(l1) import matplotlib.pyplot as plt line1, = plt.plot([1,2,3], label="Line 1", linestyle='--') line2, = plt.plot([3,2,1]...
plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.show() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 0x06 参考 1. matplotlib命令与格式:图例legend语法及设置 2. plt.legend( )函数,给图像加上图例 3. matplotlib.pyplot.legend...
当在单个子图中有多条线、多组标记等时,它们尤其有用。当调用ax.legend()时,每个没有以下划线开头的标签且包含在轴对象中的艺术家都会生成一个轴图例条目。像ax.scatter()和ax.plot()这样的绘图函数将label作为参数,默认情况下,这是创建图例时使用的标签。
3 再接着,我们使用numpy创建正弦,余弦曲线的点集合,并调用plot方法绘制:4 然后,我们只需要一行代码,plt.legend(loc='位置'), 把图例加上了:5 但是,我们可能满足于这种显示方式,我想要把图例单独单个显示,拆分出来,这里重要的代码就是,获取到legend ,调用add_artist方法,不然会被覆盖掉,到...
plt.xticks(month,['%d月'%i for i in month]) # x轴会自动只显示偶数刻度,所以要让他全部显示1-12 plt.plot(month, cost,label='支出') # 设置第一条线的标签名称label plt.plot(month,income,label='收入') plt.legend() # 图例使用默认位置(自动找到空位显示),名称使用每条线的label ...
plt.plot(x,x*x) plt.show() 具体实现效果: 5. 添加图例-legend 当线条过多时,我们设置不同颜色来区分不同线条。因此,需要对不同颜色线条做下标注,我们实用 legend() 接口来实现。 importnumpyasnp importmatplotlib.pyplotasplt # 显示中文 plt.rcParams[...
In the plt.plot() function, we defined a new argument label = where we parsed the legend label for each of the two lines plotted on the graph.Next, we introduced a new function plt.legend(), which displays the legend on the plot in the top left corner as the default position. The ...
当在单个子图中有多条线、多组标记等时,它们尤其有用。当调用ax.legend()时,每个没有以下划线开头的标签且包含在轴对象中的艺术家都会生成一个轴图例条目。像ax.scatter()和ax.plot()这样的绘图函数将label作为参数,默认情况下,这是创建图例时使用的标签。