Can';t压缩Python元组以获取绘图轴值

Can';t压缩Python元组以获取绘图轴值,python,python-3.x,tuples,python-3.5,Python,Python 3.x,Tuples,Python 3.5,我想解压绘图的X,Y值的元组,如果新元组中的项由3个组件(“AXIN',37,'reported')组成,而不是2个(AXIN,37),则会出现错误 错误表示要解包的值太多 new = (('AXIN', 37, 'reported'), ('LGR', 30, 'reported'), ('NK', 24, 'reported'), ('TN', 23, 'reported'), ('CC', 19, 'reported'), ('APC', 18, 'reported'), ('

我想解压绘图的X,Y值的元组,如果新元组中的项由3个组件(“AXIN',37,'reported')组成,而不是2个(AXIN,37),则会出现错误

错误表示要解包的值太多

new = (('AXIN', 37, 'reported'),
 ('LGR', 30, 'reported'),
 ('NK', 24, 'reported'),
 ('TN', 23, 'reported'),
 ('CC', 19, 'reported'),
 ('APC', 18, 'reported'),
 ('TRD@', 16, 'reported'),
 ('TOX', 15, 'UNREPORTED'), 
 ('LEF', 15, 'reported'),
 ('MME', 13, 'reported'),
 ('NOTUM', 13, 'reported'),
 ('PLCB', 13, 'UNREPORTED'), 
 ('GN', 11,  'UNREPORTED'),
 ('LOX', 10,  'UNREPORTED'),
 ('LOX', 10, 'reported'),
 ('CRND', 10, 'reported'),
 ('LRP', 9, 'reported'),
 ('BMP', 9, 'reported'),
 ('VSNL', 8,  'UNREPORTED'),
 ('LOC', 8, 'reported'),
 ('ZNRF', 8, 'reported'),
 ('KRT', 8,  'UNREPORTED'),
 ('CTNN', 8, '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="green") 
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')
nx.draw_networkx(G, font_size=10, node_size=2000, alpha=0.6, node_color=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)  #width=width is very fat lines
plt.savefig("gene-expression-graph.pdf")

ValueError:要解包的值太多(预期为2)

没错,要解包的值太多了。您只给出了两个要解包的变量,但每个元组有三个值。尝试给它另一个变量:

X,Y,Notes=zip(*新)
现在它被正确地打开了。如果不想使用第三个组件,可以给它一个从未使用过的变量名。或者使用
,按照Python中的惯例,它的意思是“无所谓”或“不在乎”:

X,Y,\=zip(*新)
我对这一约定的唯一保留是,在交互式使用中(例如IPython、Jupyter Notebook或stock Python interactive REPL),
\uu
也表示“最后产生的值”。这有时与“不在乎”的解释相冲突

顺便说一句,您还需要在代码中使用相同的技巧,这与“要解包的项目数量错误”的问题相同:

儿童,体重,儿童:
...
如果进行了这些更改,请弹出图表:


非常感谢。为什么它分别显示绿色和红色节点,而不是一起显示在下面的同一个1大图中?因为您的
plt.figure(…)
调用相对于
nx.draw\u networkx(…)
调用的位置。如果要在单个图像中组合这些图形,请在任何
nx.draw\u networkx(…)
图形之前放置一个(仅一个)
plt.figure(…)
。然而,为了更好的图形布局,您只需要绘制一个图形…这意味着更广泛地重新组织代码。