【发布时间】:2022-01-09 20:56:12
【问题描述】:
我需要创建一个包含 DNA 序列 ID 和分子量字典的散点图。许多 DNA 序列是不明确的,因此它们可以有许多可能的分子量(因此每个键有许多值)。字典看起来像这样,但许多键实际上有更多的值(为简洁起见,我删除了一些)。
{'seq_7009': [6236.9764, 6279.027699999999,
6319.051799999999, 6367.049999999999],
'seq_418': [3716.3642000000004, 3796.4124000000006],
'seq_9143_unamb': [4631.958999999999],
'seq_2888': [5219.3359, 5365.4089],
'seq_1101': [4287.7417, 4422.8254]}
我有另一个名为get_all_weights 的函数可以生成这个字典,所以我尝试调用该函数然后绘制结果图。这是我到目前为止所拥有的,基于此站点上的另一篇文章,但它不起作用:
import matplotlib.pyplot as plt
import itertools
def graph_weights(file_name):
with open (file_name) as file:
d = {} # Initialize a dictionary and then fill it with the results of the get_all_weights function
d.update(get_all_weights(file_name))
for k, v in d.items():
x = [key for (key,values) in b.items() for _ in range(len(values))]
y = [val for subl in d.values() for val in subl]
ax.plot(x, y)
plt.show()
有谁知道我如何做到这一点?该图应在 x 轴上显示序列 ID,在 y 轴上显示值,并且应该清楚地表明同一值可以多次出现。
【问题讨论】:
-
你知道你可以做
d = get_all_weights(...),对吧? -
不要使用
with,因为你从不使用file。如果您确实使用了with,请将所有后期处理都放在外面。尽快关闭文件。 -
展示你得到的和你想要的。这是图像合适的一种情况
-
我们不需要看生成代码。正确的minimal reproducible example 只需要
d = <first snippet>
标签: python dictionary matplotlib plot