[Python] How do I create a surface plot in Python using values from a text file?

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to get a 3D surface plot from a text file that has four columns and 700 rows. I have written the code below to generate that plot, but I constantly keep getting the following error:


    invalid value encountered in true_divide

    z = (xymlim)/(x*y)

    raise ValueError("Argument Z must be 2-dimensional.")

    ValueError: Argument Z must be 2-dimensional.
    ​

    Here is my code

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    from matplotlib import cm
    from matplotlib.ticker import LinearLocator, FormatStrFormatter
    import numpy as np


    f2 = open('openfile.txt', 'r')
    i6 = list()
    i9 = list()
    i10 = list ()
    count = 0
    while True:
    a = f2.readline()
    if not a: break
    v = a.split()
    i6.append(float(v[0]))
    i9.append(float(v[1]))
    i10.append(float(v[3]))
    count = count + 1


    mlim = np.array(i6)
    ira = np.array(i9)
    dec = np.array(i10)

    x = ira
    y = dec
    z = (x*y*mlim)/(x*y)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_wireframe(x,y,z, rstride=10, cstride=10)
    plt.savefig('filesave.png')
    plt.close()


    How do I solve this?

    I have also attached a screenshot of what the actual text file looks like:

    click here

    Thank you.

    Continue reading...

Compartilhe esta Página