修复 matplotlib 中图例的大小 [英] Fix size of legend in matplotlib

查看:69
本文介绍了修复 matplotlib 中图例的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib进行自动绘图,并在其上绘制几个不同的特征(背景是填充轮廓图,上方是pcolormesh).我试图绘制的最重要的特征是几个散点图,带有不同的标签和图标.

I am trying to make automated plots with matplotlib, with several different features plotted on top of one another (the background is a filled contour plot, one level above is a pcolormesh). The topmost feature that I'm trying to plot is several scatter plots, with different labels and icons.

我正在尝试使用以下命令向此地块添加图例:

I'm trying to add a legend to this plot, currently using the following commands:

leg = ax.legend(legplots,
           legnames,
           scatterpoints=1,
           loc='upper center',
           ncol=3,
           fontsize=14,
         bbox_to_anchor=(0.5, -0.14),
          fancybox=True, shadow=True)

假定ax是主轴,并且legplot和legnames分别是散点图及其适当标签的列表.

Assume that ax is the main axes, and that legplots and legnames are lists of scatter plots and their appropriate labels respectively.

添加图例可以正常工作,但是随着我拥有的脚手架数量(以及名称的长度)的变化,在对地块进行动画处理时,图例的大小会增加和缩小.如何控制图例框的大小和图例中的列宽?这可能吗?

Adding the legend works correctly, but as the number of legplots that I have (and their name lengths) vary, as you animate the plots, the legend grows and shrinks in size. How do I control the size of the legend box and the column widths inside the legend? Is this possible?

推荐答案

目前你的边界框的大小为 0,因为你只指定了它的位置 ((0.5, -0.14)).

At the moment your bounding box has a size of 0, since you only specify its position ((0.5, -0.14)).

您可以将图例的边界框设置为大于 0 并且也足够大以容纳最大数量的元素所需的最大尺寸.我认为您需要通过以下方式找到该尺寸反复试验.

You can set the bounding box of the legend to be bigger than 0 and also big enough for the maximum size that it needs to have for the maximum number of elements to fit in. I think you will need to find that size by trial and error.

所以使用完整的四元组表示法

So using the full 4-tuple notation

bbox_to_anchor=(x0, y0, width, height)

与适当的 loc 参数和关键字参数 mode ="expand" 结合使用,可以使图例足够大以满足您的需要.有关 4 元组符号的更详细说明,请参阅 这篇文章 并查看 legend位置指南.

in conjunction with an appropriate loc parameter and the keyword argument mode="expand" will allow you to make the legend big enough for your needs. For a more detailed explanation about the 4-tuple notation see this post and also look at the legend location guide.

这篇关于修复 matplotlib 中图例的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆