pandas - 未与 X 轴刻度对齐的条形和散景图中的第一个条形截止
问题描述
我浏览了根据我的问题标题自动填充的帖子,但找不到与我的古怪结果完全一致的任何内容。
我正在输入数据包捕获的 CSV 文件并收集协议,分别对每个协议的每个协议的长度求和,然后除以协议总数以获得每个协议的平均数据包大小。
使用我的显示命令,我能够确认协议列和字节列具有相同的大小(有 18 个协议和 18 个字节大小的平均值)。除了可能被视为一个问题的两件事之外,我还能够确认该图表是否正确绘制。
- “DNS”的第一根柱子开始得早,半截断
- 条形图未在 x 轴刻度上对齐
我试图在他们的文档中使用简单的散景水果示例来复制这个问题,但它绘制正常,但这只是创建了一个带有 x 和 y 的虚拟数组。
这是代码,但请记住,未附加 CSV 文件,并且由于 IP 地址的原因,我不得不避免分享它,但如果有人看到了任何建议或建议,我们将不胜感激。
此外,这是temp.head()的结果。
# Here is the myriad of declarations that helps this program work
import pandas as pd
import math as m
import ipympl
import ipywidgets
import numpy as np
# Bokeh libraries and modules (I am aware not all of these are required, just
# occasionally trying a few fun features, though let me know if this could be
# the problem)
from bokeh.io import show, reset_output, output_notebook, export_png
from bokeh.plotting import figure, output_file
from bokeh.models import Range1d, FactorRange, ColumnDataSource, LabelSet, HoverTool
from bokeh.models import ColorBar, LogColorMapper, LogTicker
from bokeh.layouts import gridplot, row, column
from bokeh.transform import factor_cmap, linear_cmap
from bokeh.models.annotations import Label
from bokeh.models.tools import HoverTool
from bokeh.palettes import Spectral6, Category20, viridis, turbo, linear_palette
# Set up plots to stay inside the notebook
# Remove this when you want to bring up a separate window display
output_notebook()
# Set up Bokeh visualtization toolset
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
data_r = pd.read_csv(r'C:\Users\xxx\Desktop\xxx.csv')
data_s = data_r.groupby('Protocol').Length.sum()
data_p = data_r.groupby('Protocol').Source.count()
data_a_p = data_s / data_p
data_a_p_df = data_a_p.to_frame()
temp = data_a_p_df.reset_index()
temp.columns = ['Protocol', 'bytes']
# Setting up the ColumnDataSource
cds = ColumnDataSource(temp)
p = figure(x_range=cds.data['Protocol'],
plot_height=300,
plot_width=800,
title="Average Packet Size by Protocol",
y_axis_label='Size in Bytes',
tools=TOOLS)
p.vbar(range(len(cds.data['Protocol'])),
width=.8,
top=cds.data['bytes'],
line_color='black',
fill_color=turbo(len(cds.data['Protocol'])),
fill_alpha=.5)
# To help the labels fit nicely, rotate the x-axis labels 45 degrees
p.xaxis.major_label_orientation = 45
# Display the graph
show(p)
display (len(cds.data['Protocol'])) # Results in 18
display (len(cds.data['bytes'])) # Results in 18
编辑:为了制作一个显示错误的功能示例,这里是使用虚拟数据框的更新代码:
# Here is the myriad of declarations that helps this program work
# Note I added prettytable to make the dummy csv file
import pandas as pd
import math as m
import ipympl
import ipywidgets
import numpy as np
# Bokeh libraries and modules (I am aware not all of these are required, just
# occasionally trying a few fun features, though let me know if this could be
# the problem)
from bokeh.io import show, reset_output, output_notebook, export_png
from bokeh.plotting import figure, output_file
from bokeh.models import Range1d, FactorRange, ColumnDataSource, LabelSet, HoverTool
from bokeh.models import ColorBar, LogColorMapper, LogTicker
from bokeh.layouts import gridplot, row, column
from bokeh.transform import factor_cmap, linear_cmap
from bokeh.models.annotations import Label
from bokeh.models.tools import HoverTool
from bokeh.palettes import Spectral6, Category20, viridis, turbo, linear_palette
# Set up plots to stay inside the notebook
# Remove this when you want to bring up a separate window display
output_notebook()
# Set up Bokeh visualtization toolset
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
# For this example I created a dummy dataframe for the data
data_r = pd.DataFrame({
'Protocol': ['DNS','DNS', 'TCP', 'ICMPv6', 'TCP', 'TCP',
'HTTP', 'HTTP', 'TCP', 'TCP', 'TCP', 'TCP',
'TCP', 'ICMPv6', 'TCP', 'TCP', 'TCP',
'ICMPv6', 'ICMPv6', 'AJP13', 'AJP13'],
'Length': [96, 154, 66, 110, 171, 171, 208, 209, 56, 56,
56, 56, 66, 110, 54, 55, 56, 110, 110, 171, 171]
})
data_s = data_r.groupby('Protocol').Length.sum()
data_p = data_r.groupby('Protocol').Protocol.count()
data_a_p = data_s / data_p
data_a_p_df = data_a_p.to_frame()
temp = data_a_p_df.reset_index()
temp.columns = ['Protocol', 'bytes']
# Setting up the ColumnDataSource
cds = ColumnDataSource(temp)
p = figure(x_range=cds.data['Protocol'],
plot_height=300,
plot_width=800,
title="Average Packet Size by Protocol",
y_axis_label='Size in Bytes',
tools=TOOLS)
p.vbar(range(len(cds.data['Protocol'])),
width=.8,
top=cds.data['bytes'],
line_color='black',
fill_color=turbo(len(cds.data['Protocol'])),
fill_alpha=.5)
# To help the labels fit nicely, rotate the x-axis labels 45 degrees
p.xaxis.major_label_orientation = 45
# Display the graph
show(p)
解决方案
首先,当您提供依赖于某些数据的代码时,请务必同时提供该数据。不是作为图片temp.head()
,而是作为可以复制的东西。理想情况下,只需在代码本身中包含一些玩具数据。
至于你的问题 - 只是不要使用range(len(...))
in p.vbar
。只需提供cds.data['Protocol']
作为第一个参数。
推荐阅读
- visual-c++ - editor able to find not included file c++
- python - Index of minimum values in a set
- go - Golang support to VSTS Git
- android - 使用 camera2 api 捕获图像
- python - Pandas - 将 JSON 中的数值设置为具有 4 位精度的浮点数
- php - 将文本文件分解为数组取决于逗号
- svn - 已检查 SVN 存储库的文件夹未显示绿色、红色勾号图标
- raspberry-pi - GPS数据解析
- azure - 如何从 ASP.NET MVC5 从 Azure BLOB 存储下载文件
- android - 在 android api 17 以下有 looper.quitSafely() 的替代方案吗?