概要
以下整理了从 函数功能,调用签名,参数说明和调用展示四个方面来阐述API函数的使用方法和技术细节.
一.函数plot()
函数功能:展现变量的变换趋势
相关参数:
x:x轴上的数值;
y:y轴上的数值;
ls:折线的线条风格;
lw:折线图的线条宽度;
label:标记图形内容的标签文本
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.cos(x)
plt.plot(x,y,ls = "-",lw = 2,label = "plot figure")
plt.legend()
plt.show()
二.函数scatter()
函数功能:展现变量之间的关系
相关参数:
x:x轴上的数值;
y:y轴上的数值;
c:散点图中的标记的颜色;
label:标记图形内容的标签文本
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.random.rand(1000)
plt.scatter(x,y,label = "scatter figure")
plt.legend()
plt.show()
三.函数xlim() 或者 ylim()
函数功能:展现x轴/y轴 的数值显示范围
相关参数:
xmin:x轴上的最小值;
xmax:x轴上的最大值;
平移性:xlim()函数 类比ylim()函数;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.random.rand(1000)
plt.scatter(x,y,label = "scatter figure")
plt.legend()
plt.xlim(0.05,10)
plt.ylim(0,1)
plt.show()
四.函数xlabel() 或者 ylabel()
函数功能:展现x轴/y轴 的标签文本
相关参数:
string:标签文本内容;
平移性:xlabel()函数 类比ylabel()函数;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot figure")
plt.legend()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
五.函数grid()
函数功能:绘制刻度线的网格线
相关参数:
linestyle:网格线的线条风格;
color:网格线的线条颜色;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot figure")
plt.legend()
plt.grid(linestyle = ":",color = "r")
plt.show()
六.函数axhline() 或者 axvline()
函数功能:绘制平行于x轴/y轴 的水平/垂直 参考线
相关参数:
y:水平参考线的出发点;
x:垂直参考线的出发点;
c:参考线的线条颜色;
ls:参考线的线条风格;
lw:参考线的线条宽度;
平移性:axhline()函数 类比axvline()函数;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot figure")
plt.legend()
plt.axhline(y = 0.0,c = "r",ls = "--",lw = 2)
plt.axvline(x = 4.0,c = "r",ls = "--",lw = 2)
plt.show()
七.函数 axvspan() 或者 axhspan()
函数功能:绘制垂直于x轴/y轴 的参考区域
相关参数:
xmin:参考区域的起始位置;
xmax:参考区域的终止位置;
facecolor:参考区域的填充颜色;
alpha:参考区域的填充颜色的透明度;
平移性:axvspan()函数 类比 axhspan()函数;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot figure")
plt.legend()
plt.axvspan(xmin = 4.0,xmax = 6.0,facecolor = "y",alpha = 0.3)
plt.axhspan(ymin = 0.0,ymax = 0.5,facecolor = "y",alpha = 0.3)
plt.show()
八.函数annotate()
函数功能:添加图形内容细节的指向型注释文本
相关参数:
string:图形内容的注释文本;
xy:被注释图形内容的位置坐标;
xytext:注释文本的位置坐标;
weight:注释文本的字体粗细风格;
color:注释文本的字体颜色;
arrowprops:指示被注释内容的箭头的属性字典
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot figure")
plt.legend()
plt.annotate("maximum",#被注释的文本
xy = (np.pi/2,1.0),#被注释图形的位置
xytext = ((np.pi/2) + 1.0,0.8),#注释文本的显示位置
weight = "bold",#注释文本粗细风格
color = "b",#注释文本字体颜色
arrowprops = dict(arrowstyle = "->",connectionstyle = "arc3",color = "b")#注释箭头的属性字典
)
plt.show()
九.函数text()
函数功能:添加图形内容细节的无指向型注释文本
相关参数:
x:注释文本内容所在位置的横坐标;
y:注释文本内容所在位置的纵坐标;
string:注释文本内容;
weight:注释文本内容的粗细风格;
color:注释文本内容的字体颜色;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)#在start和stop之间返回均匀间隔的数据
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot gigure")
plt.legend()
plt.text(3.10,0.09,"y = sin(x)",weight = "bold",color = "b")
plt.show()
十.函数title()
函数功能:添加图形内容的标题
相关参数:
string:注释文本内容;
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)#在start和stop之间返回均匀间隔的数据
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot gigure")
plt.legend()
plt.title("y = sin(x)")
plt.show()
十一.函数legend()
函数功能:标示不同图形的文本标签图例
相关参数:
loc:图例在图中的地理位置
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)#在start和stop之间返回均匀间隔的数据
y = np.sin(x)
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot gigure")
plt.legend()
plt.legend(loc = "lower left")
plt.show()
十二.函数组合应用
绘制图表主要函数,从 函数功能、调用签名、参数说明 和 调用展示
相关参数:
x:注释文本内容所在位置的横坐标;
y:注释文本内容所在位置的纵坐标;
string:注释文本内容;
weight:注释文本内容的粗细风格;
color:注释文本内容的字体颜色;
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm as cm
#定义数据
x = np.linspace(0.5,3.5,100)#在start和stop之间返回均匀间隔的数据
y = np.sin(x)
y1 = np.random.randn(100)
#绘制离散散点图和连续线性图
plt.scatter(x,y1,c = "0.25",label = "scatter figure")
plt.plot(x,y,ls = "-.",lw = 2,c = "c",label = "plot gigure")
#清除图表的垃圾
#1.关闭底部和右侧的边线
for spine in plt.gca().spines.keys():
if spine == "top" or spine == "right":
plt.gca().spines[spine].set_color("none")
#2.启用x轴的底部刻度,设置底部刻度线位置
plt.gca().xaxis.set_ticks_position("bottom")
#3.启用y轴的向左刻度,设置左侧刻度线位置
plt.gca().yaxis.set_ticks_position("left")
#设置xy轴极限,定义域值域
plt.xlim(0.0,4.0)
plt.ylim(-3.0,3.0)
#设置轴标签
plt.ylabel("y_axis")
plt.xlabel("x_axis")
#设置x,y轴的栅格
plt.grid(True ,ls = ":",color = "r")
#在轴上添加一条水平线
plt.axhline(y = 0.0,c = "r",ls = "--",lw = 2)
#在轴上添加垂直跨度
plt.axvspan(xmin = 1.0,xmax = 2.0,facecolor = "y",alpha = 0.3)
#设置注释信息
plt.annotate("maximum",xy = (np.pi/2,1.0),xytext =((np.pi/2) + 0.15,1.5),weight = "bold",color = "r",
arrowprops = dict(arrowstyle = "->",connectionstyle = "arc3",color = "r"))
plt.annotate("spines",xy = (0.75,-3),xytext =(0.35,-2.25),weight = "bold",color = "b",
arrowprops = dict(arrowstyle = "->",connectionstyle = "arc3",color = "b"))
plt.annotate("",xy = (0,-2.78),xytext =(0.4,-2.32),
arrowprops = dict(arrowstyle = "->",connectionstyle = "arc3",color = "b"))
plt.annotate("",xy = (3.5,-2.98),xytext =(3.6,-2.70),
arrowprops = dict(arrowstyle = "->",connectionstyle = "arc3",color = "b"))
#设置文本信息
plt.text(3.6,-2.70,"'|' is tickine",weight = "bold",color = "b")
plt.text(3.6,-2.95,"3.5 is ticklabel",weight = "bold",color = "b")
#设置标题
plt.title("structure of matplotlib")
#设置图例
plt.legend()
plt.show()