imshow 和情节并排 [英] imshow and plot side by side

查看:46
本文介绍了imshow 和情节并排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将并排的 numpy 数组显示为同一数组的图像和 seaborn distplot.我想出了以下功能:

I'm trying to put side-by-side numpy array displayed as image and seaborn distplot of the same array. I've came up with the following function:

def visualize(arr):
    f, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw = {'width_ratios': [1, 3]})

    ax1.imshow(arr)

    flat = arr.flatten()
    x = flat[~np.isnan(flat)]
    sns.distplot(x, ax=ax2)
    plt.show()

产生:

如您所见,图像的高度小于图的高度.如何修改我的函数,以使绘图和图像显示具有相同的高度?

As you can see, the image has smaller height than the plot. How can I modify my function in order to have the same height for the plot and the imshow?

我想要图像和情节的以下位置:

I want the following placement of the image and the plot:

推荐答案

有很多方法可以解决此问题.以下所有内容或多或少都会产生相同的图像

There are just so many ways to tackle this. All of the following will give more or less the same image

您可以减少可用空间,以便将两个图都限制在相同的垂直边距上.可以通过

You may reduce the available space such that both plots are constrained to the same vertical margins. This can be done by

  1. 降低图形高度

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6,2.3), ...)

使用subplots_adjust来限制边距

using subplots_adjust to limit the margins

fig.subplots_adjust(top=0.7, bottom=0.3)

B.使用 InsetPosition

您可以使用 mpl_toolkits.axes_grid1.inset_locator.InsetPosition 来调整第二个轴的坐标以匹配第一个轴的坐标.

B. Use InsetPosition

You may use mpl_toolkits.axes_grid1.inset_locator.InsetPosition to adjust the coordinates of the second axes to match those of the first one.

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition

def visualize(arr):
    fig, (ax1, ax2) = plt.subplots(1, 2,
                               gridspec_kw = {'width_ratios': [1, 3]})

    ax1.imshow(arr)

    flat = arr.flatten()
    x = flat[~np.isnan(flat)]
    sns.distplot(x, ax=ax2)

    ip = InsetPosition(ax1, [1.5,0,3,1]) 
    ax2.set_axes_locator(ip)

    plt.show()

arr = np.random.randn(200,120)
visualize(arr)

C.使用分轴器

您可以只为图像创建轴,然后使用 mpl_toolkits.axes_grid1.make_axes_locatable 在它旁边创建一个新轴.

C. Use an axes divider

You may create only the axes for the image and then use mpl_toolkits.axes_grid1.make_axes_locatable to create a new axes next to it.

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

def visualize(arr):
    fig, ax = plt.subplots()
    divider = make_axes_locatable(ax)
    ax2 = divider.new_horizontal(size="300%", pad=0.5)
    fig.add_axes(ax2)

    ax.imshow(arr)

    flat = arr.flatten()
    x = flat[~np.isnan(flat)]
    sns.distplot(x, ax=ax2)

    plt.show()

arr = np.random.randn(200,120)
visualize(arr)

D.计算所需的纵横比

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

def visualize(arr):
    gkw = {'width_ratios':[1, 3] }
    fig, (ax1, ax2) = plt.subplots(1, 2,  gridspec_kw = gkw )

    ax1.imshow(arr)

    flat = arr.flatten()
    x = flat[~np.isnan(flat)]
    sns.distplot(x, ax=ax2)

    ya = np.diff(np.array(ax2.get_ylim()))[0]
    xa = np.diff(np.array(ax2.get_xlim()))[0]
    wa = gkw['width_ratios'][0]/float(gkw['width_ratios'][1])
    ia = arr.shape[0]/float(arr.shape[1])
    ax2.set_aspect(float(wa*ia/(ya/xa)))

    plt.show()

arr = np.random.randn(200,120)
visualize(arr)

E.动态复制位置

您可以获取左侧图的位置并将其 y 坐标复制到右侧子图的位置.这是现有代码的不错的附加组件.缺点是必要的,因为对图形大小的后续更改需要重新计算位置.

E. Dynamically copy positions

You may get the position of the left plot and copy its y-coordinates to the right subplot's position. This is a nice add-on to existing code. The drawback is necessary because subsequent changes to the figure size require to recalculate the positions.

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

def visualize(arr):
    gkw = {'width_ratios':[1, 3] }
    fig, (ax1, ax2) = plt.subplots(1, 2,  gridspec_kw = gkw )

    ax1.imshow(arr)

    flat = arr.flatten()
    x = flat[~np.isnan(flat)]
    sns.distplot(x, ax=ax2)

    def on_resize(evt=None):
        ax1.apply_aspect()
        bb1 = ax1.get_position()
        bb2 = ax2.get_position()
        bb2.y0 = bb1.y0; bb2.y1 = bb1.y1
        ax2.set_position(bb2)

    fig.canvas.mpl_connect("resize_event", on_resize)
    on_resize()

    plt.show()

arr = np.random.randn(200,120)
visualize(arr)

这篇关于imshow 和情节并排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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