1、脚本代码
import matplotlib.pyplot as plt
nginx_file = ''  --填写nginx日志文件名,需在同一目录
ip = {}
#筛选nginx日志文件中的IP
with open(nginx_file) as f:
    for i in f.readlines():
        s = i.strip().split()[0]
        lengh = len(ip.keys())
        #统计每个IP的访问以字典存储
        if s in ip.keys():
            ip[s] = ip[s] + 1
        else:
            ip[s] = 1
#以IP出现的次数排序返回对象为list
ip = sorted(ip.items(), key=lambda e:e[1], reverse=True)
#取列表前十
newip = ip[0:20:1]
tu = dict(newip)
x = []
y = []
for k in tu:
    x.append(k)
    y.append(tu[k])
plt.title('ip access')
plt.xlabel('ip address')
plt.ylabel('pv')
#X 轴项的翻转角度
plt.xticks(rotation=70)
#显示每个柱状图的值
for a,b in zip(x,y):
    plt.text(a, b, '%.0f' % b, ha='center', va= 'bottom',fontsize=7)
plt.bar(x,y)
plt.legend()
plt.show()
2、效果图:


