【问题标题】:Python: display multiple values in a column for a single value in another columnPython:在一列中为另一列中的单个值显示多个值
【发布时间】:2021-08-29 04:23:24
【问题描述】:

我有一个包含三列的 pandas 数据框

data = np.array([[0,'Time',27.390000],
                [0,'Score',0.027585],
                [1,'Time',47.390000],
                [1,'Score',0.23776],
                [2,'Time',65.390000],
                [2,'Score',0.44776]])
data = pd.DataFrame(data)
data.columns = ['Rounds','Metrics','WA_All_par_1']
data["WA_All_par_1"] = pd.to_numeric(data["WA_All_par_1"])
data

    Rounds   Metrics    WA_All_par_1
1   0        Time       27.390000
2   0        Score      0.027585
3   1        Time       47.390000
4   1        Score      0.23776
5   2        Time       65.390000
6   2        Score      0.44776

我想在 X 轴上绘制轮次(每轮有 2 个指标)和 y 轴上的指标,我想随着轮次的增加显示分数和时间

我试过了

data.set_index('Rounds').plot(figsize=(8,5), grid=True)

我想在直方图中用不同颜色显示得分和时间。

【问题讨论】:

  • 你想在同一个图中用不同颜色绘制一个时间和一个分数吗?
  • 不是单独的图,而是两个指标(时间和分数)并排进行一轮

标签: python matplotlib plotly seaborn


【解决方案1】:

IIUC:

尝试:

(data.set_index(['Rounds','Metrics'])
 .unstack(1)
 .droplevel(0,axis=1)
 .sort_index(1,ascending=False)
 .plot(figsize=(8,5), grid=True,kind='bar',legend=False))

输出:

更新:

尝试:

data=(data.set_index(['Rounds','Metrics'])
 .unstack(1)
 .droplevel(0,axis=1)
 .reset_index())

fig,ax=plt.subplots(1,2,figsize=(8,5))
ax[0].bar(x=data['Rounds'],height=data['Score'])
ax[1].bar(x=data['Rounds'],height=data['Time'],color=['orange'])
ax[0].set_xlabel('Rounds')
ax[1].set_xlabel('Rounds')
ax[0].set_ylabel('Score')
ax[1].set_ylabel('Time')
ax[0].grid()
ax[1].grid()
plt.show()

【讨论】:

  • Round 应该并排显示时间和得分,x 轴应该显示 0、1、2,y 应该显示 Metrics with time 和 score as hostograms;一张图片中的2个单独的情节也很好
  • 它不显示哪个分数与哪个时间关联
  • 更新答案......请检查它是否是你想要的?
  • @Khaned Btw 这是与当前问题不同的问题......但无论如何更新的答案......请看看:)
【解决方案2】:

这是使用matplotlib 模块和groupby() 函数的解决方案。

代码

import matplotlib.pyplot as plt
import pandas as pd

data = np.array([[0,'Time',27.390000],
                [0,'Score',0.027585],
                [1,'Time',47.390000],
                [1,'Score',0.23776],
                [2,'Time',65.390000],
                [2,'Score',0.44776],
                [3, 'Time', 80.390000],
                [3, 'Score', 10.44776]])

data = pd.DataFrame(data)
data.columns = ['Rounds','Metrics','WA_All_par_1']
data["WA_All_par_1"] = pd.to_numeric(data["WA_All_par_1"])
print(data)

data=data.groupby('Rounds')

x = data.nth(0).index
X_axis = np.arange(len(data.nth(1).index))

plt.bar(X_axis - 0.2, data.nth(0)['WA_All_par_1'], 0.4, label='Time')
plt.bar(X_axis + 0.2, data.nth(1)['WA_All_par_1'], 0.4, label='Score')
plt.xticks(X_axis, x)
plt.xlabel("Rounds")
plt.legend()

plt.show()

输入

  Rounds Metrics  WA_All_par_1
0      0    Time     27.390000
1      0   Score      0.027585
2      1    Time     47.390000
3      1   Score      0.237760
4      2    Time     65.390000
5      2   Score      0.447760
6      3    Time     80.390000
7      3   Score     10.447760

注意:我添加了额外的行,因为您的分数太低而看不到

输出

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 2023-01-17
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多