利用matplotlib制作折线图

本文介绍了如何使用matplotlib库在Python中创建折线图,包括设置折线样式、折点属性,以及调整图像大小和保存格式。同时,还讨论了在图表中显示中文标签的方法和创建多图及子图的功能。
摘要由CSDN通过智能技术生成

利用matplotlib制作折线图

导入库/包:
import numpy as np
from matplotlib import pyplot as plt
代码块:
x = range(1,8) #x轴的位置
y = [17,17,18,15,11,11,13]
#传入x和y,通过plot画折线图
# plt.plot(x,y)
#折线图的颜色和形状设置
plt.plot(x,y,color='red',alpha=0.5,linestyle='-.',linewidth=3)
#折点样式
plt.plot(x,y,marker='o',color='red',markersize='20',markeredgecolor='g',markeredgewidth = 5)
plt.show()

基础属性设置

  • alpha=0.5 : 折线的透明度(0-1)
  • linestyle=’–’ : 折线的样式
  • linewidth=3 : 折线的宽度

线的样式
实线(solid)( - )
短线(dashed)( – )
短点相间线(dashdot)( -.)
虚点线(dotted)( :)

折点样式
折点样式:marker=‘o’
折点大小:markersize=‘20’
折点颜色:markeredgecolor=‘g’
折点外围大小:markeredgewidth = 5
折点形状选择:
-’ ‘’ ‘-.’ ‘:’ ‘.’ ‘,’ ‘o’ ‘v’ ‘^’ ‘<’ ‘>’ ‘1’ ‘2’ ‘3’ ‘4’ ‘s’ ‘p’ ’ * ’ ‘h’ ‘H
+’ ‘x’ ‘D’ ‘d’ ‘|’ ‘_
在这里插入图片描述
设置图片的大小和保存
设置图片的大小:
figsize:指定figure的宽和高,单位为英寸;
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是21*30cm的纸张

from matplotlib import pyplot as plt
import random
x = range(2,26,2) #x轴的位置
y = [random.randint(15,30) for i in x]
#设置图片大小
#设置画布对象
plt.figure(figsize
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值