import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.use('TkAgg') # 定义二维数组 xy = np.array([[40,0.0364],[50,0.0351],[60,0.0361],[70,0.0356],[80,0.0361],[90,0.0356],[100,0.0358],[110,0.0359],[120,0.0366],[130,0.0351]]) # 填入参数 #[:0]为每一个内部数组的序号为0的数,即这里的40到130 #[:1]作用类推序号为1的 plt.plot(xy[:,0], xy[:,1], '-o', markersize=8, linewidth=2) # 添加标签 plt.xlabel('x') plt.ylabel('y') plt.show(),我想在这代码的基础上改一下,我想画出三条曲线,并且三条曲线都在一个图中,该怎么改
时间: 2023-08-18 10:15:59 浏览: 125
如果你想在同一个图中画出三条曲线,你可以使用多次 `plot` 函数来实现。你可以定义三个不同的二维数组,然后使用 `plot` 函数分别画出这些数组对应的曲线。以下是一种实现方法:
```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('TkAgg')
# 定义三个二维数组
xy1 = np.array([[40,0.0364],[50,0.0351],[60,0.0361],[70,0.0356],[80,0.0361]])
xy2 = np.array([[70,0.0356],[80,0.0361],[90,0.0356],[100,0.0358],[110,0.0359]])
xy3 = np.array([[100,0.0358],[110,0.0359],[120,0.0366],[130,0.0351]])
# 画出三条曲线
plt.plot(xy1[:,0], xy1[:,1], '-o', markersize=8, linewidth=2, label='curve 1')
plt.plot(xy2[:,0], xy2[:,1], '-o', markersize=8, linewidth=2, label='curve 2')
plt.plot(xy3[:,0], xy3[:,1], '-o', markersize=8, linewidth=2, label='curve 3')
# 添加标签和图例
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
```
在这个例子中,我们定义了三个二维数组 `xy1`、`xy2`、`xy3`,分别表示三条曲线的数据。然后我们使用 `plot` 函数三次,分别画出这些曲线,并为每条曲线指定一个标签。最后使用 `legend` 函数添加图例。你可以根据需要自己修改曲线的数据、样式和标签。
阅读全文