首页 > 其他分享 >matplotlib中如何将图例位置放在绘图区外

matplotlib中如何将图例位置放在绘图区外

时间:2023-02-22 17:02:18浏览次数:50  
标签:upper plt matplotlib label 图例 bbox np import 区外

bbox_to_anchor参数

可以通过bbox_to_anchor参数将图例放置在坐标系外。bbox表示容纳图例的边界框-bounding box

  • plt.legend(bbox_to_anchor=(x0, y0, width, height), loc=)

widthheight是图例框的宽度和高度,而 (x0, y0) 是边界框 loc 的坐标。
边界框的坐标是相对于坐标系的位置,(0,0)是坐标系的左下角,(1,1)是坐标系的右上角

loc参数可选值(默认为0):

int str
0 best
1 upper right
2 upper left
3 lower left
4 lower right
5 right
6 center left
7 center right
8 lower center
9 upper center
10 center

示例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")

plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
plt.tight_layout()
plt.show()

如果未调用tight_layout(),保存的图片将会显示不完整

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")

plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
# plt.show()
# plt.tight_layout()
plt.savefig('0.png')

使用bbox_extra_artists bbox_inches参数,防止图像保存时被裁剪

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")

lg = plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')

plt.savefig('example.png', 
            dpi=300, 
            format='png', 
            bbox_extra_artists=(lg,), 
            bbox_inches='tight')

bbox_extra_artists 指定 Artist 的列表,在计算bbox最小边界时会将这些Artist考虑在内。
如果將 bbox_inches 设定为 tight,它将计算出图中的紧凑型 bbox

转载自:原文链接

标签:upper,plt,matplotlib,label,图例,bbox,np,import,区外
From: https://www.cnblogs.com/impromptu/p/17145047.html

相关文章

  • Matplotlib中绘制双y轴合并图例
    转载自:https://www.cnblogs.com/atanisi/p/8530693.html1.绘制双y轴:twinx(),双x轴:twiny()#-*-coding:utf-8-*-importnumpyasnpimportmatplotlib.pyplotas......
  • ​​python--matplotlib(4)​
    前言 Matplotlib画图工具的官网地址是http://matplotlib.org/Python环境下实现Matlab制图功能的第三方库,需要numpy库的支持,支持用户方便设计出二维、三维数据的图形显示,制......
  • matplotlib 饼图
    importmatplotlib.pyplotaspltimportnumpyasnpif__name__=="__main__":#0、修改支持中文的字体plt.rcParams["font.sans-serif"]=["SimHei"]#......
  • matplotlib 直方图绘制
    importmatplotlib.pyplotaspltimportnumpyasnpif__name__=="__main__":#0、修改支持中文的字体plt.rcParams["font.sans-serif"]=["SimHei"]#......
  • matplotlib 柱状图
    importmatplotlibimportmatplotlib.pyplotaspltimportnumpyasnpif__name__=="__main__":#0、修改支持中文的字体plt.rcParams["font.sans-serif......
  • matplotlib 散点图
    应用场景:探究不同变量之间的内在关系importmatplotlibimportmatplotlib.pyplotaspltimportnumpyasnpif__name__=="__main__":#0、修改支持中文的字......
  • python使用seaborn画热力图中设置colorbar图例刻度字体大小(2)
    1.问题描述使用matplotlib.pyplot画带色标(colorbar)的图时候,可以设置x,y轴坐标字体的大小,但没有办法设置图例刻度条字体的大小。期望效果如下如所示。  2.解决方......
  • 使用VS code将本地的项目推送到gitee的方法(图例)
    第一步:在gitee新建一个仓库写上基本信息,点击创建(不要勾选初始化),记住项目名称成功后的页面中,把这个项目地址复制好 第二步VScode推送至gitee   把......
  • matplotlib 在多个绘图区绘图
    创建多个画布#2、创建画布#nrows是行数#ncols是列数#1行2列就是横向排列的两个绘图区#2行1列就是纵向排列的两个绘图区#返回图形对象和绘图对象figure,axe......
  • matplotlib 添加网格、绘制两条线、添加图例
    添加网格#显示网格#linestyle网格线#透明度plt.grid(True,linestyle="--",alpha=0.5)添加描述信息#添加描述信息plt.xlabel("时间")plt.ylabel("温度")plt......