Python 您所在的位置:网站首页 matlab的箱型图怎么添加图例文字 Python

Python

2024-03-24 00:26| 来源: 网络整理| 查看: 265

如果想给自己和家人有尊严的生活,就请拼尽全力让自己变强。

目录 前言  1. 概述  2. 版本   2.1 山东青岛,2021年5月4日,Version 1   3. 参考资料 一、plt.legend(), axes.legend()和figure.legend()二、图例位置调整三、图例字体设置和边框设置总结

前言   1. 概述

  (1) 给matplotlib绘制的图添加图例。   (2) 掌握常见的图例细节调整方法。

  2. 版本    2.1 山东青岛,2021年5月4日,Version 1   3. 参考资料

[1]. matplotlib官方文档 [2]. Python——legend()图例位置调整

一、plt.legend(), axes.legend()和figure.legend() plt.legend()的是给当前的图形对象添加图例axes.legend()是给已经明确的axes子图对象添加图例, 在能明确操作的图形对象时建议直接使用Axes.legend()。figure.legend()是对当前的figure对象添加图例,需要注意的是一个figure对象可能包括多个Axes的子对象,因此figure.legend()是给多个Axes添加共同的图例时使用的。代码示例: ''' 1. 程序目的 (1) matplotlib制图时添加图例 2. 山东青岛 2021年5月4日 ''' # 1. 包的导入 import matplotlib.pyplot as plt # 2. 绘图 figure = plt.figure(figsize=(9,3)) plt.subplot(131) line_up, = plt.plot([1,2,3],label='Line 2') # 逗号不能丢掉,为什么? line_down, = plt.plot([3,2,1],label='Line 1') # 添加图例 plt.legend(handles=[line_up,line_down]) # 此时用的是制图时label的标签 axes = plt.subplot(132) line_up, = plt.plot([1,2,3],label='Line 2') # 逗号不能丢掉,为什么? line_down, = plt.plot([3,2,1],label='Line 1') # 添加图例,重新命名图例名称 axes.legend([line_up,line_down],['Line Up','Line Down'],loc='lower center') axes_2 = plt.subplot(133) line_up, = plt.plot([1,2,3],label='Line 2') # 逗号不能丢掉,为什么? line_down, = plt.plot([3,2,1],label='Line 1') # 给三个Axes子图形对象添加共同的图例,重新命名图例名称 figure.legend([line_up,line_down],['Line Up Fig','Line Down Fig'],loc='upper center') plt.show() 制图结果 legend 二、图例位置调整 ~.legend(loc=‘String’ or Number, bbox_to_anchor=(num1,num2)) 可以满足图例位置的基本调整需求,~ 指figure, Axes或者plt等可以添加图例的对象。

   (1)loc的参数设置可以参考以下表格:

StringNumberupper right1upper left2lower left3lower right4right5center left6center right7ower center8upper center9center10

   (2)bbox_to_anchor用于图例位置的微调,在基于loc调整完图例的基本位置后,bbox_to_anchor参数中的num1控制图例的左右移动,值越大越向右边移动,num2用于控制图例的上下移动,值越大,越向上移动。

图例位置调整代码示例 ''' 1. 程序目的 (1) 图例位置调整 2. 山东青岛 2021年5月5日 ''' # 1. 包的导入 import numpy as np import matplotlib.pyplot as plt # 2. 制图数据创建 z = np.random.randn(10) #print(z) # 3. 绘图 figure = plt.figure(figsize=(10,5)) axes_1 = figure.add_subplot(121) red_dot, = plt.plot(z,'ro',markersize=15) # 3.1 给其中部分数据添加十字交叉 white_cross, = plt.plot(z[:5],'w+',markeredgewidth=3,markersize=15) # 3.2 给其中一个Axes对象添加图例,并调整图例位置 axes_1.legend([red_dot,(red_dot,white_cross)], ['Red A','Red A+B'], ncol = 1, loc = 'upper right', bbox_to_anchor=(1,0.9) ) axes_2 = figure.add_subplot(122) blue_dot, = plt.plot(z,'bo',markersize=15) white_cross, = plt.plot(z[:5],'w+',markeredgewidth=3,markersize=15) # 3.3 给整个figure对象添加图例,并调整图例位置 figure.legend([red_dot,(red_dot,white_cross),blue_dot,(blue_dot,white_cross)], ['red A','red A+B','blue A','blue A+B'], ncol = 4, loc = 'upper center', #bbox_to_anchor=(0.5,0.9) ) plt.show() 程序运行结果: 图例位置 三、图例字体设置和边框设置 ~.legend()中的prop参数可以用于对图例的字体属性进行设置,其接受字典类型的变量。对于图例边框常见的操作如下: frameon = False去除图例边框facecolor = ‘orange’ 设置边框填充色为橙色edgecolor = ‘blue’ 设置图例边框的颜色为蓝色 代码示例: ''' 1. 程序目的 (1) 增加图名和坐标轴名称 2. 山东青岛 2021年5月2日 ''' # 0. 包的导入 import numpy as np import matplotlib.pyplot as plt # 1. 创建制图数据 x = np.linspace(-1,1,100) # 2. 绘图 # 2.1 图形对象创建 fig,axes = plt.subplots(figsize=(5,5)) # 创建一个图形对象和一个子图对象 axes.plot(x,x**3,label='cubic',linestyle='--') # axes对象绘图 axes.plot(x,x**2,label='square',linestyle=':') # axes对象绘图 # 2.2 axes对象添加图名称,坐标轴名称 axes.set_xlabel('x label',fontsize=14) axes.set_ylabel('y label',fontsize=14) axes.set_title('$X^{a}$',fontsize=16) # 2.3 添加图例,修饰图例 # 用于设置图例字体 font_legend = {'family': 'Times New Roman', 'weight': 'normal', 'size': 14, } axes.legend( loc = 'lower right', # 图例位置 #frameon = False, # 去除图例边框 facecolor = 'orange', edgecolor = 'blue', # 设置边框颜色,边框设置为白色则和无边框效果相似 prop = font_legend # 通过prop设置字体属性 ) # 添加图例 plt.show() 运行结果:

图例设置

总结 plt.legend() 和 axes.legend() 可以用于给当前的axes子图对象添加添加图例, figure.legend() 可以用于给当前的figure图形对象添加图例。~.legend(loc=‘String’ or Number, bbox_to_anchor=(num1,num2)) 可以满足图例位置的基本调整需求,~ 指figure, Axes或者plt等可以添加图例的对象。~.legend()中的prop参数可以用于对图例的字体属性进行设置,其接受字典类型的变量。对于图例边框常见的操作如下: frameon = False去除图例边框facecolor = ‘orange’ 设置边框填充色为橙色edgecolor = ‘blue’ 设置图例边框的颜色为蓝色


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有