本文详细介绍Matplotlib中绘制折线图的函数plot()使用
折线图用来描述两个变量之间的关系,譬如方程y=ax+b中y随x变化而变化的关系;
Matplotlib中绘制折线图的函数为plot()。
plot()常用参数使用
import math
import matplotlib.pyplot as plt
plt.figure(dpi=120)
plt.plot(range(2,10),#x轴方向变量
[math.sin(i) for i in range(2,10)],#y轴方向变量
linestyle='--',#线型
linewidth=2,#线宽
color='red',#线和marker的颜色,当markeredgecolor markerfacecolor有设定时,仅仅控制line颜色
##marker的特性设置
marker='^',#marker形状
markersize='15',#marker大小
markeredgecolor='green',#marker外框颜色
markeredgewidth=2, #marker外框宽度
markerfacecolor='red',#marker填充色
fillstyle='top',#marker填充形式,可选{'full', 'left', 'right', 'bottom', 'top', 'none'}
markerfacecoloralt='blue',#marker未被填充部分颜色
markevery=2,#每隔一个画一个marker
label='sin(x)',#图例
alpha=0.3,#线和marker的透明度
)
##下面两条线使用默认参数绘制
plt.plot(range(2,10),[math.cos(i) for i in range(2,10)],label='cos(x)')
plt.plot(range(2,10),[2*math.cos(i)+1 for i in range(2,10)],label='2*cos(x)+1')
plt.legend()#绘制图例
plt.xlabel('x')
plt.ylabel('f(x)')
image
参考资料