python - 从零开始绘制 Y 条,而不是在列表中较小的值处

标签 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

最佳答案

您需要将所有字典值转换为整数,因为当前代码将引发错误 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

关于python - 从零开始绘制 Y 条,而不是在列表中较小的值处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48446233/

相关文章:

python - 在Python中,我们如何将某个变量的先前值存储在另一个变量中?

python - 通过文本框交互式 matplotlib 图

python - 使用 matplotlib 绘制特征行为

PYTHON:使用条目/按钮 tkinter 错误发送电子邮件

Python 代码比相同的 C++ 代码慢得多?

python - Python 中的 CSV 文件删除括号、引号和 u

python - 来自第一维的 numpy 广播

pandas - 如何将多组列组合为 pandas 中的条形图

python - 两个对应的y轴

python - 如何删除圆内的一组网格点?