我正在尝试绘制图形以显示授权和未授权的不同IP地址尝试的次数。我拥有的数据如下所示:
Access Type host/IP address Count
0 Authorized 206.196.21.129 23
1 Authorized 207.30.238.8 46
2 Authorized 208.62.55.75 23
3 Authorized 216.12.111.241 23
4 Authorized 63.197.98.106 23
5 Authorized 67.95.49.172 23
6 Unauthorized 207.243.167.114 23
7 Unauthorized 209.152.168.249 10
8 Unauthorized 65.166.159.14 10
9 Unauthorized 68.143.156.89 10
我该怎么做?我认为X轴将以IP地址为主要标题,而访问类型的计数为子标题。
答案 0 :(得分:2)
您可以执行类似的操作;在下面的代码中,我将“未经授权”的IP标记为红色,将“授权” IP的标记为绿色。您可以更改它。
import pandas as pd
import matplotlib.pyplot as plt
# df has data
colors = ['r' if item == "Unauthorized" else 'g' for item in df["Access Type"]]
df.plot(kind='bar', x='host/IP address', y='Count', color=colors, legend=False)
plt.show()
答案 1 :(得分:0)
在Python中,这是一种非常优雅的方式
import numpy as np
import pandas as pd
from plotnine import *
%matplotlib inline
df = pd.read_csv('~/Downloads/Untitled spreadsheet - Sheet1.csv')
ggplot(df, aes(x='Access Type', y = "Count" ,fill = 'host address'))+
geom_bar(stat="identity",position="dodge")
如果没有蛋黄素,请使用
pip install plotnine
如果您对此感兴趣,这里是另一种着色方式。
ggplot(df, aes(x='host address', y = "Count" ,fill = 'Access Type'))+
geom_bar(stat="identity",position="dodge")+
theme(axis_text_x=element_text(angle=45))
答案 2 :(得分:0)
我参加聚会有点晚了。由于已经显示了一些条形图作为解决方案,因此您对使用气泡图有何看法?
# Visualizing 4-D mix data using bubble plots
#create custom legend
red_patch = mpatches.Patch(color='red', label='Unauthorized Access', alpha=0.4)
blue_patch = mpatches.Patch(color='blue', label='Authorized Access', alpha=0.4)
#specify figure size
plt.figure(figsize=(7,7))
#specify blubble size
size = df['Count']*25
#define fill and edge colors
fill_colors = ['red' if access_type=='Unauthorized' else 'blue' for access_type in
list(df['Access Type'])]
edge_colors = ['red' if access_type=='red' else 'blue' for access_type in list(df['Access Type'])]
#create scatter plot
plt.scatter(df['host/IP address'], df['Count'], s=size,
alpha=0.4, color=fill_colors, edgecolors="black",)
#rotate axis titles, IP Adress will not fit on the axis without rotation
plt.xticks(rotation=90)
#set legend handles
plt.legend(handles=[red_patch, blue_patch])
# give y and x axis titles and plot title
plt.xlabel('Host/IP Address')
plt.ylabel('Access Type Counts')
[![enter image description here][1]][1]plt.title('Access Type Counts by Host/IP Address',y=1.05)
您只是在这里提供示例数据,对吗?您是否可以对一个IP地址进行未经授权的访问?确保您的情节在这种情况下是可靠的。
本文提供了不错的ideas关于如何可视化分类变量的信息