欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python 定义变量x格式_如何从CSV文件中提取数据列并将它们定义为x和y变量,然后使用pylab在python中绘制它们?...

发布时间:2025/3/21 python 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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中绘制它们?...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。