Python导入txt表文件中的等高线图 [英] Contour plot in Python importing txt table file
问题描述
我正在尝试绘制等高线图,
I am trying to make a contour plot like:
使用一个txt文件中的数据表(如3列),行数长.
Using a table of data like 3 columns in a txt file, with a long number of lines.
使用此代码:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
data = np.loadtxt(r'dataa.txt')
a = [data[:,0]]
b = [data[:,1]]
n = [data[:,2]]
x = np.asarray(a)
y = np.asarray(b)
z = np.asarray(n)
print "x = ", x
print "y = ", y
print "z = ", z
fig=plt.figure()
CF = contour(x,y,z,colors = 'k')
plt.xlabel("X")
plt.ylabel("Y")
plt.colorbar()
plt.show()
我不知道为什么,它不起作用.Python 为我提供了我期望看到的值的正确轴,但图中只是一个空白,我知道它正在以正确的方式导入数据,因为它在绘图之前向我显示了我的值.
I don't know why, it is not working. Python gives me the right axes for the values that I am expecting to see, but in the graph is just a blank and I know that it is importing the data in right way because it shows me my values before the plot.
表示例:(不同之处在于我的表有90000行)
Example of table: (the diference is because my table has 90000 lines)
使用此代码:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
N = 1000 #number of points for plotting/interpolation
x, y, z = np.genfromtxt(r'dataa.txt', unpack=True)
xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')
fig = plt.figure()
plt.contour(xi, yi, zi)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
我得到了这个结果:我想我得到的建议是错误的.
Ive got this result: I think I've got the advices wrongly.
推荐答案
我的评论后续...首先,我将替换所有这些行:
Followup from my comment... first, I would replace all these lines:
data = np.loadtxt(r'dataa.txt')
a = [data[:,0]]
b = [data[:,1]]
n = [data[:,2]]
x = np.asarray(a)
y = np.asarray(b)
z = np.asarray(n)
与:
x, y, z = np.genfromtxt(r'dataa.txt', unpack=True)
您的原始代码在前面添加了一条额外的轴,因为 [data [:,0]]
是具有一个元素的数组的列表.结果是 x.shape
将是 (1, N)
而不是 (N,)
.所有这些都可以使用上面的最后一行自动完成,或者您可以使用相同的 data
加载并说:
Your original code is adding an extra axis at the front, since [data[:,0]]
is a list of arrays with one element. The result is that x.shape
will be (1, N)
instead if (N,)
. All of this can be done automatically using the last line above, or you could just use the same data
loading and say:
x = data[:,0]
y = data[:,1]
z = data[:,2]
因为这些切片将为您提供阵列.
since those slices will give you an array back.
然而,您还没有完全完成,因为 plt.contour
期望您为 z
提供一个二维数组,而不是一个一维值数组.现在,您似乎在给定的 x,y
点处具有 z
值,但是 contour
希望您为其提供一个2d数组,例如图像.
However, you're not quite done, because plt.contour
expects you to give it a 2d array for z
, not a 1d array of values. Right now, you seem to have z
values at given x, y
points, but contour
expects you to give it a 2d array, like an image.
在我回答之前,我需要知道 x
和 y
是如何间隔的.如果定期,你可以很容易地填充一个数组.如果不规律,基本上要插值才能做出等高线图.
Before I can answer that, I need to know how x
and y
are spaced. If regularly, you can just populate an array pretty easily. If not regularly, you basically have to interpolate before you can make a contour plot.
要进行插值,请使用
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
N = 1000 #number of points for plotting/interpolation
x, y, z = np.genfromtxt(r'dataa.txt', unpack=True)
xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')
fig = plt.figure()
plt.contour(xi, yi, zi)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
这篇关于Python导入txt表文件中的等高线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!