如何使用Python3中的pylab去除x轴标签中的混乱/重叠?

问题描述:

我们有以下OrderedDictionary保存在d如何使用Python3中的pylab去除x轴标签中的混乱/重叠?

OrderedDict([('CAT-QuickHeal', 328), 
      ('TheHacker', 328), 
      ('K7AntiVirus', 328), 
      ('F-Prot', 328), 
      ('BitDefender', 328), 
      ('ViRobot', 328), 
      ('McAfee-GW-Edition', 328), 
      ('Jiangmin', 328), 
      ('Fortinet', 328), 
      ('SUPERAntiSpyware', 328), 
      ('VBA32', 328), 
      ('AVG', 328), 
      ('McAfee', 327), 
      ('Avast', 327), 
      ('ClamAV', 327), 
      ('Kaspersky', 327), 
      ('Comodo', 327), 
      ('F-Secure', 327), 
      ('DrWeb', 327), 
      ('Emsisoft', 327), 
      ('Microsoft', 327), 
      ('AhnLab-V3', 327), 
      ('nProtect', 326), 
      ('Malwarebytes', 326), 
      ('VIPRE', 326), 
      ('Symantec', 326), 
      ('NANO-Antivirus', 326), 
      ('Sophos', 326), 
      ('ESET-NOD32', 326), 
      ('GData', 326), 
      ('Panda', 326), 
      ('K7GW', 325), 
      ('MicroWorld-eScan', 324), 
      ('Ikarus', 324), 
      ('Qihoo-360', 320), 
      ('AegisLab', 319), 
      ('Rising', 319), 
      ('Ad-Aware', 319), 
      ('Kingsoft', 318), 
      ('CMC', 317), 
      ('Tencent', 316), 
      ('Cyren', 315), 
      ('Zoner', 315), 
      ('Zillya', 314), 
      ('Avira', 314), 
      ('AVware', 310), 
      ('Arcabit', 309), 
      ('ALYac', 301), 
      ('TrendMicro', 299), 
      ('Yandex', 298), 
      ('Baidu', 294), 
      ('TrendMicro-HouseCall', 288), 
      ('Bkav', 279), 
      ('Antiy-AVL', 265), 
      ('Not Detected', 264), 
      ('Invincea', 250), 
      ('TotalDefense', 250), 
      ('CrowdStrike', 239), 
      ('Webroot', 219), 
      ('ZoneAlarm', 216), 
      ('Endgame', 192), 
      ('Paloalto', 176), 
      ('SentinelOne', 171), 
      ('Alibaba', 45), 
      ('Baidu-International', 31), 
      ('Agnitum', 28), 
      ('ByteHero', 27), 
      ('Norman', 19), 
      ('AntiVir', 13), 
      ('Commtouch', 12), 
      ('WhiteArmor', 9), 
      ('PCTools', 7), 
      ('eSafe', 5), 
      ('VirusBuster', 2), 
      ('NOD32', 2), 
      ('eTrust-Vet', 2), 
      ('Prevx', 2), 
      ('Avast5', 2), 
      ('SymantecMobileInsight', 1), 
      ('Authentium', 1), 
      ('Sunbelt', 1)]) 

type(d)collections.OrderedDict

如下我们绘制它:

dictlist = [] 
for key, value in d.items(): 
    temp = [key,value] 
    dictlist.append(temp) 

x = []; y = [] 
for pair in dictlist: 
    x.append(pair[0]) 
    y.append(pair[1]) 
r = range(81) 
pl.xticks(r, x) 
pl.xticks(r, x, rotation=90) 
pl.plot(r,y,'*') 
pl.show() 

所得的情节

resulting plot

我们希望删除x轴标签之间的重叠。或者,有没有更好的方式来绘制这本词典?

设置图形大小,并使用tight_layout()更好的绘图。您可以使用d.keys和d.values来获取键和值。

import matplotlib.pyplot as pl 
pl.figure(num=None, figsize=(10, 6), dpi=80, facecolor='w', edgecolor='k') 
pl.bar(range(len(d.keys())), d.values()) 
pl.xticks(range(len(d.values())), d.keys(), rotation=90) 
pl.tight_layout() 
pl.show() 

将导致

enter image description here

+0

谢谢。这样可行。最后一件事:有没有一种方法可以在此图中生成小节线而不是点或蜱?例如,一条横跨x-label ='CAT-Quickheal'并在Y轴上上升到328的小节线。 – user8195347

+0

看到我最新的编辑。 – plasmon360