如何使用 matplotlib.pyplot 更改图例大小 [英] How to change legend size with matplotlib.pyplot

查看:173
本文介绍了如何使用 matplotlib.pyplot 更改图例大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个简单的问题:我正在尝试使用 matplotlib.pyplot 使我的图例的大小变小(即,文本变小).我使用的代码是这样的:

Simple question here: I'm trying to get the size of my legend using matplotlib.pyplot to be smaller (i.e., the text to be smaller). The code I'm using goes something like this:

plot.figure()
plot.scatter(k, sum_cf, color='black', label='Sum of Cause Fractions')
plot.scatter(k, data[:, 0],  color='b', label='Dis 1: cf = .6, var = .2')
plot.scatter(k, data[:, 1],  color='r',  label='Dis 2: cf = .2, var = .1')
plot.scatter(k, data[:, 2],  color='g', label='Dis 3: cf = .1, var = .01')
plot.legend(loc=2)

推荐答案

您可以通过调整 prop 关键字为图例设置单独的字体大小.

You can set an individual font size for the legend by adjusting the prop keyword.

plot.legend(loc=2, prop={'size': 6})

这需要与 matplotlib.font_manager.FontProperties 属性对应的关键字字典.请参阅图例文档:

This takes a dictionary of keywords corresponding to matplotlib.font_manager.FontProperties properties. See the documentation for legend:

关键字参数:

prop: [ None | FontProperties | dict ]
    A matplotlib.font_manager.FontProperties instance. If prop is a 
    dictionary, a new instance will be created with prop. If None, use
    rc settings.

也有可能,从 1.2.1 开始,使用关键字 fontsize.

It is also possible, as of version 1.2.1, to use the keyword fontsize.

这篇关于如何使用 matplotlib.pyplot 更改图例大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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