从python中的电子表格列绘制Contourf

时间:2021-01-11 15:00:18

I want to plot a coloured contour graph with x,y,z from 3 columns of a comma delimited text file, but each time I try the code below, I get ValueError: too many values to unpack (expected 3) error. I would be grateful if that could be resolved.

我想用逗号分隔的文本文件的3列中的x,y,z绘制彩色等高线图,但每次我尝试下面的代码时,我得到ValueError:解压缩(预期3)错误的值太多。如果可以解决,我将不胜感激。

I would also like to know if there is another (probably better) code for plotting the 3 independent columns.

我还想知道是否有另一个(可能更好的)代码用于绘制3个独立列。

This is the code:

这是代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import scipy.interpolate

N = 100000

long_col, lat_col, Bouguer_col = np.genfromtxt(r'data.txt', unpack=True)

xi = np.linspace(long_col.min(), long_col.max(), N)
yi = np.linspace(lat_col.min(), lat_col.max(), N)
zi = scipy.interpolate.griddata((long_col, lat_col), Bouguer_col, (xi[None,:], yi[:,None]), method='cubic')


fig = plt.figure()
plt.contourf(xi, yi, zi)
plt.xlabel("Long")
plt.ylabel("Lat")
plt.show()

This is the 'data.txt' sample data.

这是'data.txt'样本数据。

Lat, Long, Elev, ObsGrav, Anomalies
6.671482000000001022e+00,7.372505999999999560e+00,3.612977999999999952e+02,9.780274000000000233e+05,-1.484474523360840976e+02
6.093078000000000216e+00,7.480882000000001142e+00,1.599972999999999956e+02,9.780334000000000233e+05,-1.492942383352201432e+02
6.092045999999999850e+00,7.278669999999999973e+00,1.462445999999999913e+02,9.780663000000000466e+05,-1.190960417173337191e+02
6.402087429999999912e+00,7.393360939999999992e+00,5.237939999999999827e+02,9.780468000000000466e+05,-8.033459449396468699e+01
6.264082730000000154e+00,7.518244540000000420e+00,2.990849999999999795e+02,9.780529000000000233e+05,-1.114865156192099676e+02
6.092975000000000030e+00,7.482914000000000065e+00,1.416474000000000046e+02,9.780338000000000466e+05,-1.525697779102483764e+02
6.383570999999999884e+00,7.289616999999999791e+00,2.590403000000000020e+02,9.780963000000000466e+05,-8.300666170357726514e+01
6.318417000000000172e+00,7.557638000000000744e+00,1.672036999999999978e+02,9.780693000000000466e+05,-1.246774551668204367e+02
6.253779999999999895e+00,7.268805999999999656e+00,1.059429999999999978e+02,9.781026999999999534e+05,-9.986763240839354694e+01
6.384635000000000282e+00,7.291032000000000401e+00,2.615624000000000251e+02,9.780963000000000466e+05,-8.256190758384764194e+01

1 个解决方案

#1


1  

If the data file looks exactly like in the question you first of all have 5 columns, which you cannot unpack to 3 variables.
Next, you have a header line which you do not want to be part of the data. Also the header line is separated by ,<space>, while the data is separated by ,.

如果数据文件看起来与问题完全相同,那么您首先要有5列,这些列无法解压缩为3个变量。接下来,您有一个标题行,您不希望它成为数据的一部分。标题行也用 分隔,而数据用,分隔,。

So in total you need

所以总的来说你需要

import numpy as np

a,b,c,d,e = np.genfromtxt("data.txt", unpack=True, delimiter=",", skip_header=1)

#1


1  

If the data file looks exactly like in the question you first of all have 5 columns, which you cannot unpack to 3 variables.
Next, you have a header line which you do not want to be part of the data. Also the header line is separated by ,<space>, while the data is separated by ,.

如果数据文件看起来与问题完全相同,那么您首先要有5列,这些列无法解压缩为3个变量。接下来,您有一个标题行,您不希望它成为数据的一部分。标题行也用 分隔,而数据用,分隔,。

So in total you need

所以总的来说你需要

import numpy as np

a,b,c,d,e = np.genfromtxt("data.txt", unpack=True, delimiter=",", skip_header=1)