如何挑选一个点一个插曲和matplotlib突出它相邻的次要情节(扩展点的区域) [英] How to pick a point in a subplot and highlight it in adjacent subplots in matplotlib(extension to region of points)
问题描述
我要创建将由一些次要情节组成散点图矩阵。我从一个.txt文件中提取我的数据和创建形状(X,Y,Z,P1,P2,P3)的阵列。阵列重新present将x,y的前三列和z从原始图像,这些数据来自最后三列(P1,P2,P3)的一些其他参数坐标。因此,阵列的每一行中的参数P1,P2,P3有。在散点图同一坐标(x,Y,Z),我要显示对P2,P3的参数P1参数中的第一阶段。对于每一点我挑,我想它(X,Y,Z)从我的数组的第一个三列参数进行注释,并在加以强调相邻的插曲或颜色相同的坐标点进行修改。
I want to create a scatter plot matrix which will be composed by some subplots. I have extracted from a .txt file my data and created an array of shape (x,y,z,p1,p2,p3). The first three columns of the array represent the x,y,z coordinates from the original image that these data come from and the last three columns(p1, p2, p3) some other parameters. Consequently, in each row of the array the parameters p1, p2, p3 have the same coordinates(x,y,z).In the scatter plot, I want to visualize the p1 parameter against the p2, p3 parameters in a first stage. For every point I pick, I would like its (x,y,z) parameters from the first three columns of my array to be annotated and the point with the same coordinates in the adjacent subplot to be highlighted or its color to be modified.
在我的code,创建了两个副区和在该终端打印由拾取点获得的(P1,P2或P3)的值,同样的点在相邻副区和各值(X,Y,Z)这点的参数
In my code, two subplots are created and in the terminal are printed the (p1,p2 or p3) values that are acquired by picking a point, the respective values of the same point in the adjacent subplot and the (x,y,z) parameters of this point.
此外,当我拿起一个点在第一个插曲,在第二次要情节的变化对应点的颜色,但不是相反。这种颜色的修改是公认只有当我手动调整的身影。我怎么能为次要情节添加交互,而无需调整图,以发现任何变化?我应该做出什么样的修改,以这种互动是可行的减少散点图矩阵想在这个问题:<一个href=\"http://stackoverflow.com/questions/7941207/is-there-a-function-to-make-scatterplot-matrices-in-matplotlib\">Is有没有功能,使散点图矩阵中matplotlib?。我不是一个有经验的蟒蛇,matplotlib用户,因此任何形式的帮助将是AP preciated
Moreover, when I pick a point in the first subplot, the color of the corresponding point in the second subplot changes but not vice versa. This color modification is recognizable only if I resize manually the figure. How could I add interactivity for both subplots without having to tweak the figure in order to notice any changes? What kind of modifications should I make in order this interactivity to be feasible in a reduced scatter plot matrix like in this question "Is there a function to make scatterplot matrices in matplotlib?" . I am not an experienced python, matplotlib user, so any kind of help will be appreciated
import numpy as np
import matplotlib.pyplot as plt
import pylab as pl
def main():
#load data from file
data = np.loadtxt(r"data.txt")
plt.close("all")
x = data[:, 3]
y = data[:, 4]
y1 = data[:, 5]
fig1 = plt.figure(1)
#subplot p1 vs p2
plt.subplot(121)
subplot1, = plt.plot(x, y, 'bo', picker=3)
plt.xlabel('p1')
plt.ylabel('p2')
#subplot p1 vs p3
plt.subplot(122)
subplot2, = plt.plot(x, y1, 'bo', picker=3)
plt.xlabel('p1')
plt.ylabel('p3')
plt.subplots_adjust(left=0.1, right=0.95, wspace=0.3, hspace=0.45)
# art.getp(fig1.patch)
def onpick(event):
thisevent = event.artist
valx = thisevent.get_xdata()
valy = thisevent.get_ydata()
ind = event.ind
print 'index', ind
print 'selected point:', zip(valx[ind], valy[ind])
print 'point in the adjacent subplot', x[ind], y1[ind]
print '(x,y,z):', data[:, 0][ind], data[:, 1][ind], data[:, 2][ind]
for xcord,ycord in zip(valx[ind], valy[ind]):
plt.annotate("(x,y,z):", xy = (x[ind], y1[ind]), xycoords = ('data' ),
xytext=(x[ind] - .5, y1[ind]- .5), textcoords='data',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3"),
)
subplot2, = plt.plot(x[ind], y[ind], 'ro', picker=3)
subplot1 = plt.plot(x[ind], y[ind], 'ro', picker=3)
fig1.canvas.mpl_connect('pick_event', onpick)
plt.show()
main()
总之,信息打印在终端方面,独立的次要情节的,当我拿起一个点。但是,颜色只有在正确的次要情节的点修改,当我拿起在左副区一个点,而不是相反。此外,直到我调整图中的颜色的变化并不明显(例如,移动或调整其大小),当我选择第二点,previous任意一方残留色。
In conclusion, information are printed in the terminal, independently of the subplot, when I pick a point. But, the color is modified only in the points of the right subplot, when I pick a point in the left subplot and not vice versa. Moreover, the change of the color is not noticeable until I tweak the figure(e.g. move it or resize it) and when I choose a second point, the previous one remains colored.
有什么贡献的将是AP preciated。谢谢你在前进。
Any kind of contribution will be appreciated. Thank you in advance.
推荐答案
你已经在正确的轨道与您当前code上。你基本上只是缺少一个电话 plt.draw()
在 onpick
功能。
You're already on the right track with your current code. You're basically just missing a call to plt.draw()
in your onpick
function.
然而,在我们的意见的讨论, mpldatacursor
上来,你问做的事情,这样的一个例子。
However, in our discussion in the comments, mpldatacursor
came up, and you asked about an example of doing things that way.
目前 HighlightingDataCursor
在 mpldatacursor
设置围绕突出整个的想法 Line2D中
的艺术家,不只是它的一个特定的索引。 (这是故意位有限,因为有画在matplotlib任何艺术家任意亮点没有什么好办法,所以我一直强调的部分很小。)
The current HighlightingDataCursor
in mpldatacursor
is set up around the idea of highlighting an entire Line2D
artist, not just a particular index of it. (It's deliberately a bit limited, as there's no good way to draw an arbitrary highlight for any artist in matplotlib, so I kept the highlighting parts small.)
不过,你可以继承类似于这样的事情(假设你使用剧情
,想在每个小区的第一件事轴被使用)。我使用也说明 point_labels
,如果你想拥有所显示的每个点不同的标签:
However, you could subclass things similar to this (assumes you're using plot
and want the first thing you plot in each axes to be used). I'm also illustrating using point_labels
, in case you want to have different labels for each point shown.:
import numpy as np
import matplotlib.pyplot as plt
from mpldatacursor import HighlightingDataCursor, DataCursor
def main():
fig, axes = plt.subplots(nrows=2, ncols=2)
for ax, marker in zip(axes.flat, ['o', '^', 's', '*']):
x, y = np.random.random((2,20))
ax.plot(x, y, ls='', marker=marker)
IndexedHighlight(axes.flat, point_labels=[str(i) for i in range(20)])
plt.show()
class IndexedHighlight(HighlightingDataCursor):
def __init__(self, axes, **kwargs):
# Use the first plotted Line2D in each axes
artists = [ax.lines[0] for ax in axes]
kwargs['display'] = 'single'
HighlightingDataCursor.__init__(self, artists, **kwargs)
self.highlights = [self.create_highlight(artist) for artist in artists]
plt.setp(self.highlights, visible=False)
def update(self, event, annotation):
# Hide all other annotations
plt.setp(self.highlights, visible=False)
# Highlight everything with the same index.
artist, ind = event.artist, event.ind
for original, highlight in zip(self.artists, self.highlights):
x, y = original.get_data()
highlight.set(visible=True, xdata=x[ind], ydata=y[ind])
DataCursor.update(self, event, annotation)
main()
同样,这假设你使用剧情
而不是,比方说,分散
。这是可能的分散
来做到这一点,但你需要改变一个烦人大量的细节。 (有突出任意matplotlib艺术家没有通用的方式,所以你必须有很多非常详细的code应对每种类型的艺术家单独的。)
Again, this assumes you're using plot
and not, say, scatter
. It is possible to do this with scatter
, but you need to change an annoyingly large amount of details. (There's no general way to highlight an arbitrary matplotlib artist, so you have to have a lot of very verbose code to deal with each type of artist individually.)
希望这是有用的,在任何速度。
Hope it's useful, at any rate.
这篇关于如何挑选一个点一个插曲和matplotlib突出它相邻的次要情节(扩展点的区域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!