Bokeh y 轴以位为单位滴答作响,并从 SI 幅度角度进行缩放

Bokeh y axis ticks in bits and have it scale from an SI magnitude perspective

这里的第一个问题,所以意识到我的方法可能不是很准确,但祈祷吧!

我希望从 pandas DataFrame 的 Bokeh Y 轴(相对于 x 轴上的时间)每秒绘制位,虽然我可以使用 PrintfTickFormatter 手动调整我的刻度,但它们不会不要按照我实施它的方式进行扩展:

p.yaxis[0].formatter = PrintfTickFormatter(format="%3f M")

我的原始数据以字节为单位,所以我在我的 DataFrame 中将其转换为位图(在计算当前字节和前一个字节之间的差异并转换为位之后,然后在本例中除以 600 以获得每个-second 值)但通常我的数据是兆位或千兆位所以我真的不想以位为单位进行绘图,因为它变得难以理解而且我真的不想使用 PrintfTickFormatter 如上所述手动调整因为我必须将 CumulbitsPerSecond 除以绘图之前的相关单位(除非我能以某种方式缩放和解释此格式化程序中的 SI 单位或根据绘制的值实现某种循环的 y 轴)。

例如,这是一个 DataFrame 示例:

                            CumulativeBytes  CumulBytesDiff  CumulbitsDiff  CumulbitsPerSecond
DateTime
2020-12-05 09:40:03.175453          1000000             NaN            NaN                 NaN
2020-12-05 09:50:03.175453          3000000       2000000.0     16000000.0        26666.666667
2020-12-05 10:00:03.175453          4000000       1000000.0      8000000.0        13333.333333
2020-12-05 10:10:03.175453          7000000       3000000.0     24000000.0        40000.000000
2020-12-05 10:20:03.175453          8000000       1000000.0      8000000.0        13333.333333
2020-12-05 10:30:03.175453          9200000       1200000.0      9600000.0        16000.000000
2020-12-05 10:40:03.175453         10600000       1400000.0     11200000.0        18666.666667

我正在寻找的正是 NumeralTickFormatter Byte y 刻度轴调节器的作用:

p.yaxis[0].formatter = NumeralTickFormatter(format="0.0b")

...但是对于位。也许 FuncTickFormatter 可能会有所帮助,但我无法理解如何在 python3.6 Bokeh 2.2.3.

中实现它

如果命名为 df,则绘制上述 DataFrame 的示例代码如下:

from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, NumeralTickFormatter, PrintfTickFormatter

output_file('Whosebugq.html')

p = figure(x_axis_type="datetime")

p.line(x='DateTime', y='CumulbitsPerSecond', color='blue', source=df)

# Only allows me to reference bytes when I need to plot bits
#p.yaxis.formatter = NumeralTickFormatter(format="0.0b")

# Requires manual adjustment of bits to Kilobits in the instance with the dataframe above
#p.yaxis[0].formatter = PrintfTickFormatter(format="%3f Kbps")

p.xaxis.axis_label = 'Date Time'
p.yaxis.axis_label = 'bits per second'

show(p)

谁能指点一下?也很高兴提供 DataFrame 代码,但希望这就足够了。

输出图如下,在这种情况下,我希望 Y 轴刻度显示为 Kb 或 Kbps,如果位值在兆位区域,则显示为 Mb 或 Mbps 等。

谢谢!

据我了解,您正在寻找这样的东西:

p.yaxis.formatter = FuncTickFormatter(code='''if (tick < 1000){
                                                  return (tick) + ' ' + 'BpS'
                                              }
                                              else if (tick < 1000000){
                                                  return (tick/1000) + ' ' + 'KiBpS'
                                              }
                                              else{
                                                  return (tick/1000000) + ' ' + 'MBpS'
                                                }'''
                                     )

这是您问题的 JavaScript 解决方案。

最小示例

import pandas as pd

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource, FuncTickFormatter
output_notebook()

df = pd.DataFrame({'BitsPerSeconde': [26, 13333, 40000, 1333300, 16666]})
p = figure()

p.line(x='index', y='BitsPerSeconde', color='blue', source=df)
p.yaxis.formatter = FuncTickFormatter(code='''if (tick < 1000){return (tick) + ' ' + 'BpS'}
                                              else if (tick < 1000000){
                                                  return (tick/1000) + ' ' + 'KiBpS'
                                                  }
                                              else{
                                                  return (tick/1000000) + ' ' + 'MBpS'
                                                }'''
                                     )
p.yaxis.axis_label = 'bits per second'
show(p)

这是输出。