在一个图形输出窗口中,底层是一个Figure实例,通常称之为画布,包含一些可见和不可见的元素。在画布上的就是图形,图形是一些Axes实例,里面几乎包含了matplotlib的组成元素,例如坐标轴、刻度、标签、线和标记等。
我们可以导入第三方包NumPy和快速绘图模块pyplot,matplotlib库就是建立在科学计算包NumPy基础之上的Python绘图库。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 3.5, 100) # 在0.5到3.5之间均匀地取100个数
y = np.sin(x)
y1 = np.random.randn(100) # 在标准正态分布中随机地取100个数
函数功能: 展现变量的趋势变化
调用签名: plt.plot(x, y, ls=‘-’, lw=2, label=‘plot figure’)
参数说明:
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='cos(x)')
plt.legend() # 显示图例
plt.show()
函数功能: 寻找变量之间的关系
调用签名: plt.scatter(x, y, c=‘b’, label=‘scatter figure’)
参数说明:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.random.randn(1000)
plt.scatter(x, y, c='b', label='scatter plot')
plt.legend()
plt.show()
函数功能: 设置x轴的数值显示范围
调用签名: plt.xlim(xmin, xmax)
参数说明:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.random.randn(1000)
plt.scatter(x, y, c='b', label='scatter plot')
plt.legend()
plt.xlim(0.05, 10)
plt.ylim(0, 1)
plt.show()
函数功能: 设置x轴的标签文本
调用签名: plt.xlabel(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='sin(x)')
plt.legend() # 显示图例
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.show()
函数功能: 绘制刻度线的网格线
调用签名: plt.grid(linestyle=‘:’, color=‘r’)
参数说明:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 100)
y = np.log(x)
plt.plot(x, y, ls='-.', lw=2, c='c', label='y=log(x)')
plt.legend()
plt.grid(linestyle=':', color='r')
plt.show()
函数功能: 绘制平行与x轴的水平参考线
调用签名: plt.axhline(y=0.0, c=‘r’, ls=‘–’, lw=2)
参数说明:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 100)
y = np.sin(x)
plt.plot(x, y, ls='-.', lw=2, c='c', label='sin(x)')
plt.legend()
plt.axhline(y=0.0, c='r', ls='--', lw=2)
plt.axvline(x=4.0, c='r', ls='--', lw=2)
plt.show()
函数功能: 绘制垂直于x轴的参考区域
调用签名: plt.axvspan(xmin=1.0, xmax=2.0, facecolor=‘y’, alpha=0.3)
参数说明:
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='sin(x)')
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()
函数功能: 添加图形内容细节的指向型注释文本
调用签名: 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’))
参数说明:
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='sin(x)')
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()
函数功能: 添加图形内容细节的无指向型注释文本
调用签名: plt.text(x, y, string, weigth=‘bold’, color=‘b’)
参数说明:
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='sin(x)')
plt.legend() # 显示图例
plt.text(3.10, 0.09, "y=sin(x)", weight="bold", color="b")
plt.show()
函数功能: 添加图形内容的标题
调用签名: 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='sin(x)')
plt.legend() # 显示图例
plt.title('sin(x)') # 标题
plt.show()
函数功能: 标识不同图形的文本标签图例
调用签名: 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='sin(x)')
plt.legend(loc='lower left') # 显示图例
plt.title('sin(x)') # 标题
plt.show()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm as cm # color map
# 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, c='r', label='plot figure')
# some clean up (removing chartjunk)
for spine in plt.gca().spines.keys():
if spine == "top" or spine == "right":
plt.gca().spines[spine].set_color('none')
# turn bottom ticks 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.xlabel('x_axis')
plt.ylabel('y_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=0.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='b'))
plt.annotate('', xy=(3.5, -2.98),
xytext=(3.6, -2.70),
arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='b'))
# 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()