1. 使用函数绘制 matplotlib 的图表组成元素
(1) 函数 plot--- 变量的变化趋势
- import matplotlib.pyplot as plt
- import numpy as np
- x = np.linespace(0.05, 10, 1000) #在 x 轴均匀取 1000 个点
- y = np.cos(x) #对应的 y 值
- plt.plot(x,y,ls="-", lw=2, label="plot figure")
- '''
- ls--------> 线条的风格
- lw---------> 线条的宽度
- label--------> 标记图形内容的标签文本
- '''
(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, label="scatter figure") plt.legend() plt.show()
(三) 函数 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(2, 10) #x 轴的显示范围 plt.ylim(0,1) plt.show()
(四) 函数 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=2, c="c", label="plot figure") #c 为颜色设置 plt.legend() plt.xlabel("x-axis") #x 轴的标签 plt.ylabel("y-axis") plt.show()
(五) 函数 grid--------- 绘制刻度线的网格线
import matplotlib.pyplot as plt import numpy as np x = p.linspace(0.05,10,1000) y = np.sin(x) plt.plot(x, y, ls="-", lw=2, c="c", label="plot figure") #c 为颜色设置 plt.legend() plt.grid(linestyle="-", color="r")#linestyle------> 线型 = ls color-------> 颜色 = c plt.show()
(六) 函数 axhline()------ 绘制平行于 x 轴的水平参考线
import matplotlib.pyplot as plt import numpy as np x = p.linspace(0.05,10,1000) y = np.sin(x) plt.plot(x, y, ls="-", lw=2, c="c", label="plot figure") #c 为颜色设置 plt.legend() plt.axhline(y = 0.0, c="c", ls="--", lw=2) #axh 轴代表水平 plt.axvline(x = 4.0, c="c", ls="--", lw=2) #axv 代表竖直 plt.show()
来源: http://www.bubuko.com/infodetail-3098024.html