列名称和图中的分组依据字符串

时间:2019-04-01 10:11:07

标签: python pandas

可重现:

 nan = np.NaN
    bynote = {'bynote_n1_trial_ms': {0: -18963961,
  1: 31992270,
  2: -13028311,
  3: 31992270,
  4: -13028311,
  5: -13028311},
 'bynote_n2_trial_ms': {0: nan,
  1: -13028311.0,
  2: nan,
  3: nan,
  4: nan,
  5: nan},
 'bynote_n3_trial_ms': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan, 5: nan},
 'n1': {0: 60, 1: 60, 2: 60, 3: 40, 4: 40, 5: 30},
 'n2': {0: nan, 1: 40.0, 2: 40.0, 3: nan, 4: 30.0, 5: nan},
 'n3': {0: nan, 1: nan, 2: 30.0, 3: nan, 4: nan, 5: nan}}

    bynote = pd.DataFrame().from_dict(bynote)

我想绘制一个图,其中列n1,n2等是x值,bynote_n1_trial_ms,bynote_n2_trial_ms等是对应的y值。

我应该以某种方式对字符串进行分组(即n1等)还是我该怎么做?

编辑:

enter image description here

每行代表一个单独的图,其中左DF为y,右DF为。

2 个答案:

答案 0 :(得分:1)

我相信可以与每个DataFrame separately一起使用:

df1 = bynote.iloc[:, 3:]
df2 = bynote.iloc[:, :3]

因此使用plt.plot()的压缩列循环:

import matplotlib.pyplot as plt

for x, y in zip(df1.columns, df2.columns):
    plt.plot(df1[x], df2[y])

plt.show()

编辑:

for x, y in zip(df1.columns, df2.columns):
    plt.figure()
    plt.plot(df1[x], df2[y])

plt.show()

答案 1 :(得分:0)

尝试通过将x和y定义为列的数组来覆盖图:

import pandas as pd
import matplotlib.pyplot as plt


nan = np.NaN
bynote = {'bynote_n1_trial_ms': {0: -18963961,
  1: 31992270,
  2: -13028311,
  3: 31992270,
  4: -13028311,
  5: -13028311},
 'bynote_n2_trial_ms': {0: nan,
  1: -13028311.0,
  2: nan,
  3: nan,
  4: nan,
  5: nan},
 'bynote_n3_trial_ms': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan, 5: nan},
 'n1': {0: 60, 1: 60, 2: 60, 3: 40, 4: 40, 5: 30},
 'n2': {0: nan, 1: 40.0, 2: 40.0, 3: nan, 4: 30.0, 5: nan},
 'n3': {0: nan, 1: nan, 2: 30.0, 3: nan, 4: nan, 5: nan}}

bynote = pd.DataFrame().from_dict(bynote)


plt.figure()

plt.scatter(bynote['n1'], bynote['bynote_n1_trial_ms'])
plt.scatter(bynote['n2'], bynote['bynote_n2_trial_ms'])
plt.scatter(bynote['n3'], bynote['bynote_n3_trial_ms'])

plt.show()