python plt.plot怎么把图显示出来?

如题所述

1, 导入matplotlib.pyplot, numpy 包:
import numpy as np
import matplotlib.pyplot as plt

2,添加主题样式:
plt.style.use('mystyle')

3, 设置图的大小,添加子图:
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111)

4,绘制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()

为项目设置matplotlib参数,

在代码执行过程中,有两种方式更改参数:

1,使用参数字典(rcParams);

2,调用matplotlib.rc()命令 通过传入关键字元祖,修改参数。

    如果不想每次使用matplotlib时都在代码部分进行配置,可以修改matplotlib的文件参数。可以用matplot.get_config()命令来找到当前用户的配置文件目录。

温馨提示:答案为网友推荐,仅供参考
相似回答