gpt4 book ai didi

Python:如何为单个跟踪添加辅助 x 轴?

转载 作者:行者123 更新时间:2023-12-04 08:55:03 25 4
gpt4 key购买 nike

我有一个 DataFrame(参见下面的“测试数据”部分),我想添加一个辅助 x 轴(在顶部)。但是这个轴必须是从 0 到 38.24(ms)。这是“时间”列中所有值的总和。它表示执行 4 个推理所花费的总时间。
到目前为止,我已经尝试过 'twinx()' 没有成功。
我怎样才能做到这一点?有可能还是我缺乏信息?
测试数据:

raw_data = {'Time': [21.9235, 4.17876, 4.02168, 3.81504, 4.2972],
'TPU': [33.3, 33.3, 33.3, 33.3, 33.3],
'CPU': [32, 32, 32, 32, 32],
'MemUsed': [435.92, 435.90, 436.02, 436.02, 436.19]}

df_m=pd.DataFrame(raw_data, columns = ['Time', 'TPU', 'CPU', 'MemUsed'])

df_m
##Sum of all values in column Time(ms)
(df_m.iloc[:, 0].sum())

##Time per inference(ms)
ax = df_m.plot(kind = 'line', y = 'MemUsed', grid = True)
ax.set_xlabel("NUMBER OF INFERENCES")
ax.set_ylabel("MemUsed(MB)")
我尝试过的:
ax = df_m.plot(kind = 'line', y = 'MemUsed', grid = True)
df_m.plot(kind='line', ax=ax.twinx(), secondary_x=range(0, 39))
ax.set_xlabel("NUMBER OF INFERENCES")
ax.set_ylabel("MemUsed(MB)")
输出图:
enter image description here
大 table 长什么样子
enter image description here

最佳答案

除了您对 plotly 的正面评价之外,这里还有一个示例,说明如何为您的数据集实现多 xaxis。
代码比看起来简单得多。由于我格式化 dict 的方式,代码显得“冗长” s 以便于阅读。
关键要素是:

  • 添加 time 的累积总和列 ( time_c ) 用于 xaxis2 .
  • 添加与 xaxis 对齐的隐藏跟踪,以及与 xaxis2 对齐的时间数据.如果没有隐藏的轨迹,要么两个轴都不出现,要么它们出现但没有对齐,因为只绘制了一个轨迹。

  • (更新)示例代码:
    以下代码已更新,以解决 OP 在使用更大(70k 行)数据集时遇到的问题。
    关键的变化是对 layout['xaxis'] 的更新。和 layout['xaxis2']包含的字典 'type': 'category' , 'nticks'并定义 'range'键。
    import pandas as pd
    from plotly.offline import plot

    # Create the dataset.
    raw_data = {'time': [21.9235, 4.17876, 4.02168, 3.81504, 4.2972],
    'tpu': [33.3, 33.3, 33.3, 33.3, 33.3],
    'cpu': [32, 32, 32, 32, 32],
    'memused': [435.92, 435.90, 436.02, 436.02, 436.19]}

    df = pd.DataFrame(raw_data)
    df['time_c'] = df['time'].cumsum().round(2)

    # Plotting code.
    data = []
    layout = {'margin': {'t': 105},
    'title': {'text': 'Example Showing use of Secondary X-Axis',
    'y': 0.97}}

    # Create a (hidden) trace for the xaxis.
    data.append({'x': df.index,
    'y': df['memused'],
    'showlegend': False,
    'mode': 'markers',
    'marker': {'size': 0.001}})
    # Create the visible trace for xaxis2.
    data.append({'x': df['time_c'],
    'y': df['memused'],
    'xaxis': 'x2',
    'name': 'Inference'})

    # Configure graph layout.
    nticks = int(df.shape[0] // (df.shape[0] * 0.05))
    layout['xaxis'] = {'title': 'Number of Inferences',
    'nticks': nticks,
    'range': [df.index.min(), df.index.max()],
    'tickangle': 45,
    'type': 'category'}
    layout['xaxis2'] = {'title': 'Time(ms)',
    'nticks': nticks,
    'overlaying': 'x1',
    'range': [df['time_c'].min(), df['time_c'].max()],
    'side': 'top',
    'tickangle': 45,
    'type': 'category'}
    layout['yaxis'] = {'title': 'Memory Used (MB)'}

    fig = {'data': data, 'layout': layout}
    plot(fig, filename='/path/to/graph.html')
    示例图(原始数据集):
    为了代码简单,我有意省略了任何额外的显示配置。然而,指的是顶级 plotly docs ,图形为 高度可配置。
    enter image description here
    示例图(新数据集):
    此图使用来自另一个答案的(更大的 70k 行)合成数据集。
    enter image description here

    关于Python:如何为单个跟踪添加辅助 x 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63875637/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com