满意答案
z690un9n3q
2016.08.27
采纳率:41% 等级:13
已帮助:7109人
# 导入matplotlib.pyplot, numpy 包
import numpy as np
import matplotlib.pyplot as plt
# 添加主题样式
plt.style.use('mystyle')
# 设置图的大小,添加子图
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111)
#绘制sin, cos
x = np.arange(-np.pi, np.pi, np.pi / 100)
y1 = np.sin(x)
y2 = np.cos(x)
sin, = ax.plot(x, y1, color='red', label='sin')
cos, = ax.plot(x, y2, color='blue', label='cos')
ax.set_ylim([-1.2, 1.2])
# 第二种方式 拆分显示
sin_legend = ax.legend(handles=[sin], loc='upper right')
ax.add_artist(sin_legend)
ax.legend(handles=[cos], loc='lower right')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# 添加主题样式
plt.style.use('mystyle')
# 设置图的大小,添加子图
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111)
for color in ['red', 'green']:
n = 750
x, y = np.random.rand(2, n)
scale = 200.0 * np.random.rand(n)
ax.scatter(x, y, c=color, s=scale,
label=color, alpha=0.3,
edgecolors='none')
ax.legend()
ax.grid(True)
plt.show()
10分享举报