%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-whitegrid') import numpy as np x = np.linspace(0,10,30) y = np.sin(x) plt.plot(x,y,'o',color='black')
plt.plot 的第三个参数是一个字符,表示图形符号的类型。具体的几种类型参见如下代码:
1 2 3 4 5 6 7 8 9 10
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.style.use('seaborn-whitegrid') rng = np.random.RandomState(0) for i in ['o','.',',','x','+','v','<','>','s','d']: plt.plot(rng.rand(5),rng.rand(5),i,label = "i='{0}'".format(i)) plt.legend(numpoints=1) plt.xlim(0,1.8) plt.axis('tight')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.style.use('seaborn-whitegrid') x = np.random.randn(10) y = np.random.randn(10) sizes= 1000*(np.random.RandomState(0).rand(10)) color = ['r','w','g','b','c','m','y','k','g','b'] plt.scatter(x,y,c=color,s=sizes,marker='o')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.style.use('seaborn-whitegrid') x = np.linspace(0,10,50) dy = 0.8 y = np.sin(x)+dy*np.random.randn(50) plt.errorbar(x,y,yerr=dy,fmt='-b',ecolor='lightblue')
关于这个函数来看一下帮助文档:
1 2 3 4 5 6 7
plt.errorbar(x,y,yerr=None,xerr=None, fmt='',ecolor=None,elinewidth=None,capsize=None, barsabove=False,lolims=False,uplims=False, xlolims=False,xuplims=False,errorevery=1, capthick=None,*,data=None,**kwargs,) #Plot y versus x as lines and/or markers with attached errorbars. #将y与x绘制为带有误差条的直线或标记
参数解析
1.x,y 参数定义数据的位置,xerr、yerr 定义错误栏的尺寸。
1 2 3 4 5 6 7
#xerr, yerr : scalar or array-like, shape(N,) or shape(2,N), optional #The errorbar sizes: #- scalar: Symmetric +/- values for all data points. #- shape(N,): Symmetric +/-values for each data point. #- shape(2,N): Separate - and + values for each bar. First rowcontains the lower errors, the second row contains the upper errors. #-*None*: No errorbar. #综上,xerr、yerr 可以是一个标量也可以是一组与点集匹配个数的数值,且所以的偏差数值都为正
1 2 3 4 5 6 7
%matplotlib inline import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10,100) y = np.sin(x) plt.errorbar(x,y,xerr=0.1,yerr=0.1,ecolor='lightblue') plt.axis('tight')
1 2 3 4 5 6 7 8
%matplotlib inline import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10,100) y = np.sin(x) dx = np.random.randn(100) plt.errorbar(x,y,xerr=dx,yerr=0.1,ecolor='lightblue') plt.axis('tight')
2.fmt/ecolor
定义线条输出的样式/定义颜色参数。
3.elinewidth/capsize
定义误差线的宽度,默认无/定义误差线的长度。
4.capthick
以点为单位控制误差线的粗细。
1 2 3 4 5 6 7 8
%matplotlib inline import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10,100) y = np.sin(x) dx = np.random.randn(100) plt.errorbar(x,y,xerr=dx,yerr=0.1,fmt=':g',ecolor='r',elinewidth=0.5,capsize=0.8,capthick=0.1) plt.axis('tight')
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10,100) y = np.sin(x) dx = np.random.randn(100) plt.errorbar(x,y,xerr=dx,yerr=0.1,fmt=':g',ecolor='r',elinewidth=0.5,capsize=0.8,capthick=0.1,barsabove='lolims',errorevery=5) plt.axis('tight')
#Signature: plt.fill_between(x,y1,y2=0,where=None, interpolate=False,step=None,*, data=None,**kwargs,) #Docstring: #Fill the area between two horizontal curves. #功能就是将两个坐标(x,y1),(x,y2)之间的区域填充
参数说明
1.x,y1,y2
都是数组,x 定义曲线节点的 x 坐标,y1 定义第一条曲线的节点的 y 坐标, y2 定义第二条曲线的节点的 y 坐标。
2.where 、interpolate
where 定义填充区域的坐标以此来排除某些地方不被填充。
interpolate 的功能是将填充交叉的地方设置为填充还是减去。默认为减去。
.3.step {‘pre’, ‘post’, ‘mid’}
定义步骤,如果给值,三个关键词的情况如下:
pre:x[i-1] ~ x[i]
post: x[i] ~ x[i+1]
mid: x位置的中间
1 2 3 4 5 6 7 8 9 10
import matplotlib.pyplot as plt import numpy as np z = np.linspace(0,2,5) x = np.array([0.1,0.2,0.3,0.4]) y = np.array([0.2,0.3,0.4,0.5]) dx = np.array([0.2,0.3,0.4,0.5]) dy = np.linspace(0,1,4) plt.plot(x,y,'or') plt.plot(x,dy,'-',color='gray') plt.fill_between(x,y,dy,color='gray',alpha=0.2)