手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

python如何在matplotlib中绘制图例legend?

时间:2024-10-13 02:49:10

1、首先导入使用到的包,matplotlib.pyplot, numpy:

python如何在matplotlib中绘制图例legend?

2、接着设置图的大小,添加子图,figsize用于设置图的大小:

python如何在matplotlib中绘制图例legend?

3、再接着,我们使用numpy创建正弦,余弦曲线的点集合,并调用plot方法绘制:

python如何在matplotlib中绘制图例legend?

4、然后,我们只需要一行代码,plt.legend(loc='位置'), 把图例加上了:

python如何在matplotlib中绘制图例legend?

5、但是,我们可能满足于这种显示方式,我想要把图例单独单个显示,拆分出来,这里重要的代码就是,获取到legend ,调用add_artist方法,不然会被覆盖掉,到此,legend图例的操作完成了。

python如何在matplotlib中绘制图例legend?

6、一个好看的图例:

python如何在matplotlib中绘制图例legend?
© 手抄报圈