【Matplotlib】概要总览第一讲

之前一直使用 matplotlib, 但都是随用随查,现在特开此系列帖子已记录其学习过程。

Matplotlib可能是Python 扩展包中仅有的最流行的 2D 绘图库。她不仅提供了快速的方式可视化Python中的数据,而且提供流行的图形格式的选择。

pyplot 是非常接近 Matlab 的一个函数库,承担了大部分的绘图任务。我们可以通过以下命令引入pyplot.

from matplotlib import pyplot as plt

文档链接:

matplotlib 中提供了一系列的参数,比如 图形大小(figure size),图形质量(dpi), 线宽(linewidth), 颜色和样式(color and style), axes, axis and grid properties, text and font properties 等等。

先举个简单的例子(使用默认设置):

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

实例化设置初始设置:

文档链接:

增加相关的设置后代码如下:

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

具体设置下一节再说吧。

原文地址:https://www.cnblogs.com/nju2014/p/5615113.html