0

I am trying to create a set of inputs for an LSTM Neural Network. I wish to create either a list or array (whichever is easiest to work with) from the y co-ordinates of each point on the graph. I have found many tutorials describing how to create a graph from a list/array, but none that describe the other way around. Here is the code I'm working with:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# x-values
x1 = [0, 1, 2, 3, 4, 5]
x2 = [0, 2, 3, 6, 8, 9]
x3 = [0, 3, 5, 8, 9, 10]

# y-values
y1 = [0, 2, 4, 5, 7, 9]
y2 = [0, 3, 4, 7, 8, 9]
y3 = [10, 8, 7, 4, 3, 1]

#Plotting the Line on Graph
plt.plot(x1, y1, color='blue')
plt.plot(x2, y2, color='green')
plt.plot(x3, y3, color='red')
plt.show()

#X-axis label
plt.xlabel("numShot")

#Y-axis label
plt.ylabel("bin_num")

#Graph title
plt.title("GRAPH EXAMPLE")

for n in x1:
    plt.plot(x1,y1, color='blue')
    plt.xlim(n,n+2)
    plt.xlabel("numShot")
    plt.ylabel("bin_num")
    plt.title("GRAPH EXTRACT")
    plt.show()
for n in x2:
    plt.plot(x2,y2, color='green')
    plt.xlim(n,n+2)
    plt.xlabel("numShot")
    plt.ylabel("bin_num")
    plt.title("GRAPH EXTRACT")
    plt.show()
for n in x3:
    plt.plot(x3,y3, color='red')
    plt.xlim(n,n+2)
    plt.xlabel("numShot")
    plt.ylabel("bin_num")
    plt.title("GRAPH EXTRACT")
    plt.show()

Assume these x,y values are not known, they are for example purposes only

The above code creates 3 lines on a single graph, I would like to write a script that would turn the y co-ordinates from the graph back into the list/array as simply as possible, as my coding skill is, er, average at best.

Edit: I've tried adding the following lines to the script as per John's links, however this throws up the following error

line = gca().get_lines()[n]
yd = line.get_ydata()

NameError: name 'gca' is not defined

This confuses me, as I thought gca came with the pyplot import as opposed to being a variable that needs defining.

  • 1
    Does this answer your question? [How to extract data from matplotlib plot](https://stackoverflow.com/questions/8938449/how-to-extract-data-from-matplotlib-plot) – ddejohn Oct 18 '21 at 18:05
  • I think it might, however whenever I try to run the code suggested in the top answer it returns that "gca is not defined" (I'm using the "alternative method" as I only need the y values). Is there something missing from the original post? – SomeCodingCasual Oct 18 '21 at 18:55
  • Well, it assumes you already have a matplotlib plot object, which *I* assumed given that you're importing matplotlib in your example code. – ddejohn Oct 18 '21 at 18:59
  • Please post the code you've used to generate your graph, and the code you've tried using to extract the data from it, if you're having problems. – ddejohn Oct 18 '21 at 19:00
  • I've just ran: line = gca().get_lines()[n] yd = line.get_ydata() in addition to the script in my original comment (including the matplotlib import), yet the "gca is not defined" error persists. – SomeCodingCasual Oct 18 '21 at 19:03
  • Well `gca` is a `pyplot` method... you need to use `plt.gca()`. – ddejohn Oct 18 '21 at 19:06
  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). – furas Oct 18 '21 at 19:13
  • 1
    better show example code with graph which you want to convert to list. – furas Oct 18 '21 at 19:14
  • I've updated the original post to show the full script – SomeCodingCasual Oct 19 '21 at 13:08
  • I'm under the impression that you have a publication (a paper, a book, a web site etc) containing a graph, and you'd like to extract the values from the published image. – gboffi Oct 19 '21 at 14:20
  • Sort of, for my Masters dissertation I have been sent a dataset that creates a series of lines similar to the one depicted above. However, the actual lines have hundreds of individual data points so it is not practical for me to manually identify all of the y values on a single line. These y values will serve as the input to a Neural Network and I need them as a data vector, not as an image. – SomeCodingCasual Oct 19 '21 at 15:48

0 Answers0