shuaf98
shuaf98

Reputation: 55

Issue plotting a simple tensor with Torch

Just beginning to use Pyorch, and I am trying to plot a very simple, 1-D array Tensor onto a histogram with Matplotlib.

 torch.manual_seed(8436) 
 a = torch.Tensor(1000) 

 a.normal_(0, 2.) #This will fill our array with a normal distribution
 plt.hist(a);

However, the result is strange..., and just consists of a bunch of vertical, multicolored lines.

The result I am supposed to get, which I do when entering:

plt.hist(a.numpy())

is the normal histogram.

Thanks in advance for any help!

Upvotes: 0

Views: 314

Answers (1)

shuaf98
shuaf98

Reputation: 55

ok, it seems to me that here is the reason:

cbook._reshape_2D is used to preprocess the data coming into plt.hist . in 3.4.3 it returns a list of arrays with only one element each, which obviously produces the wrong image above.

in 3.2.2 , however, it returns a list with one 1D array, basically a NumPy version of the tensor we provided. and this one is plotted as expected.

I downgraded the package and it worked. Would be interested to hear other solutions.

Upvotes: 1

Related Questions