matlibplot - 网络字节 y 轴到人类可读
matlibplot - network bytes y axis to human readable
我正在尝试从网络设备创建一些自动生成的报告。
数据以字节为单位从传感器发送,正如网络设备所预期的那样。然而,我遇到的“问题”是,我想修改 y 轴以将结果解释为严格为兆字节 (mb) 甚至更好 kb/mb/gb,具体取决于平均流大小集的。
作为参考,如果我通过 gui(石墨)查看此特定查询会生成 Y 轴为 Mb 的渲染
虽然 matlibplot 将浮点值解释为浮点数,因此默认为科学记数法
# a small fraction sample data
# Typically handing ~8400 rows per iteration
[
[
973920.8333330154,
'2022-03-01 00:00:00'
],
[
993574,
'2022-03-02 00:00:00'
],
[
964486.5,
'2022-03-03 00:00:00'
],
[
972345.4166669846,
'2022-03-04 00:00:00'
],
[
972770.9166665077,
'2022-03-05 00:00:00'
],
[
961019.75,
'2022-03-06 00:00:00'
],
[
957663.9166665077,
'2022-03-07 00:00:00'
],
[
957588.5,
'2022-03-08 00:00:00'
],
[
959108,
'2022-03-09 00:00:00'
],
[
959551.8333334923,
'2022-03-10 00:00:00'
]
]
这是我第一次与绘图互动,所以可能是解决这个问题的切肉刀功能,但我还没有找到。
欢迎所有想法。
更新:
我还没有弄清楚 MR.T
建议的自定义主要格式化程序
我有一个在绘图之前将数据帧值更改为 MB 的原型,但需要测试均值是否适合我的目的
import pandas as pd
import numpy as np
def sizeof_fmt(num):
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f_%s" % (num, x)
num /= 1024.0
df = pd.DataFrame(
[[2097152, '2020-01-02 00:00:00'],
[2831155.2, '2020-01-02 00:00:00'],
[1077936128, '2020-01-02 00:00:00'],
[np.NaN, '2020-01-02 00:00:00'],
[np.NaN, '2020-01-02 00:00:00']])
df.plot()
operator = sizeof_fmt(df[0].mean()).split('_')[1]
print(sizeof_fmt(df[0].mean()))
print(operator)
if (operator == 'MB'):
df[[0]] = df[[0]].div(1024*1024)
print(df)
你的功能很完美,但你从未将它应用于格式化程序。您可以这样做:
from matplotlib import pyplot as plt
import matplotlib.ticker as tkr
import numpy as np
def sizeof_fmt(x, pos):
if x<0:
return ""
for x_unit in ['bytes', 'kB', 'MB', 'GB', 'TB']:
if x < 1024.0:
return "%3.1f %s" % (x, x_unit)
x /= 1024.0
#sample data
import pandas as pd
df = pd.DataFrame(
[[12097152, '2020-01-02 00:00:00'],
[31155.2, '2020-01-02 10:00:00'],
[np.NaN, '2020-01-02 12:00:00'],
[1234, '2020-01-03 04:00:00'],
[1877936128, '2020-01-03 05:10:00']])
df[1] = pd.to_datetime(df[1])
ax = df.plot(x=1, y=0)
ax.yaxis.set_major_formatter(tkr.FuncFormatter(sizeof_fmt))
plt.show()
Formatter 的优点是放大时更新的刻度仍然是正确的格式。
您也可以编写自己的 Locator method 以确保刻度始终为 1024 的倍数,但我从未研究过这一点。
我正在尝试从网络设备创建一些自动生成的报告。
数据以字节为单位从传感器发送,正如网络设备所预期的那样。然而,我遇到的“问题”是,我想修改 y 轴以将结果解释为严格为兆字节 (mb) 甚至更好 kb/mb/gb,具体取决于平均流大小集的。
作为参考,如果我通过 gui(石墨)查看此特定查询会生成 Y 轴为 Mb 的渲染
虽然 matlibplot 将浮点值解释为浮点数,因此默认为科学记数法
# a small fraction sample data
# Typically handing ~8400 rows per iteration
[
[
973920.8333330154,
'2022-03-01 00:00:00'
],
[
993574,
'2022-03-02 00:00:00'
],
[
964486.5,
'2022-03-03 00:00:00'
],
[
972345.4166669846,
'2022-03-04 00:00:00'
],
[
972770.9166665077,
'2022-03-05 00:00:00'
],
[
961019.75,
'2022-03-06 00:00:00'
],
[
957663.9166665077,
'2022-03-07 00:00:00'
],
[
957588.5,
'2022-03-08 00:00:00'
],
[
959108,
'2022-03-09 00:00:00'
],
[
959551.8333334923,
'2022-03-10 00:00:00'
]
]
这是我第一次与绘图互动,所以可能是解决这个问题的切肉刀功能,但我还没有找到。
欢迎所有想法。
更新:
我还没有弄清楚 MR.T
建议的自定义主要格式化程序我有一个在绘图之前将数据帧值更改为 MB 的原型,但需要测试均值是否适合我的目的
import pandas as pd
import numpy as np
def sizeof_fmt(num):
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f_%s" % (num, x)
num /= 1024.0
df = pd.DataFrame(
[[2097152, '2020-01-02 00:00:00'],
[2831155.2, '2020-01-02 00:00:00'],
[1077936128, '2020-01-02 00:00:00'],
[np.NaN, '2020-01-02 00:00:00'],
[np.NaN, '2020-01-02 00:00:00']])
df.plot()
operator = sizeof_fmt(df[0].mean()).split('_')[1]
print(sizeof_fmt(df[0].mean()))
print(operator)
if (operator == 'MB'):
df[[0]] = df[[0]].div(1024*1024)
print(df)
你的功能很完美,但你从未将它应用于格式化程序。您可以这样做:
from matplotlib import pyplot as plt
import matplotlib.ticker as tkr
import numpy as np
def sizeof_fmt(x, pos):
if x<0:
return ""
for x_unit in ['bytes', 'kB', 'MB', 'GB', 'TB']:
if x < 1024.0:
return "%3.1f %s" % (x, x_unit)
x /= 1024.0
#sample data
import pandas as pd
df = pd.DataFrame(
[[12097152, '2020-01-02 00:00:00'],
[31155.2, '2020-01-02 10:00:00'],
[np.NaN, '2020-01-02 12:00:00'],
[1234, '2020-01-03 04:00:00'],
[1877936128, '2020-01-03 05:10:00']])
df[1] = pd.to_datetime(df[1])
ax = df.plot(x=1, y=0)
ax.yaxis.set_major_formatter(tkr.FuncFormatter(sizeof_fmt))
plt.show()
Formatter 的优点是放大时更新的刻度仍然是正确的格式。
您也可以编写自己的 Locator method 以确保刻度始终为 1024 的倍数,但我从未研究过这一点。