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

2024-05-16 07:45:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个更大数据的例子。想象一下下面这个数据框,我想在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

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


Tags: 数据信息dataframedf数量绘制方案plt
1条回答
网友
1楼 · 发布于 2024-05-16 07:45:42

根据深度偏移值后,可以使用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 您可能需要根据需要调整偏移

相关问题 更多 >