matplotlib给子图添加图例的方法

yipeiwu_com6年前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)
s2 = axe2.scatter(x,y, color='g', s=25, marker="o")
plt.legend([s2],['B'])
#或者
#s2 = axe1.scatter(x,y, color='r', s=25, marker="o", label='B')
#plt.legend()
plt.show()

效果图如下:

以上这篇matplotlib给子图添加图例的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python解析xml简单示例

本文实例讲述了python解析xml的方法。分享给大家供大家参考,具体如下: xml是除了json之外另外一个比较常用的用来做为数据交换的载体格式。对于一些比较固定的数据,直接保存在xm...

pandas全表查询定位某个值所在行列的方法

如下所示: # create a dataframe with an integer feature and a categorical string feature demo_df...

在Pandas中给多层索引降级的方法

# 背景介绍 通常我们不会在Pandas中主动设置多层索引,但是如果一个字段做多个不同的聚合运算, 比如sum, max这样形成的Column Level是有层次的,这样阅读非常方便,但...

Python入门篇之正则表达式

 正则表达式有两种基本的操作,分别是匹配和替换。 匹配就是在一个文本字符串中搜索匹配一特殊表达式; 替换就是在一个字符串中查找并替换匹配一特殊表达式的字符串。   1...

python中的reduce内建函数使用方法指南

官方解释: Apply function of two arguments cumulatively to the items of iterable, from left to r...