matplotlib图表组成元素

it2022-05-09  31

一、函数

1、plot()    ——   展示变量的趋势与变化

用法:

plt.plot(x,y,ls="-",lw=2,label="plot figure")

参数说明:

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() #显示图像

 生成的图像:

 

2、散点图scatter()  ——  寻找变量间的关系

plt.scatter(x,y,c="b",label="scatter figure")

 参数说明:

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()

生成图像: 

3、xlim()  ——  设置想轴的数值显示范围

plt.xlim(xmin,xmax)

 参数说明:

xmin:x轴上的最小值 xmax:x轴上的最大值 注:ylim()与xlim()的用法相同

代码示例:

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轴的标签文本

plt.xlabel(string)

 参数说明:

string:标签文本内容 注:xlabel()与ylabel()用法相同

代码实例:

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.xlabel("x-axis") plt.ylabel("y-axis") plt.show()

输出图像:

 5、grid() —— 绘制刻度线的网格线

plt.grid(linestyle=":",color="r")

参数说明:

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.xlabel("x-axis") plt.ylabel("y-axis") plt.grid(linestyle=":",color="r") plt.show()

输出图像: 

6、函数axhline() —— 绘制平行于x轴的水平参考线

plt.axhline(y=0.0,c="r",ls="--",lw=2)

 参数说明:

y:水平参考线的出发点 c:参考线的线条颜色 ls:参考线的线条风格 lw:参考线的线条宽度

代码示例:

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、函数axvspqn()绘制垂直于x轴的参考区域

plt.axvspan(xmin=1.0,xmax=2.0,facecolor="y",alpha=0.3)

 参数说明:

xmin:参考区域的起始位置 xmax:参考区域的终止位置 facecolor:参考区域的填充颜色 alpha:参考区域的填充颜色的透明度 注:上面的功能同样可用在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()

输出图像:

8、函数annotate() —— 添加图形内容细节的指向型注释文本

plt.annotate(string,xy=(np.pi/2,1.0),xytext=((np.pi/2)+0.15,1.5),weight="bold",color="b", arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))

参数说明:

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()

输出图像:

9、函数text() —— 添加图形内容细节的无指向型注释文本

plt.text(x,y,string,weight="bold",color="b")

 参数说明:

x:注释文本内容所在的位置的横坐标 y:注释文本内容所在的位置的纵坐标 string:注释文本内容 weight:注释文本内容的粗细的风格 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.text(3.10,0.09,"y = sin(x)",weight="bold",color="b") plt.show()

图像输出:

10、函数title() —— 添加图形内容的标题

plt.title(string)

代码实例:

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() —— 标识不同图形的文本标签图例

plt.legend(loc="lower left")

 代码实现:

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") plt.show()

输出图像:

 

组合应用,代码示例: 

import matplotlib.pyplot as plt import numpy as np x = np.linspace(0.5,3.5,100) y = np.sin(x) y1 = np.random.rand(100) #定义生成图像所需要的数据 #生成散点图 plt.scatter(x,y1,c="0.25",label = "scatter figure") #生成函数图像 plt.plot(x,y,ls="--",lw=2,c="c",label="plot figure") #设置图像上边框和右边框的颜色为无 for spine in plt.gca().spines.keys(): if spine == "top" or spine == "right": plt.gca().spines[spine].set_color("none") #调整刻度位置 plt.gca().xaxis.set_ticks_position("bottom") plt.gca().yaxis.set_ticks_position("left") #设置坐标范围 plt.xlim(0.0,4.0) plt.ylim(-3.0,3.0) #设置坐标轴名称 plt.xlabel("x-axis") plt.ylabel("y-axis") #显示网格图 plt.grid(True,ls=":",color="r") #绘制平行于x轴的水平参考线 plt.axhline(y=0.0,c="r",ls="--",lw=2) #绘制垂直于x轴的参考区域 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.0,-2.80),weight="bold",color="r", arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b")) #设置文本注释框 plt.text(2.5,-2.50,"'|' is tickline",weight="bold",color="b") plt.text(2.5,-2.70,"3.5 is tickline1",weight="bold",color="b") #设置图像标题 plt.title("Structure of Matplotlib") #显示图例 plt.legend(loc="upper right") #显示图像 plt.show()

图像输出:

转载于:https://www.cnblogs.com/circleyuan/p/10350168.html


最新回复(0)