Bhagwan rajneesh
Bhagwan rajneesh

Reputation: 23

whats happening to the interface?

I made a little code to plot some data from a file

from numpy import *
import matplotlib.pyplot as plt
from math import *

with open("specfunc_from_idft_U8p0g_gate-1p5U.txt") as f:
    data = f.read()

data = data.split('\n')

x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[1] for row in data]

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax1.set_title("Reference plot")
ax1.set_xlabel('$\\omega/U$')
ax1.set_ylabel('$A(\\omega)$')

ax1.plot(x,y, c='r', label='$NRG$')

leg = ax1.legend()

plt.savefig('varying_alpha_hf_withJ_'+str('ff'+'.png'))

File specfunc_from_idft_U8p0g_gate-1p5U.txt

When I open the saved file this is what I get

Image

What I have to do to repair the machine? I am using Python 3.7 in a Mac os X

Upvotes: 0

Views: 40

Answers (1)

MegaIng
MegaIng

Reputation: 7886

After looking at your data and your program, it turns out you only have problems loading the data for two reasons

x = [row.split(' ')[0] for row in data]
y = [row.split(' ')[1] for row in data]

If you look at this code, you can see that x and y are going to be list of strings which is not what you want. That they are strings is the reason for the overlapping xticks and yticks. You want them all to be floats:

x = [float(row.split(' ')[0]) for row in data]
y = [float(row.split(' ')[1]) for row in data]

Doing this reveals the main flaw with the loading:

    x = [float(row.split(' ')[0]) for row in data]
ValueError: could not convert string to float: 

If you look at the file you will see that after line 250 the lines begin with a space which breaks the split function to a small degree:

>>> " 0.000000000E+00 0.240699776E+02 0.200482966E+01 0.495502884E-16 0.153587396E+01 0.000000000E+00".split(" ")
['', '0.000000000E+00', '0.240699776E+02', '0.200482966E+01', '0.495502884E-16', '0.153587396E+01', '0.000000000E+00']

As you can see the first element is empty, which can't be a number. So instead of .split(" ") you should use .split(), which will deal with spaces at the beggining:

>>> " 0.000000000E+00 0.240699776E+02 0.200482966E+01 0.495502884E-16 0.153587396E+01 0.000000000E+00".split()
['0.000000000E+00', '0.240699776E+02', '0.200482966E+01', '0.495502884E-16', '0.153587396E+01', '0.000000000E+00']

So here is the working loading code:

x = [float(row.split()[0]) for row in data]
y = [float(row.split()[1]) for row in data]

Upvotes: 1

Related Questions