Skip to main content
 首页 » 编程设计

python之如何在 Python3 中使用 pylab 去除 x 轴标签中的困惑/重叠

2025年02月15日8shanyou

我们在 d 中保存了以下 OrderedDictionary:

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() 

结果图

我们希望移除 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() 

将导致