我有一个counter()
元素,我想将它表示为某种图。
每个元素都是一个字符串-值对,并且字符串可能很长,因此为了便于阅读,我选择使用barh
图。 我怎么才能把这一切写得更好呢? 我可以做一些类似于垂直虚线的东西,自动定位在每个条的末尾吗? 我怎样才能将图例添加到两个轴上? 我只改变了颜色。 更好地表示图形的建议被很好地接受。
plt.figure(figsize=(10, 10))
plt.barh(list(count_single_obf.keys()), list(count_single_obf.values()), color = "lightgreen")
count_single_obf
printed返回以下内容:
Counter({'EncodeArithmetic': 49,
'EncodeLiterals': 98,
'Flatten': 71,
'InitEntropy': 29,
'InitImplicitFlow': 3,
'InitOpaque': 100,
'RandomFuns': 82,
'Split': 99,
'base': 7})
此外,在打印图形之前,它会打印这一行:
; 为什么? 如何避免? 我用的是朱派特笔记本。
根据你的数据改编。 我想当你说传奇的时候,你更多的是指x和y标签。 我选择了垂直地表示条,这样就可以画出水平线。 这里的水平线显示类别的平均值作为阈值。
下面是代码片段:
import matplotlib.pyplot as plt
import numpy as np
example_dict = {'EncodeArithmetic': 49,
'EncodeLiterals': 98,
'Flatten': 71,
'InitEntropy': 29,
'InitImplicitFlow': 3,
'InitOpaque': 100,
'RandomFuns': 82,
'Split': 99,
'base': 7}
plt.figure(figsize=(8,6))
np.asarray(list(example_dict.values())).mean()
plt.bar(list(example_dict.keys()), list(example_dict.values()), color="lightgreen")
plt.axhline(np.asarray(list(example_dict.values())).mean(), color="gray") # Horizontal line adding the threshold
plt.xlabel("Frequency") # x label
plt.ylabel("Categories") # y label
plt.show()