0
タプル 'new'内のアイテムを含む単一のnetworkXグラフを描画します。 私のグラフノードは "AXIN"のようなラベルを持ち、色は緑か黄です。Python Networkxグラフ描画 - タプル内のラベル/値
「未登録」の場合、ノードを緑色にします。 タプル「レポート」内のアイテム[2]がこのノードを黄色にします。
> new = (('AXIN', 37, 'reported'), ('LGR', 30, 'reported'), ('NKD',
> 24, 'reported'), ('TNFRSF', 23, 'UNREPORTED'), ('CCND', 19,
> 'reported'), ('APCDD', 18, 'reported'), ('TR', 16, 'UNREPORTED'),
> ('TOX', 15, 'UNREPORTED'), ('LEF', 15, 'reported'), ('MME', 13,
> 'reported'))
>
> X, Y, _ = zip(*new) import seaborn as sns sns.set() import
> matplotlib.pyplot as plt %matplotlib inline plt.figure(figsize = (20,
> 10)) mytitle = "Most common genes coexpressed with {gene1}, {gene2},
> {gene3}, {gene4}".format(gene1="axin2", gene2="lef", gene3="nkd1",
> gene4="lgr5") plt.title(mytitle, fontsize=40) plt.ylabel('Number of
> same gene encounters across studies', fontsize=20) ax =
> plt.bar(range(len(X)), Y, 0.6, align='center', tick_label = X,
> color="red") ax = plt.xticks(rotation=90) new = tuple(new)
>
> import networkx as nx children = sorted(new, key=lambda x: x[1])
> parent = children.pop()[0]
>
> G = nx.Graph() for child, weight, _ in children: G.add_edge(parent,
> child, weight=weight) width = list(nx.get_edge_attributes(G,
> 'weight').values()) colors = [] for i in new:
> if i[2] == 'UNREPORTED':
> colors.append('green')
> elif i[2] == 'reported':
> colors.append('yellow') print(colors) plt.savefig("plt.gene-expression.pdf") plt.figure(figsize = (20, 10))
> mytitle = "Most common genes coexpressed with {gene1}, {gene2},
> {gene3}, {gene4}".format(gene1="axin2", gene2="lef", gene3="nkd1",
> gene4="lgr5") plt.title(mytitle, fontsize=40)
>
> nx.draw_networkx(G, font_size=10, node_size=2000, alpha=0.6,
> node_color=colors) plt.savefig("gene-expression-graph.pdf")
このコードでは、黄色のノードと緑のノードを持つ2つの異なるグラフが表示されます。
ありがとう@h_s。残念ながら、どのノードにも緑の色が割り当てられます。特に「未登録」ノードのみに限定されません。 私は、TNFRSF、TOX&TRが緑色のノードであると予想しました。しかし、私はAPCDDを緑色のノードにしていますが、APCDDが報告されているので間違っています。 –
@ J.A最小限の作業例でより完全な答えを提供しました。お役に立てれば! – harryscholes
ありがとう、今すぐ –