python第二阶段(22)numpy入门基础-可视化之图例

这篇博客介绍了numpy库和matplotlib库在Python中进行数据可视化的基础,特别是图例显示的功能。通过三个示例详细讲解了legend函数的三种使用方式:直接调用legend()、指定labels参数和设置handles及labels参数。每个示例都展示了如何在图表上添加并定制图例,帮助读者理解如何在matplotlib图形中清晰地标识不同数据系列。
摘要由CSDN通过智能技术生成

numpy入门基础-可视化之图例


演示: 在这里插入图片描述

1、图例显示legend函数

在轴上放置图例

matplotlib.pyplot.legend(*args, **kwargs)

三种形式:(用到再自行百度吧)

legend()#讲这个
legend(labels)
legend(handles, labels)

2、示例

in:

import numpy as np #导入 numpyas
import matplotlib.pyplot as plt #导入 matplotlib.pyplot
#第一种显示图例的方式
x=np.arange(1,11,1)
plt.plot(x,x*2,label='Normal')
plt.plot(x,x*3,label='Fast')
plt.plot(x,x*4,label='Faster')
plt.legend()

out:
在这里插入图片描述
in:

#第二种显示图例的方式
x=np.arange(1,11,1)
plt.plot(x,x*2)
plt.plot(x,x*3)
plt.plot(x,x*4)
plt.legend(['Normal','Fast','Faster'])

out:
在这里插入图片描述
in:

#第三种显示图例的方式,比较麻烦
x=np.arange(1,100)
fig=plt.figure()
ax=fig.add_subplot(111)
s,=plt.plot(x,x*2)
s.set_label('label')
l,=plt.plot(x,x*4)
l.set_label('label2')
ax.legend()

out:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值