filmov
tv
Python Tutorial: Plotting a histogram
![preview_player](https://i.ytimg.com/vi/pq5VY29cD1o/maxresdefault.jpg)
Показать описание
---
We saw in the last video that a histogram can be a useful plot to generate when exploring a data set. Let's go over how we can create one.
We are interested in the fraction of the vote that went to Barack Obama in each county. We can plot this as a histogram using
the matplotlib dot pyplot module's hist function. We pass it the dem_share column of the DataFrame. We could have also passed a NumPy array with the same data, and it works just fine. In fact, for this course and its sequel, you can use DataFrames and NumPy arrays interchangeably.
Note that plt dot hist returns three arrays that I am not interested in; I only want the plot. I therefore assign a dummy variable called "underscore" to them, which is common practice in Python.
After creating the histogram, we label the axes.
Always label your axes, for histograms or any other kind of plot. Otherwise no one can know what it is you are plotting.
You probably didn't notice, but this plot looks slightly different than the first plot I showed. You can see it if you look at them side-by-side.
They are different because they have different binning. In the plot at left, we have ten bins that were automatically generated by the default settings of plt dot hist, and I set up the bins on the right myself.
I specified where the edges of the bars of the histogram are, the bin edges, and use the bins keyword argument to pass that to plt dot hist.
You can also specify a number of bins, say 20, using the bins keyword argument, and Matplotlib will automatically generate 20 evenly spaced bins.
Now, the plots we've made so far are stylized with Matplotlib's default settings. I prefer to use the default settings
of Seaborn, an excellent matplotlib-based statistical data visualization package written primarily by Michael Waskom.
We import it as sns, as is traditionally done. Upon import, we can set the style to be Seaborn's default, using the sns dot set function.
This results in nicely formatted plots. Beyond this stylistic functionality, Seaborn offers useful plotting functions that we will explore in the next video.
Before we do that, let's practice making histograms with some exercises.
#PythonTutorial #DataCamp #Statistical #Thinking #Python #Plotting #histogram