matplotlib库是python中绘制二维、三维图表的数据可视化工具。它的主要特点如下:
1.使用简单绘图语句实现复杂绘图效果
2.以交互式操作实现渐趋精细的图形效果
3.使用嵌入式的LaTex输出具有印刷级别的图表、科学表达式和符号文本
4.对图表的组成元素实现精细化控制
我们导入第三方包NumPy和快速绘图模块pyplot,其中科学计算包NumPy是matplotlib库的基础。
1.函数plot()---展现变量趋势变化
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)#x轴上的数值
y = np.cos(x)#y轴上的数值
plt.plot(x,y,ls="-",lw=3,label="plot figure")
#ls折线图的线条风格,lw线条宽度、label标记图形内容的标签文本
plt.legend()
plt.show()
2.函数scatter()---寻找变量之间的关系
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,c="b",label="scatter figure")
plt.legend()
plt.show()
3.函数xlim()---设置X轴的数值显示范围
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()
4.函数xlabel()---设置X轴的标签文本
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=1,c="c",label="plot figure")
plt.legend()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
5.函数grid()---绘制刻度线的网络线
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=1,c="c",label="plot figure")
plt.legend()
plt.grid(linestyle=":",color="r")
plt.show()
6.函数axhline()---绘制平行于x轴的水平参考线
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()
7.函数axvspan()---绘制垂直于x轴的参考区域
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()
8.函数annotate()---添加图形内容细节的指向型注释文本
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,.8),#注释文本的位置坐标
weight="bold",
color="b",
arrowprops
=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
#arrowprops指示被注释内容的箭头的属性字典
plt.show()
9.函数text()---添加图形内容细节的无指向型注释文本
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.text(3.1,0.09,"y=sin(x)",weight="bold",color="b")
plt.show()
10.函数title()---添加图形内容的标题
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.title("y=sin(x)")
plt.show()
11.函数legend()---标示不同图形的文本标签图例
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(loc="lower left")#loc图例在图中的地理位置
plt.show()
函数组合应用
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm as cm
#define data
x =np.linspace(0.5,3.5,100)
y =np.sin(x)
y1 = np.random.randn(100)
#scatter figure
plt.scatter(x,y1,c="0.25",label="scatter figure")
#plot figure
plt.plot(x,y,ls="--",lw=2,label="plot figure")
#some clear up(removing chartjunk)
#turn the top spine and the right spine off
for spine in plt.gca().spines.keys():
if spine =="top" or spine =="right":
plt.gca().spines[spine].set_color("none")
#turn bottom tick for x-axis on
plt.gca().xaxis.set_ticks_position("bottom")
#set tick_line position of bottom
#turn left ticks for y-axis on
plt.gca().yaxis.set_ticks_position("left")
#set tick_line position of left
#set x,yaxis limit
plt.xlim(0.0,4.0)
plt.ylim(-3.0,3.0)
#set axes labels
plt.ylabel("y_axis")
plt.xlabel("x_axis")
#set x,yaxis grid
plt.grid(True,ls=":",color="r")
#add a horizontal line across the axis
plt.axhline(y=0.0,c="r",ls="--",lw=2)
#add a vertical span across the axis
plt.axvspan(xmin=1.0,xmax=2.0,facecolor="y",alpha=.3)
#set annotate info
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="r"))
plt.annotate("",xy=(3.5,-2.98),
xytext=(3.6,-2.7),
arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="r"))
#set text info
plt.text(3.6,-2.7,"'|' is tickline",weight="bold",color="b")
plt.text(3.6,-2.95,"3.5 is ticklabel",weight="bold",color="b")
#set title
plt.title("structure of matplotlib")
#set legend
plt.legend()
plt.show()