在Matplotlib中将图例作为单独的图片获取 [英] Get legend as a separate picture in Matplotlib

查看:374
本文介绍了在Matplotlib中将图例作为单独的图片获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Web应用程序,并希望在页面的不同位置显示图形及其图例.这意味着我需要将图例另存为单独的png文件.在Matplotlib中以某种或多或少的直接方式可能吗?

I'm developing a Web application and want to display a figure and its legend in different locations on the page. Which means I need to save the legend as a separate png file. Is this possible in Matplotlib in a more or less straightforward way?

推荐答案

这可能有效:

import pylab
fig = pylab.figure()
figlegend = pylab.figure(figsize=(3,2))
ax = fig.add_subplot(111)
lines = ax.plot(range(10), pylab.randn(10), range(10), pylab.randn(10))
figlegend.legend(lines, ('one', 'two'), 'center')
fig.show()
figlegend.show()
figlegend.savefig('legend.png')

这篇关于在Matplotlib中将图例作为单独的图片获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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