利用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