0

Right now I have a list of coordinates that are in the following format:

(1,12),(2,3),(3,9)....

All the coordinates are in brackets as above, and the format is exactly the same for the rest of the string (the list is in string format). The x-values of the coordinates increase by one throughout the string (and could continue on to even 100000. The y-values are random and could range from 1 to 2048 or even higher numbers. I want to find a way to take that string (which is a list of coordinates in the above format) and turn it into a picture or virtual graph, with each point plotted on it. I am currently using Python 2.7 in visual studio, but am open to using other programs, as the string itself is stored in a .txt file in the above format.

Once that is done, I'd like to be able to take that graph and using only it as a base point, be able to regenerate the entire string again.

1 Answers1

1

You can use matplotlib to plot and ast.literal_eval to evaluate your string:

import matplotlib.pylab as plt
from ast import literal_eval
from operator import itemgetter

with open("your_file") as f:
    lst = literal_eval(f.read())
    x,y = map(itemgetter(0),lst), map(itemgetter(1),lst)
    plt.plot(x,y)
    plt.show()
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Beautiful, this code is perfect for what I'm trying to do. My question is, though, how would I be able to revert that code back to my list of coordinates? – Shivansh Vij Aug 21 '15 at 00:44
  • @ShivanshVij, `lst = literal_eval(f.read())` holds your list of tuples – Padraic Cunningham Aug 21 '15 at 00:45
  • No, I mean using only the plt.show() command (Because when that command executes it allows me to save the graph as different file types, including png's but also other types). I need to be able to use only that saved file (either the png, or not) and turn it back into the list of tuples. – Shivansh Vij Aug 21 '15 at 00:47
  • turn a png into a list of coords? – Padraic Cunningham Aug 21 '15 at 00:49
  • Not necessarily png. I have the option to save it as .png, .eps, .pdf, .pgf, .ps, .raw, .rgba, .svg, .svgz I was thinking .ps (postscript) becuase it is in reality text already, and using that would work. I just don't know how. Would it help if I uploaded the saveable formats? – Shivansh Vij Aug 21 '15 at 00:57
  • I want to be able to save the list (which is quite long) to an image of the graph. Then I want to be able to later take that image and using only it, derive the list again. Of course, it doesn't have to be an image, the other formats are just fine with me too. Thanks so much for your help so far. – Shivansh Vij Aug 21 '15 at 01:09
  • Do you have any ideas? – Shivansh Vij Aug 21 '15 at 01:24
  • @ShivanshVij, I can pull the coords of the points easily, I just need to figure the rest, it is late here so I will have another crack after some sleep and edit the answer later today – Padraic Cunningham Aug 21 '15 at 01:35
  • @ShivanshVij, http://stackoverflow.com/questions/13306519/get-data-from-plot-with-matplotlib – Padraic Cunningham Aug 21 '15 at 01:52
  • okay I looked at the link you left, and I don't get it. The plot being made with your code isn't a scatter plot, it's a bar graph. So how would that enormous code (the DataCursor) even help? Anyways, I feel like there is an easier method to all of that. Any other ideas? And thanks for your help. – Shivansh Vij Aug 21 '15 at 02:25
  • Anyone have any ideas? – Shivansh Vij Aug 21 '15 at 20:37