1

我有以下形式的数据(链接):

Y   X 0 X 10    X 20
15  4.83    4.91    4.99
20  4.58    4.65    4.73
25  4.43    4.49    4.56

我正在尝试绘制 X标签值的轮廓,在这些情况下为 0、10、20(如代码中的 z),x 轴作为 X 值,y 轴作为 Y 值。使用以下代码创建图:

import numpy as np
import re
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import matplotlib.cm as cm

with open('contour.dat', "r") as data:
    while True:
        line = data.readline()
        if not line.startswith('#'):
            break
    data_header = [i for i in line.strip().split('\t') if i]
_data_ = np.genfromtxt('contour.dat', skiprows = 2, delimiter = '\t')
x = _data_[:, 0]
y = _data_[:, 1:]


y_n = []
for i in range(len(data_header)):
    if data_header[i][0] == 'X':
        y_n = np.int_(np.append(y_n, i))
y_index = [data_header[i] for i in y_n]
z = []
for i in range(0, len(data_header)):
    z = np.append(z, re.findall(r"[-+]?\d*\.\d+|\d+", data_header[i]))
z = z.reshape(len(z), 1)
z = np.tile(z, (len(x), len(x)))


with PdfPages('./on_tau.pdf') as p_tau:
    _p_vs_tau_ = plt.figure(figsize=(5, 5))
    _p_vs_tau_.clf()
    p_vs_tau = plt.subplot(111)
    x, y = np.meshgrid(x, y)
    surf = plt.contourf(y, x, z, 30, rstride=1, cstride=1, cmap=cm.gist_heat,
                           linewidth=0, antialiased=False, alpha = 1.0)
    surf1 = plt.contour(y, x, z, 30, colors = '#000000',
                           linewidths=0.5, antialiased=False, alpha = 1.0)
    plt.clabel(surf1, inline=1, fontsize=6)
    p_tau.savefig(bbox_inches='tight')
    plt.close()

但我没有得到正确的轮廓。我相信我在某处做错了什么,可能是在重复 z 值时。如何正确获得轮廓?

4

1 回答 1

0

这会生成您正在寻找的轮廓吗?z=np.tile 语句中的修改代码(现已注释掉):

#z = np.tile(z, (len(x), len(x)))
xm = np.tile(x,11)
xm = np.reshape(xm,(11,10)).T
zm = np.tile(z,10).T

with PdfPages('./on_tau.pdf') as p_tau:
    _p_vs_tau_ = plt.figure(figsize=(5, 5))
    _p_vs_tau_.clf()
    p_vs_tau = plt.subplot(111)
    #x, y = np.meshgrid(x, y)
    surf = plt.contourf(y, xm, zm, 30, rstride=1, cstride=1, cmap=cm.gist_heat,
                       linewidth=0, antialiased=False, alpha = 1.0)
    surf1 = plt.contour(y, xm, zm, 30, colors = '#000000',
                       linewidths=0.5, antialiased=False, alpha = 1.0)
    plt.clabel(surf1, inline=1, fontsize=6)
    p_tau.savefig(bbox_inches='tight')
    plt.close()
于 2015-12-11T15:21:11.657 回答