从零开始绘制Y条,而不是列表的较小值

时间:2018-01-25 15:18:42

标签: python matplotlib

是否有任何关于如何正确绘制它以零开始Y栏的建议?

使用这本词典:

D = {
    'GCF_000416945.1': '450',
    'GCF_001294115.1': '451',
    'GCF_000245395.1': '416',
    'GCF_002318825.1': '451',
    'GCF_001401215.1': '450',
    'GCF_000935735.1': '450'
}

和这些行:

plt.bar(range(len(D)), D.values(), align='center')  # python 2.x
plt.xticks(range(len(D)), D.keys()) 

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要将所有字典值转换为整数,因为将使用当前代码引发错误TypeError: unsupported operand type(s) for +: 'int' and 'str'。当纠正后一个错误时,代码在Python2中实现了正确的结果:

import matplotlib.pyplot as plt
D = {
'GCF_000416945.1': '450',
'GCF_001294115.1': '451',
'GCF_000245395.1': '416',
'GCF_002318825.1': '451',
'GCF_001401215.1': '450',
'GCF_000935735.1': '450'
}
plt.bar(range(len(D)), map(int, D.values()), align='center')
plt.xticks(range(len(D)), D.keys())

enter image description here

答案 1 :(得分:0)

以下行应该可以解决问题:

plt.ylim(ymin=0)