Why matplotlib can't plot a histogram from a list of tensors?

Viewed 414

I have a list of tensors:

data = [tensor(0.1647),tensor(0.1662),tensor(0.1650),tensor(0.1645),tensor(0.1683),tensor(0.1683),tensor(0.1648),tensor(0.1694),tensor(0.5016),tensor(0.5059),tensor(0.5031),tensor(0.5069),tensor(1.0047),tensor(0.9966),tensor(0.4958),tensor(0.9984),tensor(0.1664),tensor(0.1725),tensor(0.5011),tensor(0.1679),tensor(0.1694),tensor(0.5003),tensor(0.1672),tensor(0.4957),tensor(0.5080),tensor(0.5047),tensor(0.4956),tensor(0.5012),tensor(0.4978),tensor(0.4975),tensor(0.9926)]

I am trying to plot a histogram using

import matplotlib.pyplot as plt
plt.figure(figsize=(12,12))
plt.hist(data, bins=5)
plt.show();

The results are really weird:

enter image description here

However, if I'll change the list of tensors as follows:

new_data = [i.item() for i in flag_syn_values]

>>> new_data = [0.16471165418624878,0.16618900001049042,0.16499125957489014,0.16447265446186066,0.1683468520641327,0.16827784478664398,0.16477319598197937,0.16940128803253174,0.5015971064567566,0.5058760046958923,0.5030592679977417,0.5068832039833069,1.0046963691711426,0.9966360330581665,0.4957870543003082,0.9984496831893921,0.16643814742565155,0.17246568202972412,0.5011343955993652,0.16787128150463104,0.16941896080970764,0.5003153085708618,0.16719254851341248,0.4957270622253418,0.5079832673072815,0.5047211647033691,0.4956021308898926,0.5012259483337402,0.4977755844593048,0.49753040075302124,0.9925762414932251]

I can plot it normally:

plt.figure(figsize=(12,12))
plt.hist(new_data, bins=5)
plt.show();

enter image description here

1 Answers

Your first example corresponds to the "sequence of (n,) arrays" case for the x parameter in the docs: In this case you obtain "... or ([n0, n1, ...], bins, [patches0, patches1, ...]) if the input contains multiple data". In other words: You are getting many overlaid histograms, each with a single data point. This is exactly what your first plot is showing. The solution to this will always be some flattening of the data, as you do in your second case.

Related