条形图与散景图中的X轴刻度线和第一个条形截止线不对齐

时间:2020-08-13 11:42:29

标签: pandas jupyter-notebook bar-chart bokeh

我浏览了根据我的问题标题自动填充的帖子,但找不到与我的奇数结果完全一致的内容。

我正在获取数据包捕获的CSV文件并收集协议,分别将每个协议的每个协议的长度相加,然后除以协议总数,得出每个协议的平均数据包大小。

使用显示命令,我可以确认列协议和字节大小相同(有18个协议和18个字节大小的平均值)。除了可能被视为一个问题的两件事之外,我还能够确认该图形是否正确绘制。

  1. “ DNS”的第一个小节开始得很早,截止时间是一半
  2. 条形图未沿x轴刻度线对齐

我试图在他们的文档中使用简单的Bokeh水果示例来复制问题,但该图正常绘制,但这只是用x和y创建一个虚拟数组。

typename

这里是代码,但请记住,由于IP地址未附加CSV文件,因此我必须避免共享该文件,但是如果有人看到此建议,将不胜感激。

此外,这是Off-Center Plot的结果。

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

1 个答案:

答案 0 :(得分:0)

首先,当您提供依赖于某些数据的代码时,也必须提供该数据。不是作为temp.head()的图片,而是可以复制的东西。理想情况下,只需在代码本身中包含一些玩具数据即可。

关于您的问题-请不要在range(len(...))中使用p.vbar。只需提供cds.data['Protocol']作为第一个参数即可。