Astrid
Astrid

Reputation: 1936

Convert a left-to-right plot to bottom-to-top (transpose not working)

I have a simple problem that I cannot quite understand why it doesn't work.

MWE:

import numpy as np
import matplotlib as plt
test = np.random.rand(100,5)
plt.plot(test)
plt.show()

Produces

enter image description here

Now all I want to do is to quite literally transpose the whole test matrix so that my data on the x-axis is now plotted vertically instead (so [0-100] is on y instead). But when I do that:

plt.plot(test.T)
plt.show()

I get this instead

enter image description here

The data streams are thus being superimposed on top of each other rather than transposing the array. I was expecting the whole thing to just get flipped as so x --> y and y --> x. Perhaps what I want is not transpose. So the data is plotted horizontally now, and I just want to plot i vertically instead.

Hence, where am I going wrong? I have clearly misunderstood something very basic.

Upvotes: 1

Views: 1621

Answers (2)

Vinícius Figueiredo
Vinícius Figueiredo

Reputation: 6518

Generalising Astrid's answer, one can define a helper function to transpose the axis for any 1d-array plot, like this:

def transpose_plot(array):
    return plt.plot(array, range(len(array)))

Demo:

import numpy as np
import matplotlib.pyplot as plt

def transpose_plot(array):
    return plt.plot(array, range(len(array)))

test = np.random.rand(100,5)
transpose_plot(test)
plt.show()

Upvotes: 1

Astrid
Astrid

Reputation: 1936

Well this solved it...

plt.plot(test,range(100))
plt.show()

enter image description here

Upvotes: 2

Related Questions