如何在 x 轴上绘制具有 2 个变量并在 y 轴上计数的图形? [英] How to plot a graph with 2 variables on the x axis and count on the y axis?

查看:38
本文介绍了如何在 x 轴上绘制具有 2 个变量并在 y 轴上计数的图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制图形以显示授权和未授权的不同IP地址尝试的次数.我拥有的数据如下所示:

I'm trying to plot a graph to show the number authorized and unauthorized distinct IP address attempts. The data I have looks something like this:

    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地址为主要标题,而访问类型的计数为子标题.

How do i go about doing it? I am thinking that the X-axis will have the IP addresses as the main header and the count of the access types as the sub header.

推荐答案

你可以这样做;在下面的代码中,我将未经授权"的IP标记为红色,将授权" IP的标记为绿色.你可以改变它.

You can do something like that; In the following code I've colored "Unauthorized" IPs as RED and the "Authorized" IPs as GREEN. You can change that.

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

产生类似这样的东西:

which produce something like that:

这篇关于如何在 x 轴上绘制具有 2 个变量并在 y 轴上计数的图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆