添加图例和注解

来源:互联网 发布:兔子软件安装器 编辑:程序博客网 时间:2024/05/23 16:36
from matplotlib import pyplot as pltimport numpy as npx1 = np.random.normal(30, 3, 100)x2 = np.random.normal(20, 2, 100)x3 = np.random.normal(10, 3, 100)# 如果不想在图例中显示标签,可将标签设置为_nolegend_。plt.plot(x1, label='1st plot')plt.plot(x2, label='2nd plot')plt.plot(x3, label='3rd plot')# 添加图例'''loc参数:确定图例框的位置,这个参数是可选的。ncol参数:标签列数。bbox_to_anchor参数:边界框的位置。前两个值为起始位置,第三个值为宽度,第四个值为高度,所有值均                   基于归一化轴坐标系。mode参数:可设置为None或expand,expand图例框水平扩展至整个坐标轴区域。borderaxespad参数:指定坐标轴和图例边界之间的间距。'''plt.legend(bbox_to_anchor=(0.0, 1.02, 1.0, 0.102), loc=3, ncol=3,           mode='expand', borderaxespad=0.0)# 添加注解'''第一个参数:注解的字符串第二个参数:添加注解的数据点xy坐标位置xycoords参数:设置为'data',指定注解和数据使用相同的坐标系xytext参数:注解文本的起始位置arrowprops字典:定义了很多箭头属性,arrowstyle指定箭头的风格'''plt.annotate('Important value', (55, 20), xycoords='data', xytext=(5, 38),             arrowprops=dict(arrowstyle='->'))plt.show()
原创粉丝点击