
在matplotlib中为子图添加图例的方法
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本文介绍了如何在使用Python中的matplotlib库时,向图表中的各个子图分别添加图例的具体方法和技巧。
以下是代码的修正版本:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [5, 2, 4, 2, 1, 4, 5, 2]
axe1 = plt.subplot(211)
s1 = axe1.scatter(x,y,color=r, s=25, marker=o)
plt.legend([s1], [A])
# 或者
# s1 = axe1.scatter(x,y,color=r, s=25, marker=o, label=A)
# plt.legend()
axe2 = plt.subplot(212)
```
注意:在使用`scatter()`函数时,颜色参数应该用字符串形式表示(例如g代表绿色),同时,marker形状需要用引号括起来。代码中存在一些语法错误如`color=r, marker=o`应改为`color=r, marker=o`。此外,由于您提供的代码不完整,在第二个子图上没有绘制裁点或其他图形对象的代码片段被省略了。
全部评论 (0)
还没有任何评论哟~


