怎么用loglog函数把多个曲线画在一张图上
时间: 2024-03-07 08:47:15 浏览: 15
要用loglog函数将多个曲线画在一张图上,可以按照以下步骤进行:
1. 导入matplotlib库,并创建一个画布和一个子图。
```python
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
```
2. 对于每个曲线,定义x和y值,并使用loglog函数绘制曲线。在loglog函数中,可以设置label参数来为每个曲线定义一个标签。
```python
x1 = [1, 10, 100, 1000]
y1 = [1, 100, 10000, 1000000]
ax.loglog(x1, y1, label='Line 1')
x2 = [1, 10, 100, 1000]
y2 = [10, 1000, 100000, 10000000]
ax.loglog(x2, y2, label='Line 2')
```
3. 在绘制完所有曲线后,使用legend函数添加图例。
```python
ax.legend()
```
4. 最后,使用show函数显示图形。
```python
plt.show()
```
完整代码:
```python
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x1 = [1, 10, 100, 1000]
y1 = [1, 100, 10000, 1000000]
ax.loglog(x1, y1, label='Line 1')
x2 = [1, 10, 100, 1000]
y2 = [10, 1000, 100000, 10000000]
ax.loglog(x2, y2, label='Line 2')
ax.legend()
plt.show()
```