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中实现
- 下一篇: 中的 隐藏鼠标菜单_如何在鼠标右键菜单中