Python 借助DataFrame的pivot进行数据的可视化

借助DataFrame的pivot函数进行数据的可视化


这是数据
在这里插入图片描述

1.把数据读取成DataFrame格式

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('score(1).csv',header=None)
data_original = {'algorithm':df.iloc[1:,1].values,'target':df.iloc[1:,2].values,'number':df.iloc[1:,3].values}
data_original = pd.DataFrame(data_original)
data_original

在这里插入图片描述
2.使用pivot函数对DataFrame进行数据透视

data = data_original.pivot(index = 'algorithm',columns = 'target',values = 'number')
data

在这里插入图片描述
3.我们只需要数据,所以

data.values

在这里插入图片描述4. 画图

plt.plot(range(5), data.values[0,:], 'r-',label = 'relief_f')
plt.plot(range(5), data.values[1,:], 'g-',label = 'relief_f_n')
plt.xticks(range(5),data.columns)
plt.xlabel('Target')
plt.ylabel('Precision')
plt.title("The Precision")

ax = plt.gca()
ax.invert_yaxis() #y轴反向

plt.legend()  # 显示左下角的图例
plt.show()

在这里插入图片描述

每篇小附录:
SNAP图论数据集

猜你喜欢

转载自blog.csdn.net/qq_46523755/article/details/105404212