添加批注框,一个matplotlib轮廓/热图情节 [英] Adding an annotation box to a matplotlib contour/heat map plot

查看:1105
本文介绍了添加批注框,一个matplotlib轮廓/热图情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成同一个彩条轮廓图/热图,然后添加注释框。这个数字是丑陋的,但我希望得到什么:

I want generate a contour plot/heat map with a color bar and then add an annotation box. This figure is ugly, but gets at what I want:

add_subplot()是不够的。如果我试图把一切都放在同一个插曲,箱子被掩盖了。我可以通过使它dragable然后与图像的大小把玩解决这个问题,但是这是没有好处的。我将不得不作出一些这些图像,所有的标准尺寸,我不能与规模一遍又一遍的战斗。

add_subplot() is not enough. If I try to put everything in the same subplot, the box gets covered up. I can get around this by making it dragable and then futzing with the size of the image, but this is no good. I am going to have to make several of these images, all of a standard size, and I can't fight with the size over and over again.

我试过轴()为好,把盒子在一个单独的轴。但是,对于绘图,涵盖了大部分我的彩条生成一个新的窗口。我想会有办法使窗口完全透明的。但是,当我到这一点,我觉得我的方法一定是完全错误的。

I tried axes() as well, putting the box in a separate axis. But that generates a new window for plotting that covers up most of my color bar. I guess there would be ways to make the window completely transparent. But when I get to that point, I think my approach must be completely wrong.

这似乎并不像它应该是这么难。任何想法?

This doesn't seem like it should be so hard. Any ideas?

推荐答案

Annotation box to contour map:

做过这样的:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import randn

from mpl_toolkits.axes_grid1.axes_divider import HBoxDivider
import mpl_toolkits.axes_grid1.axes_size as Size


def make_heights_equal(fig, rect, ax1, ax2, ax3, pad):
    # pad in inches

    h1, v1 = Size.AxesX(ax1), Size.AxesY(ax1)
    h2, v2 = Size.AxesX(ax2, 0.1), Size.AxesY(ax2)
    h3, v3 = Size.AxesX(ax3), Size.AxesY(ax3)

    pad_v = Size.Scaled(1)
    pad_h = Size.Fixed(pad)

    my_divider = HBoxDivider(fig, rect,
                             horizontal=[h1, pad_h, h2, pad_h, h3],
                             vertical=[v1, pad_v, v2, pad_v, v3])


    ax1.set_axes_locator(my_divider.new_locator(0))
    ax2.set_axes_locator(my_divider.new_locator(2))
    ax3.set_axes_locator(my_divider.new_locator(4))


# Make plot with vertical (default) colorbar
fig = plt.figure()
img_ax = fig.add_subplot(131)
bar_ax = fig.add_subplot(132)
ann_ax = fig.add_subplot(133)

data = np.clip(randn(250, 250), -1, 1)

im = img_ax.imshow(data, interpolation='nearest', cmap=cm.coolwarm)

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(im, cax=bar_ax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])# vertically oriented colorbar

ann_ax.axis('off')

ann_ax.annotate("Hello, I'm an annotation", (0.5, 0.5),
                 xycoords="axes fraction", va="center", ha="center",
                 bbox=dict(boxstyle="round, pad=1", fc="w")) 

make_heights_equal(fig, 111, img_ax, bar_ax, ann_ax, 0.2)

plt.savefig("try.png")

这篇关于添加批注框,一个matplotlib轮廓/热图情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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