python 定义变量x格式_如何从CSV文件中提取数据列并将它们定义为x和y变量,然后使用pylab在python中绘制它们?...
我知道这篇文章已经过时了;但是,对于需要快速绘制csv数据的人来说,下面的脚本将提供一个很好的解决方案。
它展示了如何从csv文件导入数据,以及如何使用matplotlib绘制一个png并打印出来。
使用没有标题的Lake Huron的数据,可以得到一个很好的绘图。import csv
import matplotlib.pyplot as plt
from numpy import arange
#####################################
# instatiation of variables
filehandle = "lake_huron.csv"
x_data = [] # makes a list
y_data = [] # makes a list
png_filename = 'LakeData.png'
#####################################
# opening csv file and reading
my_file = open(filehandle, "rb") # opens file for reading
data = csv.reader(my_file) # saves file to variable "data"
#####################################
# saves csv data to x_data and y_data
for row in data:
x_data.append(eval(row[1])) # selects data from the ith row
y_data.append(eval(row[2])) # selects data from the ith row
#####################################
# closes csv file
my_file.close() # closes file
#####################################
# makes plot (show) and saves png
fig = plt.figure() # calls a variable for the png info
# defines plot's information (more options can be seen on matplotlib website)
plt.title("Level of Lake Huron from 1875 to 1972") # plot name
plt.xlabel('Year') # x axis label
plt.ylabel('Lake Level (in feet)') # y axis label
plt.xticks(arange(1875,1973,10)) # x axis tick marks
plt.axis([1875,1973,573,584]) # x and y ranges
# defines png size
fig.set_size_inches(10.5,5.5) # png size in inches
# plots the data from the csv above
plt.plot(x_data,y_data)
#saves png with specific resolution and shows plot
fig.savefig(png_filename ,dpi=600, bbox_inches='tight') # saves png
plt.show() # shows plot
plt.close() # closes pylab
总结
以上是生活随笔为你收集整理的python 定义变量x格式_如何从CSV文件中提取数据列并将它们定义为x和y变量,然后使用pylab在python中绘制它们?...的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: easyui复杂表单_EasyUI中实现
- 下一篇: python中的input函数怎么用_p