NoName
NoName

Reputation: 10324

NumPy ndarray are plotted differently than equivalent lists?

I expected "b" to be plotted the same way as "a", but it's not:

import matplotlib.pyplot as plt
import numpy as np

figure, axes = plt.subplots(nrows = 2, ncols = 1)

a = [[1, 2, 2, 3, 3, 3], [1, 1, 1, 2, 2, 3]]
b = np.array(object = a)

axes[0].hist(x = a, bins = np.arange(start = 0, stop = 5, step = 1), density = True)
axes[1].hist(x = b, bins = np.arange(start = 0, stop = 5, step = 1), density = True)

figure.show()

enter image description here

Why?

Upvotes: 2

Views: 50

Answers (1)

user2357112
user2357112

Reputation: 280261

matplotlib.axes.Axes.hist just does that. It's weird.

Multiple data can be provided via x as a list of datasets of potentially different length ([x0, x1, ...]), or as a 2-D ndarray in which each column is a dataset. Note that the ndarray form is transposed relative to the list form.

If you pass a list of datasets, each row is a dataset. If you pass a 2D array, each column is a dataset.

Upvotes: 5

Related Questions