【问题标题】:How to zoomed a portion of image and insert in the same plot in matplotlib如何缩放图像的一部分并在 matplotlib 的同一图中插入
【发布时间】:2012-11-15 00:14:48
【问题描述】:

我想缩放一部分数据/图像并将其绘制在同一个图中。它看起来像这个数字。

是否可以在同一个图中插入一部分缩放图像。我认为可以用 subplot 绘制另一个图形,但它会绘制两个不同的图形。我还阅读了添加补丁以插入矩形/圆形,但不确定将一部分图像插入图中是否有用。我基本上从文本文件中加载数据并使用如下所示的简单绘图命令对其进行绘图。

我从 matplotlib 图片库here 中找到了一个相关示例,但不确定它是如何工作的。非常感谢您的帮助。

from numpy import *
import os
import matplotlib.pyplot as plt
data = loadtxt(os.getcwd()+txtfl[0], skiprows=1)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.semilogx(data[:,1],data[:,2])
plt.show()

【问题讨论】:

  • 您已经找到了与您想要的类似的代码。做研究的荣誉。现在,该代码的哪一部分需要解释?我们怎样才能帮助你钓鱼,而不是给你鱼? (作为一种通用工具,我发现注释行和修改行中的数字是一种粗略但有效的方法来找出语句的作用......)
  • 感谢您的回复。我阅读了那些代码,但无法理解。我是一个基本的 matplotlib 用户并开始学习 matplotlib。我试图应用这些代码,但对我不起作用。例如我试图分配 ax2 = ax1.axes([0.2*amin(data[:,1], 0.5*amax(data[:,1]),0.5*amin(data[:,2]),0.8* amax(data[:,2])]) 然后尝试绘制为:ax2.semilogx(data[3:8,1],data[3:8,2])。我这样得到错误。不知道如何分配轴参数,然后绘制它。

标签: python image matplotlib


【解决方案1】:

使用可运行代码是其中之一 学习 Python 的最快方法。

让我们从code from the matplotlib example gallery开始。

鉴于代码中的 cmets,代码似乎分为 4 个主要节。 第一节生成一些数据,第二节生成主情节, 第三和第四节创建插入轴。

我们知道如何生成数据并绘制主图,所以让我们关注第三节:

a = axes([.65, .6, .2, .2], axisbg='y')
n, bins, patches = hist(s, 400, normed=1)
title('Probability')
setp(a, xticks=[], yticks=[])

将示例代码复制到一个名为test.py 的新文件中。

如果我们将.65 更改为.3 会发生什么?

a = axes([.35, .6, .2, .2], axisbg='y')

运行脚本:

python test.py

您会发现“概率”插图移到了左侧。 所以axes 函数控制插入的位置。 如果你多玩一些数字,你会发现 (.35, .6) 是 插图左下角的位置,(.2, .2) 是宽度和 插图的高度。数字从 0 到 1,(0,0) 位于 图的左下角。

好的,现在我们正在做饭。在下一行我们有:

n, bins, patches = hist(s, 400, normed=1)

您可能会认出这是matplotlib command for drawing a histogram,但是 如果不是,将数字 400 更改为 10,将产生一个图像 更粗的直方图,所以再次通过玩数字你很快就会弄清楚 这条线与插图中的图像有关。

您需要在此处致电semilogx(data[3:8,1],data[3:8,2])

线title('Probability') 显然会生成插入上方的文本。

最后我们来到setp(a, xticks=[], yticks=[])。没有数字可玩, 那么如果我们通过在行首添加# 来注释掉整行会发生什么:

# setp(a, xticks=[], yticks=[])

重新运行脚本。哦!现在插入轴上有很多刻度线和刻度线标签。 美好的。所以现在我们知道setp(a, xticks=[], yticks=[]) 会从坐标轴a 中删除刻度线和标签。

现在,理论上您有足够的信息可以将此代码应用于您的问题。 但还有一个潜在的绊脚石:matplotlib 示例使用 from pylab import * 而你使用import matplotlib.pyplot as plt

The matplotlib FAQimport matplotlib.pyplot as plt 是编写脚本时推荐使用 matplotlib 的方法,而 from pylab import * 用于交互式会话。所以你做对了,(虽然我也建议使用import numpy as np而不是from numpy import *)。

那么我们如何将 matplotlib 示例转换为使用 import matplotlib.pyplot as plt 运行?

进行转换需要一些使用 matplotlib 的经验。一般来说,你只需 在 axessetp 等裸名前添加 plt.,但有时 函数来自 numpy,有时调用应该来自轴 对象,而不是来自模块 plt。需要经验才能知道所有这些在哪里 函数来自。谷歌搜索函数的名称和“matplotlib”会有所帮助。 阅读示例代码可以积累经验,但没有捷径可走。

这样,转换后的代码就变成了

ax2 = plt.axes([.65, .6, .2, .2], axisbg='y')
ax2.semilogx(t[3:8],s[3:8])
plt.setp(ax2, xticks=[], yticks=[])

你可以像这样在你的代码中使用它:

from numpy import *
import os
import matplotlib.pyplot as plt
data = loadtxt(os.getcwd()+txtfl[0], skiprows=1)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.semilogx(data[:,1],data[:,2])

ax2 = plt.axes([.65, .6, .2, .2], axisbg='y')
ax2.semilogx(data[3:8,1],data[3:8,2])
plt.setp(ax2, xticks=[], yticks=[])

plt.show()

【讨论】:

  • 谢谢。它回答了我的问题。想知道为什么 ax2 = fig1.axes([.65. 0.6, 0.2, 0.2], axisbg = 'y') 不起作用。例如,如果我想绘制两个图形并且我想在两个图形中添加插图。如果我使用 ax3 = fig2.add_subplot(111) 然后 ax3.semilogx(x,y) 然后 ax4 = plt.axes([.2, 0.5, 0.2, 0.2], axisbg = 'y') 和 ax4.semilogx( x1,y1) 。它将两个插图添加到第二个数字中。
  • plt.axes 创建一个新轴。它将轴添加到当前图形。 (将图形视为整个窗口区域,将轴视为绘制图形的图形的一部分。)fig1.axes 返回包含在fig1 中的轴列表。我很惊讶fig1.axes([...]) 没有为您生成TypeError。所以首先,将fig1.axes 更改为plt.axes。接下来,知道调用fig2 = plt.figure() 会使fig2 成为活动人物。因此,任何对plt.axes 的调用都会将插入轴添加到fig2plt.figureplt.axes 调用的顺序很重要。
  • 谢谢。它可以将 fig2 = plt.figure() 和 ax3 = fig2.add_subplot(111) 放在 ax2.semilogx([...]) 之后。
【解决方案2】:

最简单的方法是结合“zoomed_inset_axes”和“mark_inset”,其描述和 相关示例可以在这里找到: Overview of AxesGrid toolkit

【讨论】:

【解决方案3】:

我所知道的最好的方法是使用 mpl_toolkits.axes_grid1.inset_locator(matplotlib 的一部分)。

这里有一个很好的源代码示例:https://github.com/NelleV/jhepc/tree/master/2013/entry10

【讨论】:

  • 好身材!
【解决方案4】:

使用 matplotlib 放大图的一部分的基本步骤

import numpy as np
from matplotlib import pyplot as plt

# Generate the main data
X = np.linspace(-6, 6, 1024)
Y = np.sinc(X)

# Generate data for the zoomed portion
X_detail = np.linspace(-3, 3, 1024)
Y_detail = np.sinc(X_detail)

# plot the main figure
plt.plot(X, Y, c = 'k')  

 # location for the zoomed portion 
sub_axes = plt.axes([.6, .6, .25, .25]) 

# plot the zoomed portion
sub_axes.plot(X_detail, Y_detail, c = 'k') 

# insert the zoomed figure
# plt.setp(sub_axes)

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2016-06-12
    • 2012-12-01
    • 2015-05-09
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多