2

I wrote a script that generates random coordinates and writes them into a text file with no formatting.

Is there a way to format this list so it is easier to read? Like (x, y) per line? Right now it is a list with a single space between them.

Is there an easier way to generate random coordinates in one python file without the use of a text file? Or is it easier to use a text file?

Below is the working code for this and an example of the text file: (Revised based on commentary and working)

import random
import threading

def main():

    #Open a file named numbersmake.txt.
    outfile = open('new.txt', 'w')

    for count in range(12000):
        x = random.randint(0,10000)
        y = random.randint(0,10000)
        outfile.write("{},{}\n".format(x, y))

    #Close the file.
    outfile.close()
    print('The data is now the the new.txt file')

def coordinate():
    threading.Timer(0.0000000000001, coordinate).start ()

coordinate()

#Call the main function
main()

I have tried split and does not work. I know I don't need the threading option. I would rather have the threading over the range but the range is okay for now...

Example of the text in text file: [4308][1461][1163][846][1532][318]... and so on


I wrote a python script that reads the text file of coordinates and places them on a graph, however, no points are being plotted. The graph itself does show. Below is the code: (Revised based on commentary)

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
from numpy import loadtxt

style.use('dark_background')

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

with open('new.txt') as graph_data:
    for line in graph_data:
        x, y = line.split(',') 

def animate(i):
    xs = []
    ys = []
    for line in graph_data:
        if len(line)>1:
            x,y = line.split(',')
            xs.append(x)
            ys.append(y)

    ax1.clear()
    ax1.plot(xs,ys)

lines = loadtxt("C:\\Users\\591756\\new.txt", comments="#", delimiter=",", unpack="false")
ani = animation.FuncAnimation(fig, animate, interval=1000) # 1 second- 10000 milliseconds
plt.show()
7
  • 1
    You must manually write a newline character into the file, or any other delimiter. However, pickle or json modules would be a better approach Commented Jun 8, 2017 at 13:35
  • 1
    Maybe no points are plotted because split(',') won't work without commas in the data? Commented Jun 8, 2017 at 13:37
  • 1
    Wonderful! The text file looks BEAUTIFUL. Thank you so much for your help! I have all the edits done and added to the script that is pulling values, however, I just received an error message stating "ValueError: I/O operation on closed file." Commented Jun 8, 2017 at 13:53
  • 1
    If you use the with open syntax, you don't need to manually close the file Commented Jun 8, 2017 at 13:55
  • 1
    I have updated the above text to show the revised code. Still not working with the "with open" syntax. Do I still need the "for" statement in the "def animate" ? Commented Jun 8, 2017 at 14:06

1 Answer 1

3

In order for the logic of your plotting to even work, the file should be written like so

with open('new.txt', 'w') as out_file:
    for count in range(12000):
        x = random.randint(0,10000)
        y = random.randint(0,10000)
        outfile.write("{},{}\n".format(x, y))

Also, you read lines like this

def animate(i):
    xs = []
    ys = []
    with open('filename') as graph_data:
        for line in graph_data:
            x, y = line.split(',') 
            xs.append(x)
            ys.append(y)
    ax1.clear()
    ax1.plot(xs,ys)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.