Plt.hist(some_data) vs. plt.hist(data = some_data)

Hello,

what is the mechanical difference between plt.hist(some_data) vs. plt.hist(data = some_data) ?
when I input plt.hist(data = some_data) error is thrown say x is missing value when I put plt.hist(data = some_data, x = some_value_in_some_data) another error is thrown: len() of unsized object

the expression: plt.hist(some_data) has worked for me so far, but I would like to know what is changed by putting data = some_data as opposed to mere some_data.

Thanks

plt.hist() takes two parameters: an array (you can import Numpy for this too) and the number of bins. (there’s a bunch of other parameters and keyword argument options too for further customization). See: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html

And:
https://matplotlib.org/stable/gallery/statistics/hist.html

More:
https://pythonforundergradengineers.com/histogram-plots-with-matplotlib-and-python.html

And for Seaborn it’s just a little bit different:
https://seaborn.pydata.org/generated/seaborn.histplot.html

Where you can supply the data and either x or y values.

2 Likes

When in doubt the docs are always a good look-in:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html
The data argument makes it convenient to pass things like dictionaries or dataframes (indexable containers).

For example-

my_data = {'A': [1, 2, 3,], 'B': [-4, -5, -6,],}
plt.hist(x='A', data=my_data)

It’s a bit easier that way, they’re not immediately interchangeable items though.

2 Likes