以前一直使用 matplotlib, 但都是随用随查,如今特开此系列帖子已记录其学习过程。html
Matplotlib
多是Python 扩展包中仅有的最流行的 2D 绘图库。她不只提供了快速的方式可视化Python
中的数据,并且提供流行的图形格式的选择。api
pyplot
是很是接近 Matlab
的一个函数库,承担了大部分的绘图任务。咱们能够经过如下命令引入pyplot
.函数
from matplotlib import pyplot as plt
学习
文档连接:spa
plot tutorialcode
matplotlib 中提供了一系列的参数,好比 图形大小(figure size),图形质量(dpi), 线宽(linewidth), 颜色和样式(color and style), axes, axis and grid properties, text and font properties 等等。ci
先举个简单的例子(使用默认设置):文档
import numpy as np import matplotlib.pyplot as plt X = np.linspace(-np.pi, np.pi, 256, endpoint=True) C, S = np.cos(X), np.sin(X) plt.plot(X, C) plt.plot(X, S) plt.show()
实例化设置初始设置:get
文档连接:
增长相关的设置后代码以下:
import numpy as np import matplotlib.pyplot as plt # Create a figure of size 8x6 inches, 80 dots per inch plt.figure(figsize=(8, 6), dpi=80) # Create a new subplot from a grid of 1x1 plt.subplot(1, 1, 1) X = np.linspace(-np.pi, np.pi, 256, endpoint=True) C, S = np.cos(X), np.sin(X) # Plot cosine with a blue continuous line of width 1 (pixels) plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-") # Plot sine with a green continuous line of width 1 (pixels) plt.plot(X, S, color="green", linewidth=1.0, linestyle="-") # Set x limits plt.xlim(-4.0, 4.0) # Set x ticks plt.xticks(np.linspace(-4, 4, 9, endpoint=True)) # Set y limits plt.ylim(-1.0, 1.0) # Set y ticks plt.yticks(np.linspace(-1, 1, 5, endpoint=True)) # Save figure using 72 dots per inch # plt.savefig("exercice_2.png", dpi=72) # Show result on screen plt.show()
具体设置下一节再说吧。