栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

1.绘制matplotlib图表组成元素的函数用法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1.绘制matplotlib图表组成元素的函数用法

文章目录
  • 1.函数plot()——展现变量的趋势变化
  • 2.函数scatter()——寻找变量之间的关系
  • 3.函数xlim()——设置x轴的数值显示范围
  • 4.函数xlabel()——设置x轴的标签文本
  • 5.函数grid()——绘制刻度线的网格线
  • 6.函数axhline()——绘制平行于x轴的水平参考线
  • 7.函数axvspan()——绘制垂直于x轴的参考区域
  • 8.函数annotate()——添加图形内容细节的指向型注释文本
  • 9.函数text()——添加图形内容细节的无指向型注释文本
  • 10.函数title()——添加图形内容的标题
  • 11.函数legend()
  • 12.函数组合应用

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)
# linspace(0.05,10,1000)表示在0.5至3.5之间均匀取100个数
y = np.cos(x)

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

plt.legend()

plt.show()

2.函数scatter()——寻找变量之间的关系
函数功能:寻找变量之间的关系
调用签名:plt.scatter(x,y1,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()——设置x轴的数值显示范围
函数功能:设置x轴的数值显示范围
调用签名:plt.xlim(xmin,xmax)
参数说明
xmin:x轴上的最小值
xmax:x轴上的最大值
以上的函数功能,调用签名和参数说明同样可以用到函数yslim()上
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轴的标签文本
函数功能:设置x轴的标签文本
调用签名:plt.xlabel(string)
参数说明:
string:标签文本内容
上面的函数功能对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-axix")

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.grid(linestyle=":",color="r")

plt.show()

6.函数axhline()——绘制平行于x轴的水平参考线
函数功能:绘制平行于x轴的水平参考线
调用签名:plt.axhline(y=0.0,c="r",ls="--",lw=2)
参数说明:
y:水平参考线的出发点
c:参考线的线条颜色
ls:参考线的线条风格
lw:参考线的线条宽度
平移性:上面的函数功能同样可以用到函数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()

7.函数axvspan()——绘制垂直于x轴的参考区域
函数功能:绘制垂直于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,.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)
参数说明:
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.text(3.10,0.09,"y=sin(x)",weight="bold",color="b")

plt.title("y=sin(x)")

plt.show()

11.函数legend()
函数功能:标示不同图形的文本标签图例
调用签名:plt.legend(loc="lower left")
参数说明:
loc:图例在图中的地理位置
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.title("y=sin(x)")

plt.show()

12.函数组合应用
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,ls="--",lw=2,label="plot figure")
###############################
# some clean 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
########################################################
# setx,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 annotating 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.70),
             arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="r"))

# set text info
plt.text(3.6,-2.70,"'|' 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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883639.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号