Matplotlib-全局图例和标题子图 [英] Matplotlib - global legend and title aside subplots

查看:670
本文介绍了Matplotlib-全局图例和标题子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用matplot并管理了一些基本绘图,但是现在我发现很难发现如何做一些我现在需要的东西了:(

I've started with matplot and managed some basic plots, but now I find it hard to discover how to do some stuff I need now :(

我的实际问题是如何在带有子图的图形上放置全局标题和全局图例.

My actual question is how to place a global title and global legend on a figure with subplots.

我正在做2x3子图,其中有很多不同颜色的图形(大约200张).为了区分(大多数)我写了类似的内容

I'm doing 2x3 subplots where I have a lot of different graphs in various colors (about 200). To distinguish (most) of them I wrote something like

def style(i, total):
    return dict(color=jet(i/total),
                linestyle=["-", "--", "-.", ":"][i%4],
                marker=["+", "*", "1", "2", "3", "4", "s"][i%7])

fig=plt.figure()
p0=fig.add_subplot(321)
for i, y in enumerate(data):
    p0.plot(x, trans0(y), "-", label=i, **style(i, total))
# and more subplots with other transN functions

(对此有任何想法吗?:))每个子图具有相同的样式功能.

(any thoughts on this? :)) Each subplot has the same style function.

现在,我正在尝试为所有子图取一个全局标题,并为一个解释所有样式的全局图例.另外,我还需要使字体变小以适合那里的所有200种样式(我不需要完全独特的样式,但至少需要尝试一下)

Now I'm trying to get a global title for all subplots and also a global legend which explains all styles. Also I need to make the font tiny to fit all 200 styles on there (I don't need completely unique styles, but at least some attempt)

有人可以帮我解决这个问题吗?

Can someone help me solve this task?

推荐答案

全局标题:在较新的matplotlib版本中,可以使用

Global title: In newer releases of matplotlib one can use Figure.suptitle() method of Figure:

import matplotlib.pyplot as plt
fig = plt.gcf()
fig.suptitle("Title centered above all subplots", fontsize=14)

或者(基于下面的 @Steven C. Howell 的评论(谢谢!)) ,请使用 matplotlib.pyplot.suptitle()函数:

Alternatively (based on @Steven C. Howell's comment below (thank you!)), use the matplotlib.pyplot.suptitle() function:

 import matplotlib.pyplot as plt
 # plot stuff
 # ...
 plt.suptitle("Title centered above all subplots", fontsize=14)

这篇关于Matplotlib-全局图例和标题子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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