【问题标题】:making a contour plot using irregular x y z data使用不规则 x y z 数据制作等高线图
【发布时间】:2021-09-25 11:04:22
【问题描述】:

我想用我所附的数据制作一张漂亮的等高线图,并想在地图上写上 A B C D E F G H。

dt   x      y       z
A  31.53  77.95   0.112
B  31.40  78.35   0.032
C  31.66  78.03  -0.001
D  31.48  77.75  -0.092
E  32.28  78.45  -0.113
F  31.99  76.42  -0.184
G  31.64  77.34  -0.016
H  32.50  75.62  -0.121

我的脚本在这里:

import matplotlib.pyplot as plt
import numpy as np

data=np.loadtxt("data.txt",dtype='str')

plt.tricontour(data[:,2],data[:,1],data[:,3],  colors='black');
#plt.tricontour(x,y,z)
plt.show()

我收到了错误

self.triangles, self._neighbors = _qhull.delaunay(x, y)
ValueError: x and y must be 1D arrays of the same length

请为此提出更好的解决方案。提前致谢。

我需要像https://jakevdp.github.io/PythonDataScienceHandbook/04.04-density-and-contour-plots.html这样的情节

【问题讨论】:

    标签: python pandas numpy matplotlib seaborn


    【解决方案1】:

    当您使用方法:np.loadtxt() 时,您会得到一个与 pandas Dataframe 不同的 ndarray 对象,因为第一个索引是 而不是列。

    np.loadtxt() 文档:https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html

    这段代码应该可以完成这项工作:

    import matplotlib.pyplot as plt
    import numpy as np
    
    data = np.loadtxt("data.txt", dtype='str')
    
    # Lets check out the internal order of the object.
    print(data[1])
    
    # so the first place are rows and not the columns.
    # lets take the column content:
    x = []
    y = []
    z = []
    for i in range(1, len(data)):
        x.append(float(data[i][1]))
        y.append(float(data[i][2]))
        z.append(float(data[i][3]))
    
    plt.tricontour(x, y, z, colors='black')
    plt.show()
    

    输出:

    上色后:

    【讨论】:

    • 我需要关闭轮廓...怎么做,我需要在轮廓线上写A B C D...
    • 嘿,你说关闭是什么意思?
    • 我添加了一个屏幕截图,让我知道您是否打算这样做
    • 我需要根据我的问题在上面写上 A B C D...
    猜你喜欢
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多