python plt legend并排_matplotlib如何控制legend的位置之二
在使用matplotlib做图时,总免不了和图例(legend)打交道,那图例到底该放在哪?该如何放到指定的位置?(本文只讨论legend的坐标系为axes的情况)
上篇文章介绍了如何通过loc参数设置legend的位置:
1.loc = str类型,将legend放到9个固定的位置,左上角,右上角等等
2.loc =(float, float) ,通过设置lengend左小角相对于坐标轴的坐标,进行更精确的定位legend的位置
除了loc参数外,还有一个参数可以和loc一起使用更好的控制legend的位置,那就是bbox_to_anchor。
bbox_to_anchor参数可选值为:
1. 默认值为BboxBase: 本人理解为axes.bbox,即以x,y轴为长宽组成的矩形区域,或者figure.bbox,即画布所在的区域
2. 2元元组(x, y):一个点
3. 4元元组(x, y, width, height):以(x, y)为定位点,width和height分别为长和宽所组成的矩形区域
loc参数与bbox_to_anchor参数是如何互动的?
其实很简单,就是bbox_to_anchor画了一块地(矩形或者点),再通过loc参数的设置,使legend围绕着这块地儿闪转腾挪,其实当你没设置这个参数的时候,这个参数的默认值帮你画好了地,不是axes.bbox就是figure.bbox。现在你可以通过(x, y, width, height)自己画地儿了。
那现在由我们自己来画地,看看loc参数与bbox_to_anchor的互动情况:
1. 当bbox_to_anchor是一个点时:(x, y)
2.当bbox_to_anchor是一个矩形时:(x, y, width, height)
3.当bbox_to_anchor是一条线时:(x, y, width, height=0)
bbox_to_anchor是一个点(x, y)
当bbox_to_anchor为一个点时,分为两种情况:
1. 当loc为str时,就是让loc所指定的legend的位置与(x, y)重合
例如: loc='upper left' , bbox_to_anchor时,意味着legend的左上角与(x, y) 重合
from matplotlib.patches import Rectangle
import matplotlib.patches mpatches
fig, ax = plt.subplots(figsize=(10, 5))
ax.set_ylim(0, 5)
ax.set_xlim(0, 10)
ax.grid()
ax.scatter(6, 3, color='red', marker="*")
ax.tick_params(size=8)
patch = mpatches.Patch(color='blue', label='legend')
ax.legend(handles=[patch], loc='upper left', bbox_to_anchor=(6/10, 3/5), borderaxespad=0, fontsize=18, edgecolor='yellow')
# ax.annotate('(14, 6)', xy=(14, 6), xytext=(15, 6.5),arrowprops={'arrowstyle':'->'}, fontsize=18)
ax.text(5.5, 3.3, 'legend的左上角与点(x, y)重合', fontsize=18)
2.当loc为元组时, loc不再起作用,仅仅是让legend的左下角与bbox_to_anchor指定的点重合
from matplotlib.patches import Rectangle
import matplotlib.patches as mpatches
fig, ax = plt.subplots(figsize=(10, 5))
ax.set_ylim(0, 5)
ax.set_xlim(0, 10)
ax.grid()
ax.scatter(6, 3, color='red', marker="*")
ax.tick_params(size=8)
patch = mpatches.Patch(color='blue', label='legend')
ax.legend(handles=[patch], loc=(10,10), bbox_to_anchor=(6/10, 3/5), borderaxespad=0, fontsize=18, edgecolor='yellow')
# ax.annotate('(14, 6)', xy=(14, 6), xytext=(15, 6.5),arrowprops={'arrowstyle':'->'}, fontsize=18)
ax.text(5.5, 4, '此时loc不再起作用', fontsize=18)
fig.savefig(r'/Users/llh/Desktop/文章/matplot_legend/6.png', dpi=600)
bbox_to_anchor代表一块矩形区域时(以下称为矩形),同样分为两种情况,此时就和bbox_to_anchor为默认值时差不多了当loc为字符串时(字符串所代表的9个位置),意味着legend和矩形的‘’字符串‘’位置重合
例:loc='upper left',bbox_to_anchor=(x, y, width, height) 表示左上角重合
from matplotlib.patches import Rectangle
import matplotlib.patches as mpatches
fig, ax = plt.subplots(figsize=(10, 5))
ax.set_ylim(0, 5)
ax.set_xlim(0, 10)
ax.grid()
rect = Rectangle((2, 1), 6, 3, fill=False, ec='red',lw=5)
ax.add_artist(rect)
ax.tick_params(size=8)
patch = mpatches.Patch(color='blue', label='legend')
ax.legend(handles=[patch], loc='upper left', bbox_to_anchor=(1/5, 1/5, 6/10, 3/5), borderaxespad=0, fontsize=18, edgecolor='yellow')
# ax.annotate('(14, 6)', xy=(14, 6), xytext=(15, 6.5),arrowprops={'arrowstyle':'->'}, fontsize=18)
ax.text(2, 4.3, '红框即为bbox_to_anchor所代表的区域', fontsize=18)
fig.savefig(r'/Users/llh/Desktop/文章/matplot_legend/6.png', dpi=600)
2.当loc 为(x, y)时,意味将legend的左下角位于loc所指定的点上,只不过此时(x, y) 是矩形区域的相对位置
from matplotlib.patches import Rectangle
import matplotlib.patches as mpatches
fig, ax = plt.subplots(figsize=(10, 5))
ax.set_ylim(0, 5)
ax.set_xlim(0, 10)
ax.grid()
rect = Rectangle((2, 1), 6, 3, fill=False, ec='red',lw=5)
ax.add_artist(rect)
ax.tick_params(size=8)
patch = mpatches.Patch(color='blue', label='legend')
ax.legend(handles=[patch], loc=(1/3,1/3), bbox_to_anchor=(1/5, 1/5, 6/10, 3/5), borderaxespad=0, fontsize=18, edgecolor='yellow')
# ax.annotate('(14, 6)', xy=(14, 6), xytext=(15, 6.5),arrowprops={'arrowstyle':'->'}, fontsize=18)
ax.text(2, 4.3, '红框即为bbox_to_anchor所代表的区域', fontsize=18)
fig.savefig(r'/Users/llh/Desktop/文章/matplot_legend/6.png', dpi=600)
还有最后一种情况,当bbox_to_anchor是一条线时,这个就留给各位去探索了
以上就是关于loc与bbox_to_anchor如何控制legend位置的全部内容了
如发现错误,欢迎批评指正,欢迎随时交流
python plt legend并排_matplotlib如何控制legend的位置之二相关推荐
- python图例重复显示_matplotlib中的legend()——用于显示图例
Keyword Description loc a location code prop the font property (matplotlib.font_manager.FontProperti ...
- python plt legend并排,matplotlib Legend 图例用法
matplotLib Legend添加图例:展示数据的信息 用法: legend(): 默认获取各组数据的Label并展示在图框左上角 legend(labels): legend(handles, ...
- python中matplotlib库legend_matplotlib中的legend()—显示图例
legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import numpy as np x = np.arange(1, 11) fig = ...
- python使用Axes3D画三维图加入legend图例时报错AttributeError: ‘Poly3DCollection‘ object has no attribute ‘_edgecolo
Q:python使用Axes3D画三维图加入legend图例时报错AttributeError: 'Poly3DCollection' object has no attribute '_edgeco ...
- python 2: 解决python中的plot函数的图例legend不能显示中文问题
python 2: 解决python中的plot函数的图例legend不能显示中文问题 参考文章: (1)python 2: 解决python中的plot函数的图例legend不能显示中文问题 (2) ...
- 如何使用Python plt像MATLAB一样绘图
1. Python plt绘图 使用Python的绘图,制作训练的迭代次数与准确率.损失函数值的图像: 使用说明 代码 使用说明 Introduction 简介: 我们在用MATLAB绘图时感觉很轻巧 ...
- python 数组合并排重_并排深度学习:Julia vs Python
python 数组合并排重 Julia could possibly be the biggest threat to Python. For a variety of applications, J ...
- python图例重复显示_matplotlib中的图例中有重复项?
下面是一种在正常分配标签后删除重复图例项的方法:representatives=[[[-100,40],[-50,20],[0,0],[75,-5],[100,5]], #made up some d ...
- Python实现 通过RS232 SCPI 命令控制Tektronix DMM4050
Python实现 通过RS232 SCPI 命令控制Tektronix DMM4050@TOC import serial import matplotlib.pyplot as plt import ...
最新文章
- sar sensor传感器的作用_传感器攻防战-惯导IMU
- c语言打开文件出现分段故障,我不明白为什么我使用ifstream时出现分段错误
- webx3 日志系统级别问题
- echo count(“abc”); 输出什么?
- oracle插入数据语句实例,oracle Insert 用法总结
- 后端:请谨慎使用Arrays.asList、ArrayList的subList
- 知识图谱最新权威综述论文解读:实体发现
- 分享一个JAVA专业接口开发利器,牛牛牛新鲜出炉!!!
- 【第七章】 springboot + retrofit
- 架构师速成6.4-开发框架
- java list 泛型 转换_Java中List与数组互相转换
- HTML5权威指南 6.多媒体相关API
- 中国便利店行业战略发展及投资盈利研究报告2022年版
- 【PS】如何使用Photoshop调整图片指定部分的大小
- Bypass 360主机卫士SQL注入防御(多姿势)
- c语言蚂蚁搬,关于蚂蚁搬食的作文
- 拼多多是如何做用户增长的?
- VB实现移动鼠标产生粒子效果
- 中考可以使用计算机吗,2017年中考可以带计算器吗
- 大学c语言活动策划,大学校园文化班级趣味运动会活动方案