栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python-如何将图例排除在情节之外

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python-如何将图例排除在情节之外

有很多方法可以做你想要的。要添加

@inalis
@Navi
所说的内容,可以使用
bbox_to_anchor
关键字参数将图例部分地放置在轴外and/or 减小字体大小。

在考虑减小字体大小(这会使阅读起来非常困难)之前,请尝试将图例放在不同的位置:

因此,让我们从一个通用示例开始:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(10)fig = plt.figure()ax = plt.subplot(111)for i in xrange(5):    ax.plot(x, i * x, label='$y = %ix$' % i)ax.legend()plt.show()

如果我们做同样的事情,但是使用

bbox_to_anchor
关键字参数,我们可以将图例稍微移出轴边界:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(10)fig = plt.figure()ax = plt.subplot(111)for i in xrange(5):    ax.plot(x, i * x, label='$y = %ix$' % i)ax.legend(bbox_to_anchor=(1.1, 1.05))plt.show()

同样,你可以使图例更加水平和/或将其放在图的顶部(我也打开了圆角和简单的阴影):

import matplotlib.pyplot as pltimport numpy as npx = np.arange(10)fig = plt.figure()ax = plt.subplot(111)for i in xrange(5):    line, = ax.plot(x, i * x, label='$y = %ix$'%i)ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),          ncol=3, fancybox=True, shadow=True)plt.show()

另外,你可以缩小当前图的宽度,并将图例完全放在图的轴外:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(10)fig = plt.figure()ax = plt.subplot(111)for i in xrange(5):    ax.plot(x, i * x, label='$y = %ix$'%i)# Shrink current axis by 20%box = ax.get_position()ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])# Put a legend to the right of the current axisax.legend(loc='center left', bbox_to_anchor=(1, 0.5))plt.show()

同样,你可以垂直缩小图,并在底部放置水平图例:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(10)fig = plt.figure()ax = plt.subplot(111)for i in xrange(5):    line, = ax.plot(x, i * x, label='$y = %ix$'%i)# Shrink current axis's height by 10% on the bottombox = ax.get_position()ax.set_position([box.x0, box.y0 + box.height * 0.1,      box.width, box.height * 0.9])# Put a legend below current axisax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),          fancybox=True, shadow=True, ncol=5)plt.show()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/594625.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号