代码之家  ›  专栏  ›  技术社区  ›  user026

如何在二维打印中打印3轴信息?

  •  0
  • user026  · 技术社区  · 4 年前

    这是一个更大数据的例子。想象一下下面这个数据框,我想在2D图中绘制3个信息,就像一个多行方案。

    我想把专长[0]到专长[3]的值放在Y轴上,特征列的数量(4)放在X轴上,深度值也放在Y轴上。虽然我只画出了一行的信息。

    df = pd.DataFrame({'depth':[300, 301, 302, 303, 304, 306],
                       'feat[0]':[0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
                       'feat[1]':[0.2, 0.2, 0.2, 0.2, 0.2, 0.2],
                       'feat[2]':[0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
                       'feat[3]':[0.2, 0.2, 0.2, 0.2, 0.2, 0.2]})
    
    plt.plot(np.arange(1, df.shape[1]), df.iloc[0,1:])
    plt.title('Depth 300')
    plt.ylabel('Features values')
    plt.xlabel('N of columns')
    plt.show()
    

    enter image description here

    我的想法是为每行绘制一条线(“深度”)。因此,这些值将有一个基于深度差的步长。所以最后我应该得到如下4行:

    enter image description here

    虽然我对这些绘图位置感到迷茫。有人能帮我吗?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Stef    4 年前

    你可以用一个 parallel coodinates plot

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({'depth':[300, 301, 302, 303, 304, 306],
                       'feat[0]':[0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
                       'feat[1]':[0.2, 0.2, 0.2, 0.2, 0.2, 0.2],
                       'feat[2]':[0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
                       'feat[3]':[0.2, 0.2, 0.2, 0.2, 0.2, 0.2]})
    
    offset = .01
    offsets = np.linspace(0, offset*len(df), len(df), endpoint=False)
    legend = [f'{d} (+{x})' if x > 0 else f'{d}' for d,x in zip(df.depth, offsets)]
    
    pd.plotting.parallel_coordinates((df.iloc[:,1:]+offsets[None].T).assign(depth=legend), 'depth')
    

    enter image description here 您可能需要根据需要调整偏移。