使用matplotlib imshow和scatter获得相同的子图大小 [英] Getting the same subplot size using matplotlib imshow and scatter

查看:279
本文介绍了使用matplotlib imshow和scatter获得相同的子图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一张图中绘制图像(使用matplotlib.imshow)和散点图.尝试此操作时,图像看起来比散点图小.较小的示例代码如下所示:

I am trying to plot an image (using matplotlib.imshow) and a scatter plot within the same figure. When trying this, the image appears smaller than the scatter plot. Small example code is shown below:

import matplotlib.pyplot as plt
import numpy as np

image = np.random.randint(100,200,(200,200))
x = np.arange(0,10,0.1)
y = np.sin(x)

fig, (ax1, ax2) = plt.subplots(1,2)
ax1.imshow(image)
ax2.scatter(x,y)

plt.show()

哪个给出下图:

如何使两个子高度相同? (和我想的宽度)

How can I get the two sublpots to have the same height? (and width I suppose)

我已尝试使用 gridspec ,如但这给出了相同的结果.我还尝试使用以下方法手动调整子图的大小:

But this gives the same result. I have also tried to adjust the subplot sizes manually by using:

fig = plt.figure()
ax1 = plt.axes([0.05,0.05,0.45,0.9])
ax2 = plt.axes([0.55,0.19,0.45,0.62])

ax1.imshow(image)
ax2.scatter(x,y)

通过反复试验,我可以将两个子图的大小更改为正确的大小,尽管整体图形大小的任何变化都将意味着子图的大小将不再相同.

By trial and error I can get the two subplots to the correct size, though any change in the overall figure size will mean that the subplots will no longer be the same size.

是否可以在不手动更改轴尺寸的情况下使imshowscatter绘图在图形中显示相同的大小?

Is there a way to make imshow and a scatter plot appear the same size in a figure without manually changing the axes sizes?

我正在使用Python 2.7和matplotlib 2.0.0

I am using Python 2.7 and matplotlib 2.0.0

推荐答案

目前尚不清楚您想要的结果是什么.

It's not perfectly clear what your desired outcome is.

  1. 您可以在图像上使用自动宽高比

  1. You may use automatic aspect on the image

ax.imshow(z, aspect="auto")

或者您可以根据线条图的轴限制设置线条图的纵横比,以使其具有与图像相同的尺寸(如果图像的x和y尺寸相等)

Or you may set the aspect of the line plot depending on its axis limits such that it gets the same size as the image (in case the image has equal x and y sizes)

asp = np.diff(ax2.get_xlim())[0] / np.diff(ax2.get_ylim())[0]
ax2.set_aspect(asp)

完整的代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,10,20)
y = np.sin(x)
z = np.random.rand(100,100)

fig, (ax, ax2) = plt.subplots(ncols=2)

ax.imshow(z)
ax2.plot(x,y, marker=".")

asp = np.diff(ax2.get_xlim())[0] / np.diff(ax2.get_ylim())[0]
ax2.set_aspect(asp)

plt.show()

如果图像没有相等的限制(不是正方形),则仍然需要按图像的纵横比进行划分:

If the image does not have equal limits (is not square), one still needs to divide by the aspect of the image:

asp = np.diff(ax2.get_xlim())[0] / np.diff(ax2.get_ylim())[0]
asp /= np.abs(np.diff(ax1.get_xlim())[0] / np.diff(ax1.get_ylim())[0])
ax2.set_aspect(asp)

  • 更复杂的解决方案:

  • More sophisticated solutions:

    • This answer for using the subplot parameters to achieve a certain aspect.

    如果您想使用mpl_toolkits并使您的手变脏,此答案将是一个很好的选择阅读.

    If you want to use mpl_toolkits and make your hands dirty, this answer would be a good read.

    这篇关于使用matplotlib imshow和scatter获得相同的子图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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