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()
补充⼀个设置坐标轴间隔的代码,这⾥未展⽰效果:
from matplotlib.pyplot import MultipleLocator#设置坐标轴刻度间
x_major_locator= MultipleLocator(31)#把x轴的刻度间隔设置为31,并存在变量⾥
y_major_locator= MultipleLocator(500000)#把y轴的刻度间隔设置为500000,并存在变量⾥
ax= a()#ax为两条坐标轴的实例
ax.xaxis.set_major_locator(x_major_locator)#把x轴的主刻度设置为31的倍数
ax.yaxis.set_major_locator(y_major_locator)#把y轴的主刻度设置为500000的倍数
结果
python中的⼩tips
np.arrage():开始值、终值和步长创建表⽰等差数列的⼀维数组。得到的结果数组不包含终值。
np.linspace():开始值、终值和元素个数创建表⽰等差数列的⼀维数组,可以通 过endpoint参数指定是否包含终值,默认值为True,即包含终值。
π:np.pi
⼀些属性
线的颜⾊:
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
linspace numpy‘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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论