如何缩放图像的一部分并插入matplotlib中的相同图中 [英] How to zoomed a portion of image and insert in the same plot in matplotlib

查看:101
本文介绍了如何缩放图像的一部分并插入matplotlib中的相同图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to zoom a portion of data/image and plot it inside the same figure. It looks something like this figure.

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

Is it possible to insert a portion of zoomed image inside the same plot. I think it is possible to draw another figure with subplot but it draws two different figures. I also read to add patch to insert rectangle/circle but not sure if it is useful to insert a portion of image into the figure. I basically load data from the text file and plot it using a simple plot commands shown below.

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

I found one related example from matplotlib image gallery here but not sure how it works. Your help is much appreciated.

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()


推荐答案

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

Playing with runnable code is one of the fastest ways to learn Python.

所以让我们从来自matplotlib示例库的代码开始。

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

Given the comments in the code, it appears the code is broken up into 4 main stanzas. The first stanza generates some data, the second stanza generates the main plot, the third and fourth stanzas create the inset axes.

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

We know how to generate data and plot the main plot, so let's focus on the third stanza:

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

将示例代码复制到新文件中,称为 test.py

Copy the example code into a new file, called, say, test.py.

如果我们更改会发生什么。 65 .3

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

运行脚本:

python test.py

您会发现概率插图向左移动。
所以函数控制插入的位置。
如果您使用数字进行更多游戏,您会发现(.35,.6)是插图左下角的
位置,(。2,.2)是插图的宽度和
高度。数字从0到1,而(0,0)位于图的左下角

You'll find the "Probability" inset moved to the left. So the axes function controls the placement of the inset. If you play some more with the numbers you'll figure out that (.35, .6) is the location of the lower left corner of the inset, and (.2, .2) is the width and height of the inset. The numbers go from 0 to 1 and (0,0) is the located at the lower left corner of the figure.

好的,现在我们是烹饪。到下一行我们有:

Okay, now we're cooking. On to the next line we have:

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

您可能会将此识别为用于绘制直方图的matplotlib命令,但是如果没有
,将数字400更改为10,则会产生一个带有
大块的图像直方图,所以再次通过玩数字,你很快就会发现
这条线与插图内的图像有关。

You might recognize this as the matplotlib command for drawing a histogram, but if not, changing the number 400 to, say, 10, will produce an image with a much chunkier histogram, so again by playing with the numbers you'll soon figure out that this line has something to do with the image inside the inset.

你'我想在这里打电话给 semilogx(数据[3:8,1],数据[3:8,2])

You'll want to call semilogx(data[3:8,1],data[3:8,2]) here.

标题('概率')
显然会在插图上方生成文本。

The line title('Probability') obviously generates the text above the inset.

最后我们来到 setp(a,xticks = [],yticks = [])。没有数字可以玩,
所以如果我们只是通过在行的开头放置一个来注释整行,会发生什么:

Finally we come to setp(a, xticks=[], yticks=[]). There are no numbers to play with, so what happens if we just comment out the whole line by placing a # at the beginning of the line:

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

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

Rerun the script. Oh! now there are lots of tick marks and tick labels on the inset axes. Fine. So now we know that setp(a, xticks=[], yticks=[]) removes the tick marks and labels from the axes a.

现在,从理论上讲,您有足够的信息将此代码应用于您的问题。
但还有一个潜在的障碍:matplotlib示例使用
来自pylab import *
而你使用将matplotlib.pyplot导入为plt

Now, in theory you have enough information to apply this code to your problem. But there is one more potential stumbling block: The matplotlib example uses from pylab import * whereas you use import matplotlib.pyplot as plt.

matplotlib常见问题解答导入matplotlib.pyplot为plt
是推荐的编写脚本时使用matplotlib的方法,而来自pylab import * 的
用于交互式会话。所以你正在以正确的方式做到这一点,(虽然我建议使用 import numpy as np 而不是来自numpy import * 太了)。

The matplotlib FAQ says import matplotlib.pyplot as plt is the recommended way to use matplotlib when writing scripts, while from pylab import * is for use in interactive sessions. So you are doing it the right way, (though I would recommend using import numpy as np instead of from numpy import * too).

那么我们如何将matplotlib示例转换为使用导入matplotlib.pyplot作为plt

So how do we convert the matplotlib example to run with import matplotlib.pyplot as plt?

进行转换需要一些matplotlib的经验。一般来说,你只需要
plt。
> setp ,但有时
函数来自numpy,有时调用应该来自一个轴
对象,而不是来自模块 plt 。需要经验才能知道所有这些
函数的来源。使用matplotlib搜索函数名称可以提供帮助。
阅读示例代码可以构建体验,但没有简单的快捷方式。

Doing the conversion takes some experience with matplotlib. Generally, you just add plt. in front of bare names like axes and setp, but sometimes the function come from numpy, and sometimes the call should come from an axes object, not from the module plt. It takes experience to know where all these functions come from. Googling the names of functions along with "matplotlib" can help. Reading example code can builds experience, but there is no easy shortcut.

因此,转换后的代码变为

So, the converted code becomes

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

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

And you could use it in your code like this:

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()

这篇关于如何缩放图像的一部分并插入matplotlib中的相同图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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