[英]open and plot data in python with matplotlib
I am new to both Python and Stackoverflow. 我是Python和Stackoverflow的新手。 I have a .txt file that contains floating point numbers that looks something like this:
我有一个.txt文件,其中包含如下所示的浮点数:
8.2178200e-02
8.2173600e-02 8.2129400e-02 8.2209000e-02 8.2183000e-02 8.2098900e-02 8.2162500e-02 8.2157700e-02 8.2177900e-02 8.2177600e-02 8.2088400e-02 8.2142900e-02 8.2179600e-02 8.2159200e-02 8.2144800e-02 8.2139000e-02 8.2121200e-02 8.2157900e-02 8.2142600e-02 8.2190600e-02 8.2129500e-02 8.2125800e-02 8.2097500e-02 8.2087300e-02 8.2206800e-02 8.2175400e-02 8.2183300e-02 8.2197400e-02 8.2129500e-02 8.2101600e-02 8.2117800e-02 8.2125900e-02 8.2131300e-02 8.2107600e-02 8.2146900e-02 8.2122400e-02 8.2111800e-02 8.2156100e-02 8.2088500e-02 8.2135300e-02 8.2119700e-02 8.2100800e-02 8.2135700e-02 8.2126900e-02 8.2134000e-02 8.2111000e-02 8.2101600e-02 8.2108600e-02 8.2142900e-02 8.2091000e-02 8.2117700e-02 8.2061400e-02 8.2085200e-02 8.2080400e-02 8.2075400e-02 8.2064400e-02 8.2059700e-02 8.2098200e-02 8.2077200e-02 8.2138200e-02 8.2116300e-02 8.2092000e-02 8.2071900e-02 8.2092500e-02 8.2056900e-02 8.2108900e-02 8.2061300e-02 8.2064300e-02 8.2063900e-02 8.2120600e-02 8.2049500e-02 8.2087300e-02 8.2066 8.2173600e-02 8.2129400e-02 8.2209000e-02 8.2183000e-02 8.2098900e-02 8.2162500e-02 8.2157700e-02 8.2177900e-02 8.2177600e-02 8.2088400e-02 8.2142900e-02 8.2179600e-02 8.2159200e -02 8.2144800e-02 8.2139000e-02 8.2121200e-02 8.2157900e-02 8.2142600e-02 8.2190600e-02 8.2129500e-02 8.2125800e-02 8.2097500e-02 8.2087300e-02 8.2206800e-02 8.2175400e-02 8.2183300e-02 8.2197400e-02 8.2129500e-02 8.2101600e-02 8.2117800e-02 8.2125900e-02 8.2131300e-02 8.2107600e-02 8.2146900e-02 8.2122400e-02 8.2111800e-02 8.2156100e-02 8.2088500e -02 8.2135300e-02 8.2119700e-02 8.2100800e-02 8.2135700e-02 8.2126900e-02 8.2134000e-02 8.2111000e-02 8.2101600e-02 8.2108600e-02 8.2142900e-02 8.2091000e-02 8.2117700e-02 8.2061400e-02 8.2085200e-02 8.2080400e-02 8.2075400e-02 8.2064400e-02 8.2059700e-02 8.2098200e-02 8.2077200e-02 8.2138200e-02 8.2116300e-02 8.2092000e-02 8.2071900e-02 8.2092500e -02 8.2056900e-02 8.2108900e-02 8.2061300e-02 8.2064300e-02 8.2063900e-02 8.2120600e-02 8.2049500e-02 8.2087300e-02 8.2066 800e-02 8.2074900e-02 8.2052400e-02 8.2093200e-02 8.2061800e-02 8.2043700e-02 8.2070500e-02 8.2056900e-02 8.2084000e-02 8.2075900e-02 8.2065900e-02 8.2054200e-02 8.2037400e-02 8.2040600e-02 8.2085500e-02 8.2029000e-02 8.2057000e-02 8.2045700e-02 8.2112600e-02 8.2068000e-02 8.2034900e-02 8.2045200e-02 8.2046400e-02 8.2067300e-02 8.2080500e-02 8.2021400e-02 8.2047300e-02 8.2060200e-02 8.2042900e-02 8.2065200e-02 8.2056100e-02 8.1990900e-02 8.2055700e-02 8.2030300e-02 8.2103400e-02 8.2092600e-02 8.1995200e-02 8.2075300e-02 8.2001500e-02 8.2064000e-02 8.2033500e-02 8.2042800e-02 8.2037400e-02 8.2002000e-02 8.2057900e-02 8.2025100e-02 8.2038900e-02 8.2035200e-02 8.2005700e-02 8.2016700e-02 8.2012800e-02 8.1984900e-02 8.2066200e-02 8.2029600e-02 8.2027400e-02 8.2012200e-02 8.2009400e-02 8.2024900e-02 8.2038700e-02 8.2034700e-02 8.2016200e-02 8.1964500e-02 8.2019400e-02 8.2010500e-02 8.2004100e-02 8.2057500e-02 8.2052300e-02 8.2004500e-02 8.1998400e-02 8.2011600e-02 8.2038400e-02 8.2002500e-0
800e-02 8.2074900e-02 8.2052400e-02 8.2093200e-02 8.2061800e-02 8.2043700e-02 8.2070500e-02 8.2056900e-02 8.2084000e-02 8.2075900e-02 8.2065900e-02 8.2054200e-02 8.2037400e- 02 8.2040600e-02 8.2085500e-02 8.2029000e-02 8.2057000e-02 8.2045700e-02 8.2112600e-02 8.2068000e-02 8.2034900e-02 8.2045200e-02 8.2046400e-02 8.2067300e-02 8.2080500e-02 8.2021400 e-02 8.2047300e-02 8.2060200e-02 8.2042900e-02 8.2065200e-02 8.2056100e-02 8.1990900e-02 8.2055700e-02 8.2030300e-02 8.2103400e-02 8.2092600e-02 8.1995200e-02 8.2075300e- 02 8.2001500e-02 8.2064000e-02 8.2033500e-02 8.2042800e-02 8.2037400e-02 8.2002000e-02 8.2057900e-02 8.2025100e-02 8.2038900e-02 8.2035200e-02 8.2005700e-02 8.2016700e-02 8.2012800 e-02 8.1984900e-02 8.2066200e-02 8.2029600e-02 8.2027400e-02 8.2012200e-02 8.2009400e-02 8.2024900e-02 8.2038700e-02 8.2034700e-02 8.2016200e-02 8.1964500e-02 8.2019400e- 02 8.2010500e-02 8.2004100e-02 8.2057500e-02 8.2052300e-02 8.2004500e-02 8.1998400e-02 8.2011600e-02 8.2038400e-02 8.2002500e-0 2 8.2005700e-02 8.2065900e-02 8.1991200e-02 8.2039900e-02 8.2028200e-02 8.2027000e-02 8.2021300e-02 8.2019600e-02 8.2032900e-02 8.2011700e-02 8.2017400e-02 8.2069400e-02 8.1998400e-02 8.2059400e-02 8.1958300e-02 8.1995800e-02 8.2018500e-02 8.1973400e-02 8.2008800e-02 8.1995900e-02 8.1989400e-02 8.1991800e-02 8.2000600e-02 8.2040400e-02 8.2035700e-02 8.1987800e-02 8.2027400e-02 8.2010800e-02 8.1991300e-02 8.1999400e-02 8.1926800e-02 8.2021100e-02 8.1967800e-02 8.1992600e-02 8.2022200e-02 8.1933100e-02 8.1998900e-02 8.2004300e-02
2 8.2005700e-02 8.2065900e-02 8.1991200e-02 8.2039900e-02 8.2028200e-02 8.2027000e-02 8.2021300e-02 8.2019600e-02 8.2032900e-02 8.2011700e-02 8.2017400e-02 8.2069400e-02 8.1998400 e-02 8.2059400e-02 8.1958300e-02 8.1995800e-02 8.2018500e-02 8.1973400e-02 8.2008800e-02 8.1995900e-02 8.1989400e-02 8.1991800e-02 8.2000600e-02 8.2040400e-02 8.2035700e- 02 8.1987800e-02 8.2027400e-02 8.2010800e-02 8.1991300e-02 8.1999400e-02 8.1926800e-02 8.2021100e-02 8.1967800e-02 8.1992600e-02 8.2022200e-02 8.1933100e-02 8.1998900e-02 8.2004300 E-02
How can I build a program that put this data into a string, which I need to use to get frequency response and plot the data using Matplotlib? 我该如何构建将这些数据放入字符串的程序,我需要使用该程序来获取频率响应并使用Matplotlib绘制数据? The amount of data will be unknown.
数据量将是未知的。 For the y-axis, data points are plotted.
对于y轴,绘制数据点。 for x-axis, the number increments by 1, [0,1,2,3,4,5,6...n].
对于x轴,数字递增1,[0,1,2,3,4,5,6 ... n]。 If there is a better way to implement this, please elaborate.
如果有更好的方法可以实现这一点,请详细说明。 Thanks you!
谢谢!
使用numpy将数据读取到一个浮点数组中,针对范围(len(your_array))进行绘图,显示该绘图。
To avoid loading the whole file into memory, you could read them off one at a time like this: 为避免将整个文件加载到内存中,您可以一次读取一个文件,如下所示:
import matplotlib.pyplot as plt
with open('number_file.txt', 'r') as f:
number_string = f.read(13) # 13 characters in each number
x_index = 0
while number_string != '':
plt.plot([x_index], [float(number_string)], 'ob')
# print(x_index, number_string)
f.read(1) # this throws away the extra space
number_string = f.read(13)
x_index += 1
plt.show()
This assumes the numbers and spacing is always in that exact format. 假设数字和间距始终采用该确切格式。
You mentioned wanting information on the frequency information. 您提到想要有关频率信息的信息。 If you're looking for a power spectral density plot, have a look at
matplotlib.pyplot.psd
. 如果要查找功率谱密度图,请查看
matplotlib.pyplot.psd
。
For example, with the data you posted above: 例如,使用上面发布的数据:
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('yourdata.txt')
plt.psd(data)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.