[TOC]
常用的需要导入的是 pyplot 模块,并命名为 plt
import matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False绘制图片
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10, 10))
data = np.random.randint(10, 100, size=10)
plt.plot(data)
plt.show()将得到
可以在一张图中绘制多条曲线
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10, 10))
t = np.arange(1, 10, 1)
plt.plot(t, t, t, t * 2, t, t ** 2)
plt.show()将得到
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10, 10))
t = np.arange(1, 10, 1)
plt.plot(t, t, color='red', linestyle='-', linewidth=1, label='Label 1')
plt.plot(t, t * 2, color='green', linestyle='-.', linewidth=2, label='Label 2')
plt.plot(t, t ** 2, color='blue', linestyle='--', linewidth=3, label='Label 3')
plt.show()将得到
| 参数名 | 含义 | |
|---|---|---|
color |
颜色 | color=red |
linestyle |
线形 | linestyle='-.' |
linewidth |
线宽 | linewidth=3 |
label |
标签 | label='Label 1' |
绘制散点图
例如
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10,10))
data = np.random.randint(10, 100, size=10)
plt.scatter(x=range(len(data)), y=data, marker='*')
plt.show()将得到
| 参数名 | 含义 |
|---|---|
x |
x 轴坐标数据 |
y |
y 轴坐标数据 |
marker |
标志点样式 |
绘制条形图
例如
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10,10))
data = np.random.randint(10, 100, size=10)
plt.bar(x=range(len(data)), height=data)
plt.show()将得到
参数
| 参数名 | 含义 | 例 |
|---|---|---|
figsize |
尺寸 | figsize=(100, 100) |
dpi |
分辨列 | dpi=100 |
图例
plt.legend(loc='best')例如
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10, 10))
t = np.arange(1, 10, 1)
plt.plot(t, t, color='red', linestyle='-', linewidth=1, label='Label 1')
plt.plot(t, t * 2, color='green', linestyle='-.', linewidth=2, label='Label 2')
plt.plot(t, t ** 2, color='blue', linestyle='--', linewidth=3, label='Label 3')
plt.legend(loc='best')
plt.show()将得到
保存图片
plt.savefig('路径名')图片标题
x 轴标签
y 轴标签
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
x = np.linspace(-np.pi, np.pi, 300)
plt.figure(1)
plt.subplot(211)
plt.plot(x, np.sin(x), color='red')
plt.subplot(212)
plt.plot(x, np.cos(x), color='blue')
plt.show()将得到
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 300)
fig, (ax0, ax1) = plt.subplots(2, 1, figsize=(10, 10))
ax0.plot(x, np.sin(x), color='red')
ax0.set_title('sin(x)')
ax1.plot(x, np.cos(x), color='blue')
ax1.set_title('cos(x)')
plt.show()将得到
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10, 10))
x = np.linspace(-np.pi, np.pi, 300)
plt.axes((0.1, 0.1, 0.8, 0.8))
plt.plot(x, np.sin(x), color='red')
plt.axes((0.4, 0.15, 0.4, 0.3))
plt.plot(x, np.cos(x), color='blue')
plt.show()可得到
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = True
plt.figure()
start_val = 0
end_val = 10
num_val = 100
x = np.linspace(start=start_val, stop=end_val, num=num_val)
y = np.sin(x)
plt.plot(x, y, '--g', lw=2, label='sin(x)')
x_min = 0
x_max = 10
y_min = -1.5
y_max = 1.5
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xlabel('X Axis', fontsize=15)
plt.ylabel('Y Axis', fontsize=15)
x_location = np.arange(x_min, x_max, 2)
x_labels = ('2019-1-1', '2019-1-2', '2019-1-3', '2019-1-4', '2019-1-5')
plt.xticks(x_location, x_labels, rotation=45, fontsize=15)
y_location = np.arange(y_min, y_max, 1)
y_labels = ('最小值', '零值', '最大值')
plt.yticks(y_location, y_labels, fontsize=15)
plt.grid(True, ls=':', color='r', alpha=0.5)
plt.legend(loc='best')
plt.title('Figure of sin(x)', fontsize=15)
plt.show()import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = True
fig = plt.figure()
ax = fig.add_subplot(111)
start_val = 0
end_val = 10
num_val = 100
x = np.linspace(start=start_val, stop=end_val, num=num_val)
y = np.sin(x)
ax.plot(x, y, '--g', lw=2, label='sin(x)')
x_min = 0
x_max = 10
y_min = -1.5
y_max = 1.5
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.set_xlabel('X Axis', fontsize=15)
ax.set_ylabel('Y Axis', fontsize=15)
x_location = np.arange(x_min, x_max, 2)
x_labels = ('2019-1-1', '2019-1-2', '2019-1-3', '2019-1-4', '2019-1-5')
ax.set_xticks(x_location)
ax.set_xticklabels(x_labels, rotation=45, fontsize=15)
y_location = np.arange(y_min, y_max, 1)
y_labels = ('最小值', '零值', '最大值')
ax.set_yticks(y_location)
ax.set_yticklabels(y_labels, fontsize=15)
ax.grid(True, ls=':', color='r', alpha=0.5)
ax.legend(loc='best')
ax.set_title('Figure of sin(x)', fontsize=15)
plt.show()