Python如何利用pandas读取csv数据并绘图

米米素材网

如何利用pandas读取csv数据并绘图

导包,常用的numpy和pandas,绘图模块matplotlib,

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)

读取csv文件的数据,保存到numpy数组内

path_csv = "E:\\python\\python\\2021\\202104\\04091\\path_data.csv"
xa = np.array([42.0, 44.4, 43.1, 40.6])
ya = np.array([21.6, 21.2, 13.5, 14.0])
xa1 = np.array([10, 40])
ya1 = np.array([10, 40])
path_data_x = pd.read_csv(path_csv, header=None, usecols=[0])
path_data_y = pd.read_csv(path_csv, header=None, usecols=[1])
path_x = np.array(path_data_x)[:, 0]
path_y = np.array(path_data_y)[:, 0]

绘制图像

print(path_x[0])
print(path_y[0])
ax.plot(xa1, ya1, color='g', linestyle='', marker='.')
ax.plot(xa, ya, color='g', linestyle='-', marker='.')
ax.plot(path_x, path_y, color='m', linestyle='', marker='.')
plt.show()

展示结果

pandas画pearson相关系数热力图

pearson相关系数计算函数

  • data.corr()

该方法支持空值:np.nan

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
data = pd.DataFrame({"A":[np.nan,2,9], "B":[4,14,6], "c":[987,8,9]})
f, ax= plt.subplots(figsize = (14, 10))
corr = data.corr()
# print(corr)
sns.heatmap(corr,cmap='RdBu', linewidths = 0.05, ax = ax)
# 设置Axes的标题
ax.set_title('Correlation between features')
plt.show()
plt.close()
f.savefig('sns_style_origin.jpg', dpi=100, bbox_inches='tight')

其中heatmap()方法中有annot参数,默认为False,不显示每个颜色的数字,如果设置为:annot=True, 则在每个热力图上显示数字。

效果如下:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持米米素材网。