Python利用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()
展示结果: