python如何使用python绘制折线图?
使用python绘制折线图过程
1、导入库和设置输入折线图数据
importnumpyasnp
importmatplotlib.pyplotasplt
#x轴刻度标签
x_ticks=['a','b','c','d','e','f']
#x轴范围(0,1,...,len(x_ticks)-1)
x=np.arange(len(x_ticks))
#第1条折线数据
y1=[5,3,2,4,1,6]
#第2条折线数据
y2=[3,1,6,5,2,4]
2、设置画布大小并绘制折线
plt.figure(figsize=(10,6))
#画第1条折线,参数看名字就懂,还可以自定义数据点样式等等。
plt.plot(x,y1,color='#FF0000',label='label1',linewidth=3.0)
#画第2条折线
plt.plot(x,y2,color='#00FF00',label='label2',linewidth=3.0)
#给第1条折线数据点加上数值,前两个参数是坐标,第三个是数值,ha和va分别是水平和垂直位置(数据点相对数值)。
fora,binzip(x,y1):
plt.text(a,b,'%d'%b,ha='center',va='bottom',fontsize=18)
#给第2条折线数据点加上数值
fora,binzip(x,y2):
plt.text(a,b,'%d'%b,ha='center',va='bottom',fontsize=18)
#画水平横线,参数分别表示在y=3,x=0~len(x)-1处画直线。
plt.hlines(3,0,len(x)-1,colors="#000000",linestyles="dashed")
3、添加x轴和y轴刻度标签
plt.xticks([rforrinx],x_ticks,fontsize=18,rotation=20)
plt.yticks(fontsize=18)
#添加x轴和y轴标签
plt.xlabel(u'x_label',fontsize=18)
plt.ylabel(u'y_label',fontsize=18)
4、绘制折线图标题和图例
#标题
plt.title(u'Title',fontsize=18)
#图例
plt.legend(fontsize=18)
5、保存完成
#保存图片
plt.savefig('./figure.pdf',bbox_inches='tight')
#显示图片
plt.show()
以上就是如何使用python绘制折线图,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中mat是什么意思?
python中mat是什么意思?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运...详情>>
2023-11-10 23:22:48
python上下文管理的使用场景
python上下文管理的使用场景本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、上下文管理的使用场景凡是要在代码块前后插入代码的...详情>>
2023-11-10 20:07:44
python常量折叠是什么
python常量折叠是什么1、概念所谓常量折叠,指的是在编译时就查找并计算常量表达式,而不是在运行时再对其进行计算,从而会使运行时更加精简和...详情>>
2023-11-10 19:11:50
pythonTCP与UDP的不同点分析
python中TCP与UDP的不同点分析(推荐操作系统:windows7系统、Python3.9.1、DELLG3电脑。)1、不同点面向连接(确认有创建三方交握,连接已创建才...详情>>
2023-11-10 18:16:05热门推荐
技术干货






