如果不更改任何代码,绘制的图形将不同。在新的重击中第一次正确运行,在下一次运行中无序运行(也许它可以循环回到正确的顺序)
具体来说:
环境:MacOS Mojave 10.14.2,python3.7.1通过自制软件安装。
方法:在同一个axes
上为两组或三组数据绘制scatter
,每一组都有不同的markertype
和不同的colors
。绘制自定义图例,显示每个markertype
代表的数据集
很抱歉,我没有足够的时间准备可测试代码(目前),但这部分似乎是问题所在:
markerTypes = cycle(['o', 's', '^', 'd', 'p', 'P', '*'])
strainLegends = []
strains = list(set([idx.split('_')[0] for idx in pca2Plot.index]))
for strain in strains:
# markerType is fixed here, and shouldn't be passed on to the next python run anyway.
markerType = next(markerTypes)
# strainSamples connects directly to strain variable, then data is generated from getting strainSamples:
strainSamples = [sample for sample in samples if
sample.split('_')[0] == strain]
xData = pca2Plot.loc[strainSamples, 'PC1']
yData = pca2Plot.loc[strainSamples, 'PC2']
# See pictures below, data is correctly identified from source
# both scatter and legend instance use the same fixed markerType
ax.scatter(xData, yData, c=drawColors[strainSamples],
s=40, marker=markerType, zorder=3)
strainLegends.append(Line2D([0], [0], marker=markerType, color='k',
markersize=10,
linewidth=0, label=strain))
# print([i for i in ax.get_children() if isinstance(i, PathCollection)])
ax.legend(handles=strainLegends)
如您所见,markerType
和strain
数据与数据相关
对于bash中使用python3 my_code.py
的第一次运行,它创建了一个正确的图片:请参见圆表示a,正方形表示B
A=圆形,B=方形。参见(-3, -3.8)
周围的正方形,该数据点来自数据集B
如果我在同一个终端中再次运行代码python3 my_code.py
注A和B完全聚集在一起,不相关。
现在如图例所示:A=正方形,B=圆形。再次查看数据点(-3, -3.8)
,它来自数据集B,现在注释为A
如果我再次运行代码,可能会产生另一个结果
下面是我用来生成注释的代码:
dictColor = {ax: pd.Series(index=pca2Plot.index), }
HoverClick = interactionHoverClick(
dictColor, fig, ax)
fig.canvas.mpl_connect("motion_notify_event", HoverClick.hover)
fig.canvas.mpl_connect("button_press_event", HoverClick.click)
在HoverClick
课上,我有
def hover(self, event):
if event.inaxes != None:
ax = event.inaxes
annot = self.annotAxs[ax]
# class matplotlib.collections.PathCollection, here refere to the scatter plotting event (correct?)
drawingNum = sum(isinstance(i, PathCollection)
for i in ax.get_children())
# print([i for i in ax.get_children() if isinstance(i, PathCollection)])
plotSeq = 0
jump = []
indInd = []
indIndInstances = []
for i in range(drawingNum):
sc = ax.get_children()[i]
cont, ind = sc.contains(event)
jump.append(len(sc.get_facecolor()))
indIndInstances.append(ind['ind'])
if cont:
plotSeq = i
indInd.extend(ind['ind'])
# here plotSeq is the index of last PathCollection instance that program find my mouse hovering on a datapoint of it.
sc = ax.get_children()[plotSeq]
cont, ind = sc.contains(event)
if cont:
try:
exist = (indInd[0] in self.hovered)
except:
exist = False
if not exist:
hovered = indInd[0]
pos = sc.get_offsets()[indInd[0]]
textList = []
for num in range(plotSeq + 1):
singleJump = sum(jump[:num])
textList.extend([self.colorDict[ax].index[i + singleJump]
for i in indIndInstances[num]])
text = '\n'.join(textList)
annot.xy = pos
annot.set_text(text)
annot.set_visible(True)
self.fig.canvas.draw_idle()
else:
if annot.get_visible():
annot.set_visible(False)
self.fig.canvas.draw_idle()
# hover
注意,我为print-each实例注释了代码。这是经过测试的,因为我认为这可能是在代码的其他部分中更改的实例顺序。但结果表明,无论是正确的还是错误的情况,顺序都没有改变
有人知道发生了什么吗? 以前有人经历过吗? 如果我需要在代码末尾清理内存,我应该怎么做
由于代码不完整,很难说清楚,但是标记的顺序似乎被
cycle
迭代器弄乱了。你为什么不试试:当然,这假设
strains
和markerTypes
的长度相同,并且标记在列表中的位置与要分配它们的应变值相同我发现这个问题是由我在
strains
中进行的反复制过程引起的因此,我问的问题不是一个有效的问题。谢谢,很抱歉所有人都调查了此事
相关问题 更多 >
编程相关推荐