序列项 0:预期的 str 实例,找到 numpy.int64

Sequence item 0: expected str instance, numpy.int64 found

当我尝试绘制 p 值时,我总是遇到这个问题。我不明白序列项 0 是什么。我发现了几个类似的问题,但我仍然不明白是什么导致了我下面的代码中的这个问题,也不知道如何解决它。

from statannotations.Annotator import Annotator

cluster_0_wmd = Hub_all_data.loc[(Hub_all_data.Module_ID == 0), "Within_module_degree"].values
cluster_1_wmd = Hub_all_data.loc[(Hub_all_data.Module_ID == 1), "Within_module_degree"].values
cluster_2_wmd = Hub_all_data.loc[(Hub_all_data.Module_ID == 2), "Within_module_degree"].values


with sns.plotting_context('notebook', font_scale=1.4):
# Plot with seaborn
sns.violinplot(**plotting_parameters)

stat_results = [mannwhitneyu(cluster_0_wmd, cluster_1_wmd, alternative="two-sided"),
            mannwhitneyu(cluster_0_wmd, cluster_2_wmd, alternative="two-sided"),
            mannwhitneyu(cluster_1_wmd, cluster_2_wmd, alternative="two-sided")]

pvalues = [result.pvalue for result in stat_results]

xval = [0,1,2]

plotting_parameters = {
'data':    Hub_all_data,
'x':       'Module_ID',
'y':       'Within_module_degree',
'palette': my_col}

pairs = [('cluster_0_wmd', 'cluster_1_wmd'),
     ('cluster_0_wmd', 'cluster_2_wmd'),
     ('cluster_1_wmd', 'cluster_2_wmd')]

pairs2 = [(0,1), (0,2), (1,2)]


formatted_pvalues = [f"p={p:.2e}" for p in pvalues]

annotator = Annotator(ax, pairs2, **plotting_parameters)
annotator.set_custom_annotations(formatted_pvalues)
annotator.annotate()


plt.show()

我在 annotator.annotate() 行收到错误。这是错误行:

runcell(27, '/Users/albitcabanmurillo/N5_nwxwryan.py')
p-value annotation legend:
      ns: p <= 1.00e+00
       *: 1.00e-02 < p <= 5.00e-02
      **: 1.00e-03 < p <= 1.00e-02
     ***: 1.00e-04 < p <= 1.00e-03
    ****: p <= 1.00e-04

    Traceback (most recent call last):

  File "/Users/albitcabanmurillo/N5_nwxwryan.py", line 421, in <module>
    annotator.annotate()

  File "/Users/albitcabanmurillo/opt/anaconda3/envs/caiman2/lib/python3.7/site-packages/statannotations/Annotator.py", line 222, in annotate
    orig_value_lim=orig_value_lim)

  File "/Users/albitcabanmurillo/opt/anaconda3/envs/caiman2/lib/python3.7/site-packages/statannotations/Annotator.py", line 506, in _annotate_pair
    annotation.print_labels_and_content()

  File "/Users/albitcabanmurillo/opt/anaconda3/envs/caiman2/lib/python3.7/site-packages/statannotations/Annotation.py", line 43, in print_labels_and_content
    for struct in self.structs])

TypeError: sequence item 0: expected str instance, numpy.int64 found

这里有一些代码可以尝试使用一些虚拟数据重现您的问题。 statannotations 似乎不喜欢 'Module_ID' 中的数值。您可以尝试将它们更改为字符串(也将它们用于 pairs2)。根据您更改它们的时间,您可能还需要将数值更改为 Hub_all_data.loc[(Hub_all_data.Module_ID == "0"), ...].

中的字符串

请注意,在您的示例代码中,您在赋值之前使用了 plotting_parameters(我假设您只想将赋值移动到调用 sns.violinplot 之前)。该代码还使用未知的 ax 作为 Annotator(...) 的第一个参数;这里我假设 axsns.violinplot.

的 return 值
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from statannotations.Annotator import Annotator

# first create some dummy data for testing
Hub_all_data = pd.DataFrame({'Module_ID': np.repeat(np.arange(3), 100),
                             'Within_module_degree': np.random.randn(300).cumsum()})
# change Module_ID from numeric to string
Hub_all_data['Module_ID'] = Hub_all_data['Module_ID'].astype(str)

plotting_parameters = {'data': Hub_all_data,
                       'x': 'Module_ID',
                       'y': 'Within_module_degree'}
with sns.plotting_context('notebook', font_scale=1.4):
    ax = sns.violinplot(**plotting_parameters)

# also change these values to strings
pairs2 = [("0", "1"), ("0", "2"), ("1", "2")]

# just use some dummy data, as we don't have the original data nor the functions
pvalues = [0.001, 0.002, 0.03]
formatted_pvalues = [f"p={p:.2e}" for p in pvalues]

annotator = Annotator(ax, pairs2, **plotting_parameters)
annotator.set_custom_annotations(formatted_pvalues)
annotator.annotate()

sns.despine()
plt.tight_layout() # make all labels fit nicely
plt.show()