densities - introducing Seaborn

pull/38/head
Jen Looper 4 years ago
parent 266c173126
commit 9efd99d77c

@ -9,6 +9,14 @@ In this lesson, you will use three different libraries to learn how to create in
An excellent library to create both simple and sophisticated plots and charts of various kinds is [Matplotlib](https://matplotlib.org/stable/index.html). In general terms, the process of plotting data using these libraries includes identifying the parts of your dataframe that you want to target, performing any transforms on that data necessary, assigning its x and y axis values, deciding what kind of plot to show, and then showing the plot. Matplotlib offers a large variety of visualizations, but for this lesson, let's focus on the ones most appropriate for visualizing quantity: line charts, scatterplots, and bar plots.
> ✅ Use the best chart to suit your data's structure and the story you want to tell.
> - To analyze trends over time: line
> - To compare values: bar, column, pie, scatterplot
> - To show how parts relate to a whole: pie
> - To show distribution of data: scatterplot, bar
> - To show trends: line, column
> - To show relationships between values: line, scatterplot, bubble
If you have a dataset and need to discover how much of a given item is included, one of the first tasks you have at hand will be to inspect its values.
✅ There are very good 'cheat sheets' available for Matplotlib [here](https://github.com/matplotlib/cheatsheets/blob/master/cheatsheets-1.png) and [here](https://github.com/matplotlib/cheatsheets/blob/master/cheatsheets-2.png).
@ -134,7 +142,7 @@ This bar chart, however, is unreadable because there is too much non-grouped dat
Filter your data to include only the bird's category.
✅ Notice that you use Pandas to manage the data, and then let Matplotlib do the charting.
✅ Notice that you use Pandas to manage the data, and then let Matplotlib do the charting.
Since there are many categories, you can display this chart vertically and tweak its height to account for all the data:
@ -175,7 +183,7 @@ plt.barh(category, minLength)
plt.show()
```
In this plot you can see the range, per category, of the Minimum Length and Maximum length of a given bird category. You can safely say, that, given this data, the bigger the bird, the larger its length range. Fascinating!
In this plot you can see the range, per category, of the Minimum Length and Maximum length of a given bird category. You can safely say that, given this data, the bigger the bird, the larger its length range. Fascinating!
![superimposed values](images/superimposed.png)

File diff suppressed because one or more lines are too long

@ -1,8 +1,178 @@
# Visualizing Distributions
In the previous lesson, you learned some interesting facts about a dataset about the birds of Minnesota. You found some erroneous data by visualizing outliers and looked at the differences between bird categories by their maximum length.
## Pre-Lecture Quiz
[Pre-lecture quiz]()
## Explore the birds dataset
Another way to dig into data is by looking at its distribution, or how the data is organized along an axis. Perhaps, for example, you'd like to learn about the general distribution, for this dataset, of maximum wingspan or maximum body mass for the birds of Minnesota.
Let's discover some facts about the distributions of data in this dataset. In the _notebook.ipynb_ file at the root of this lesson folder, import Pandas, Matplotlib, and your data:
```python
import pandas as pd
import matplotlib.pyplot as plt
birds = pd.read_csv('../data/birds.csv')
birds.head()
```
In general, you can quickly look at the way data is distributed by using a scatter plot as we did in the previous lesson:
```python
birds.plot(kind='scatter',x='MaxLength',y='Order',figsize=(12,8))
plt.title('Max Length per Order')
plt.ylabel('Order')
plt.xlabel('Max Length')
plt.show()
```
This gives an overview of the general distribution of body length per bird Order, but it is not the optimal way to display true distributions. That task is usually handled by creating a Histogram.
## Working with histograms
Matplotlib offers very good ways to visualize data distribution using Histograms. This type of chart is like a bar chart where the distribution can be seen via a rise and fall of the bars. To build a histogram, you need numeric data.To build a Histogram, you can plot a chart defining the kind as 'hist' for Histogram. This chart show the distribution of MaxBodyMass for the entire dataset's range of numeric data. By dividing the array of data it is given into smaller bins, it can display the distribution of the data's values:
```python
birds['MaxBodyMass'].plot(kind = 'hist', bins = 10, figsize = (12,12))
plt.show()
```
![distribution over the entire dataset](images/dist1.png)
As you can see, most of the 400+ birds in this dataset fall in the range of under 2000 for their Max Body Mass. Gain more insight on the data by changing the `bins` parameter to a higher number, something like 30:
```python
birds['MaxBodyMass'].plot(kind = 'hist', bins = 30, figsize = (12,12))
plt.show()
```
![distribution over the entire dataset with larger bins param](images/dist2.png)
This chart shows the distribution in a bit more granular fashion. A chart less skewed to the left could be created by ensuring that you only select data within a given range:
Filter your data to get only those birds whose body mass is under 60, and show 40 `bins`:
```python
filteredBirds = birds[(birds['MaxBodyMass'] > 1) & (birds['MaxBodyMass'] < 60)]
filteredBirds['MaxBodyMass'].plot(kind = 'hist',bins = 40,figsize = (12,12))
plt.show()
```
![filtered histogram](images/dist3.png)
✅ Try some other filters and data points. To see the full distribution of the data, remove the `['MaxBodyMass']` filter to show labeled distributions.
The histogram offers some nice color and labeling enhancements to try as well:
Create a 2D histogram to compare the relationship between two distributions. Let's compare `MaxBodyMass` vs. `MaxLength`. Matplotlib offers a built-in way to show convergence using brighter colors:
```python
x = filteredBirds['MaxBodyMass']
y = filteredBirds['MaxLength']
fig, ax = plt.subplots(tight_layout=True)
hist = ax.hist2d(x, y)
```
There appears to be an expected correlation between these two elements along an expected axis, with one particularly strong point of convergence:
![2D plot](images/2D.png)
Histograms work well by default for numeric data. What if you need to see distributions according to text data?
## Explore the dataset for distributions using text data
This dataset also includes good information about the bird category and its genus, species and family as well as its conservation status. Let's dig into this conservation information. What is the distribution of the birds according to their conservation status?
> ✅ In the dataset, several acronyms are used to describe conservation status. These acronyms come from the [IUCN Red List Categories](https://www.iucnredlist.org/), an organization that catalogue species status.
>
> - CR: Critically Endangered
> - EN: Endangered
> - EX: Extinct
> - LC: Least Concern
> - NT: Near Threatened
> - VU: Vulnerable
These are text-based values so you will need to do a transform to create a histogram. Using the filteredBirds dataframe, display its conservation status alongside its Minimum Wingspan. What do you see?
```python
x1 = filteredBirds.loc[filteredBirds.ConservationStatus=='EX', 'MinWingspan']
x2 = filteredBirds.loc[filteredBirds.ConservationStatus=='CR', 'MinWingspan']
x3 = filteredBirds.loc[filteredBirds.ConservationStatus=='EN', 'MinWingspan']
x4 = filteredBirds.loc[filteredBirds.ConservationStatus=='NT', 'MinWingspan']
x5 = filteredBirds.loc[filteredBirds.ConservationStatus=='VU', 'MinWingspan']
x6 = filteredBirds.loc[filteredBirds.ConservationStatus=='LC', 'MinWingspan']
kwargs = dict(alpha=0.5, bins=20)
plt.hist(x1, **kwargs, color='red', label='Extinct')
plt.hist(x2, **kwargs, color='orange', label='Critically Endangered')
plt.hist(x3, **kwargs, color='yellow', label='Endangered')
plt.hist(x4, **kwargs, color='green', label='Near Threatened')
plt.hist(x5, **kwargs, color='blue', label='Vulnerable')
plt.hist(x6, **kwargs, color='gray', label='Least Concern')
plt.gca().set(title='Conservation Status', ylabel='Max Body Mass')
plt.legend();
```
![wingspan and conservation collation](images/histogram-conservation.png)
There doesn't seem to be a good correlation between minimum wingspan and conservation status. Test other elements of the dataset using this method. You can try different filters as well. Do you find any correlation?
## Density plots
You may have noticed that the histograms we have looked at so far are 'stepped' and do not flow smoothy in an arc. To show a smoother density chart, you can try a density plot.
To work with density plots, familiarize yourself with a new plotting library, [Seaborn](https://seaborn.pydata.org/generated/seaborn.kdeplot.html).
Loading Seaborn, try a basic density plot:
```python
import seaborn as sns
import matplotlib.pyplot as plt
sns.kdeplot(filteredBirds['MinWingspan'])
plt.show()
```
![Density plot](images/density1.png)
You can see how the plot echoes the previous one for Minimum Wingspan data; it's just a bit smoother. According to Seaborn's documentation, "Relative to a histogram, KDE can produce a plot that is less cluttered and more interpretable, especially when drawing multiple distributions. But it has the potential to introduce distortions if the underlying distribution is bounded or not smooth. Like a histogram, the quality of the representation also depends on the selection of good smoothing parameters." [source](https://seaborn.pydata.org/generated/seaborn.kdeplot.html) In other words, outliers as always will make your charts behave badly.
If you wanted to revisit that jagged MaxBodyMass line in the second chart you built, you could smooth it out very well by recreating it using this method:
```python
sns.kdeplot(filteredBirds['MaxBodyMass'])
plt.show()
```
![smooth bodymass line](images/density2.png)
If you wanted a smooth, but not too smooth line, edit the `bw_adjust` parameter
```python
sns.kdeplot(filteredBirds['MaxBodyMass'], bw_adjust=.2)
plt.show()
```
![less smooth bodymass line](images/density3.png)
✅ Read about the parameters available for this type of plot and experiment!
This type of chart offers beautifully explanatory visualizations. With a few lines of code, for example, you can show the max body mass density per bird Order:
```python
sns.kdeplot(
data=filteredBirds, x="MaxBodyMass", hue="Order",
fill=True, common_norm=False, palette="crest",
alpha=.5, linewidth=0,
)
```
![bodymass per order](images/density4.png)
You can also map the density of several variables in one chart. Text the MaxLength and MinLength of a bird compared to their conservation status:
```python
sns.kdeplot(data=filteredBirds, x="MinLength", y="MaxLength", hue="ConservationStatus")
```
![multiple densities, superimposed](images/multi.png)
Perhaps it's worth researching whether the cluster of 'Vulnerable' birds according to their lengths is meaningful or not.
## 🚀 Challenge

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

File diff suppressed because one or more lines are too long

@ -373,10 +373,9 @@ Savannah sparrow,Passerculus sandwichensis,New World sparrows,Passeriformes,Pass
Song sparrow,Melospiza melodia,New World sparrows,Passeriformes,Passerellidae,Melospiza,LC,11,18,11.9,53,18,25.4
Lincoln's sparrow,Melospiza lincolnii,New World sparrows,Passeriformes,Passerellidae,Melospiza,LC,13,15,17,19,19,22
Swamp sparrow,Melospiza georgiana,New World sparrows,Passeriformes,Passerellidae,Melospiza,LC,12,15,15,23,18,19
Green-tailed towhee,Pipilo chlorurus,New World sparrows,Passeriformes,Passerellidae,Pipilo,LC,18.4,18.4,29,29,,
Spotted towhee,Pipilo maculatus,New World sparrows,Passeriformes,Passerellidae,Pipilo,LC,17,21,22,49,28,28
Eastern towhee,Pipilo erythrophthalmus,New World sparrows,Passeriformes,Passerellidae,Pipilo,LC,17.3,23,32,53,20,30
Yellow-breasted chat,Icteria virens,Yellow-breasted chat,Passeriformes,Icteriidae,Icteria,LC,17,19.1,20.2,33.8,23,27
Yellow-breasted chat,Icteria virens,Yellow-breasted chat,Passeriformes,Icteridae,Icteria,LC,17,19.1,20.2,33.8,23,27
Yellow-headed blackbird,Xanthocephalus xanthocephalus,Troupials/Allies,Passeriformes,Icteridae,Xanthocephalus,LC,21,26,44,100,42,44
Bobolink,Dolichonyx oryzivorus,Troupials/Allies,Passeriformes,Icteridae,Dolichonyx,LC,15,21,29,56,27,27
Eastern meadowlark,Sturnella magna,Troupials/Allies,Passeriformes,Icteridae,Sturnella,NT,19,28,76,150,35,40

1 Name ScientificName Category Order Family Genus ConservationStatus MinLength MaxLength MinBodyMass MaxBodyMass MinWingspan MaxWingspan
373 Song sparrow Melospiza melodia New World sparrows Passeriformes Passerellidae Melospiza LC 11 18 11.9 53 18 25.4
374 Lincoln's sparrow Melospiza lincolnii New World sparrows Passeriformes Passerellidae Melospiza LC 13 15 17 19 19 22
375 Swamp sparrow Melospiza georgiana New World sparrows Passeriformes Passerellidae Melospiza LC 12 15 15 23 18 19
Green-tailed towhee Pipilo chlorurus New World sparrows Passeriformes Passerellidae Pipilo LC 18.4 18.4 29 29
376 Spotted towhee Pipilo maculatus New World sparrows Passeriformes Passerellidae Pipilo LC 17 21 22 49 28 28
377 Eastern towhee Pipilo erythrophthalmus New World sparrows Passeriformes Passerellidae Pipilo LC 17.3 23 32 53 20 30
378 Yellow-breasted chat Icteria virens Yellow-breasted chat Passeriformes Icteriidae Icteridae Icteria LC 17 19.1 20.2 33.8 23 27
379 Yellow-headed blackbird Xanthocephalus xanthocephalus Troupials/Allies Passeriformes Icteridae Xanthocephalus LC 21 26 44 100 42 44
380 Bobolink Dolichonyx oryzivorus Troupials/Allies Passeriformes Icteridae Dolichonyx LC 15 21 29 56 27 27
381 Eastern meadowlark Sturnella magna Troupials/Allies Passeriformes Icteridae Sturnella NT 19 28 76 150 35 40
Loading…
Cancel
Save