0

I have a three-dimensional array.

The first dimension has 4 elements. The second dimension has 10 elements. The third dimension has 5 elements.

I want to plot the contents of this array as follows.

Each element of the first dimension gets its own graph (four graphs on the page) The values of the second dimension correspond to the y values of the graphs. (there are 10 lines on each graph) The values of the third dimension correspond to the x values of the graphs (each of the 10 lines has 5 x values)

I'm pretty new to python, and even newer to graphing. I figured out how to correctly load my array with the data...and I'm not even trying to get the 'four graphs on one page' aspect working.

For now I just want one graph to work correctly. Here's what I have so far (once my array is set up, and I've correctly loaded my arrays. Right now the graph shows up, but it's blank, and the x-axis includes negative values. None of my data is negative)

for n in range(1):
  for m in range(10):
      for o in range(5):
        plt.plot(quadnumcounts[n][m][o])
        plt.xlabel("Trials")
        plt.ylabel("Frequency")
  plt.show()

Any help would be really appreciated!

Edit. Further clarification. Let's say my array is loaded as follows:

myarray[0][1][0] = 22
myarray[0][1][1] = 10
myarray[0][1][2] = 15
myarray[0][1][3] = 25
myarray[0][1][4] = 13

I want there to be a line, with the y values 22, 10, 15, 25, 13, and the x values 1, 2, 3, 4, 5 (since it's 0 indexed, I can just +1 before printing the label)

Then, let's say I have

myarray[0][2][0] = 10
myarray[0][2][1] = 17
myarray[0][2][2] = 9
myarray[0][2][3] = 12
myarray[0][2][4] = 3

I want that to be another line, following the same rules as the first.

2
  • Does your data exist as (x,y)-pairs, or are the x- or y-coordinates the same as the indices? Can you give some example data and some context on what you want to plot? Say quadnumcounts[0][3] is the list [7.1, 6.5, 4.7, 2.3, 9.8]. Do you want to plot a line through the points (0, 7.1), (1, 6.5), ..., (4, 9.8)? Commented Sep 28, 2015 at 17:42
  • Added some clarification. Does that help? Commented Sep 28, 2015 at 18:01

2 Answers 2

1

Here's how to make the 4 plots with 10 lines in each.

import matplotlib.pyplot as plt
for i, fig_data in enumerate(quadnumcounts):
    # Set current figure to the i'th subplot in the 2x2 grid
    plt.subplot(2, 2, i + 1)
    # Set axis labels for current figure
    plt.xlabel('Trials')
    plt.ylabel('Frequency')
    for line_data in fig_data:
        # Plot a single line
        xs = [i + 1 for i in range(len(line_data))]
        ys = line_data
        plt.plot(xs, ys)
# Now that we have created all plots, show the result
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the example of creating subplots of your data. You have not provided the dataset so I used x to be an angle from 0 to 360 degrees and the y to be the trigonemetric functions of x (sine and cosine).

Code example:

import numpy as np
import pylab as plt

x = np.arange(0, 361)  # 0 to 360 degrees
y = []
y.append(1*np.sin(x*np.pi/180.0))
y.append(2*np.sin(x*np.pi/180.0))
y.append(1*np.cos(x*np.pi/180.0))
y.append(2*np.cos(x*np.pi/180.0))

z = [[x, y[0]], [x, y[1]], [x, y[2]], [x, y[3]]]  # 3-dimensional array


# plot graphs
for count, (x_data, y_data) in enumerate(z):
    plt.subplot(2, 2, count + 1)
    plt.plot(x_data, y_data)
    plt.xlabel('Angle')
    plt.ylabel('Amplitude')
    plt.grid(True)

plt.show()

Output:

enter image description here

UPDATE: Using the sample date you provided in your update, you could proceed as follows:

import numpy as np
import pylab as plt

y1 = (10, 17, 9, 12, 3)
y2 = (22, 10, 15, 25, 13)
y3 = tuple(reversed(y1))  # generated for explanation
y4 = tuple(reversed(y2))  # generated for explanation
mydata = [y1, y2, y3, y4]

# plot graphs
for count, y_data in enumerate(mydata):
    x_data = range(1, len(y_data) + 1)
    print x_data
    print y_data
    plt.subplot(2, 2, count + 1)
    plt.plot(x_data, y_data, '-*')
    plt.xlabel('Trials')
    plt.ylabel('Frequency')
    plt.grid(True)

plt.show()

Note that the dimensions are slightly different from yours. Here they are such that mydata[0][0] == 10, mydata[1][3] == 25 etc. The output is show below: enter image description here

1 Comment

This is exactly what I want! Now I just need to know how to put 10 lines on each graph.

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.