matplotlib.pyplot绘制图像之同一图中多条曲线对比

绘制sinx和cosx

# -*- coding:utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * (np.pi))  #numpy.linspace(开始,终值(含终值)),个数)
y1 = np.sin(x)
y2 = np.cos(x)

#画图
plt.title('Compare cosx with sinx')  #标题
#plt.plot(x,y)
#常见线的属性有:color,label,linewidth,linestyle,marker等
plt.plot(x, y1, color='cyan', label='sinx')
plt.plot(x, y2, 'b', label='cosx')#'b'指:color='blue'
plt.legend()  #显示上面的label
plt.xlabel('x')
plt.ylabel('f(x)')
plt.axis([0, 2*np.pi, -1, 1])#设置坐标范围axis([xmin,xmax,ymin,ymax])
#plt.ylim(-1,1)#仅设置y轴坐标范围
plt.show()

结果

matplotlib.pyplot绘制图像之同一图中多条曲线对比_第1张图片

python中的小tips

  • np.arrage():开始值、终值和步长创建表示等差数列的一维数组。得到的结果数组不包含终值。
  • np.linspace():开始值、终值和元素个数创建表示等差数列的一维数组,可以通 过endpoint参数指定是否包含终值,默认值为True,即包含终值。
  • π:np.pi

一些属性

线的颜色:

‘b’ blue 
‘g’ green 
‘r’ red 
‘c’ cyan 
‘m’ magenta 
‘y’ yellow 
‘k’ black 
‘w’ white

线的形状:

‘-’ solid line style 
‘–’ dashed line style 
‘-.’ dash-dot line style 
‘:’ dotted line style

点的标记:

‘.’ point marker 
‘,’ pixel marker 
‘o’ circle marker 
‘v’ triangle_down marker 
‘^’ triangle_up marker 
‘<’ triangle_left marker 
‘>’ triangle_right marker 
‘1’ tri_down marker 
‘2’ tri_up marker 
‘3’ tri_left marker 
‘4’ tri_right marker 
‘s’ square marker 
‘p’ pentagon marker 
‘*’ star marker 
‘h’ hexagon1 marker 
‘H’ hexagon2 marker 
‘+’ plus marker 
‘x’ x marker 
‘D’ diamond marker 
‘d’ thin_diamond marker 
‘|’ vline marker 
‘_’ hline marker

你可能感兴趣的:(python数据分析)