从字典中绘制直方图时出错

2024-09-29 23:16:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典,里面有7191个键,这些值表示每个键的频率。在

degree_distri = {'F2': 102, 'EGFR': 23, 'C1R': 20,...} 

为了绘制直方图,我做了:

^{pr2}$

但我收到了一条错误信息: unsupported operand type(s) for -: 'str' and 'float'

我应该不使用上面的代码来绘制柱状图吗?如果没有,有什么建议?为什么会导致错误呢?在

谢谢你!在


Tags: for字典type绘制直方图频率f2operand
2条回答

^{}作为强制性参数,两个标量序列:条形左侧的x坐标和钢筋的高度。因此,您应该使用^{{cd2>}获取所需的参数,然后使用^{}设置所需的记号:

import matplotlib.pyplot as plt

degree_distri = {'F2': 102, 'EGFR': 23, 'C1R': 20}
keys, values = degree_distri.keys(), degree_distri.values()
plt.bar(range(len(values)), values, color='r')
plt.xticks(range(len(values)), keys)
plt.show()

enter image description here

看看这个例子heredocumentation你必须 以不同的格式提供数据。在

import matplotlib.pyplot as plt
import numpy as np

degree_distri = {'F2': 102, 'EGFR': 23, 'C1R': 20}

fig, ax = plt.subplots()
indices = np.arange(len(degree_distri))
width = 0.6

ax.bar(indices, degree_distri.values(), width)

ax.set_xticks(indices)
ax.set_xticklabels(degree_distri.keys())

{{0}的数组{。然后提供值。必须将dict中的keys设置为轴标签,并且要将轴标签放置在正确的位置,必须使用条的x位置调用set_xticks。在

相关问题 更多 >

    热门问题