读取CSV文件并在Matplotlib中绘制出来: CSV文件样式: 代码如下:
#!E:\anaconda\python.exe
# -*- coding:utf-8 -*-
import csv
from matplotlib import pyplot as plt
xiaoliang ={}
#读取csv文件,将文件中的数据绘制出来
with open("C:\\Users\\Administrator\\Desktop\\xiaoliang.csv") as f:
reader = csv.reader(f)
header = next(reader) #使读取CSV文件中不包含表头
#print(header)
for i in reader:
xiaoliang[int(i[0])] = int(i[1]) #此处数据必须为int型
plt.scatter(xiaoliang.keys(),xiaoliang.values()) #散点图
plt.plot(xiaoliang.keys(),xiaoliang.values()) #连续图
plt.title("《python入门销量》",fontproperties="SimHei") #fontproperties解决中文不显示问题
plt.xlabel(header[0],fontproperties="SimHei")
plt.ylabel(header[1],fontproperties="SimHei")
plt.show()
注:纸上得来终觉浅,绝知此事要躬行。