What is an Lmplot() in Seaborn ? With Example
It is a combination of Scatter Plot and Regression Line.
A regression line is simply a single line that best fits the data (in terms of having the smallest overall distance from the line to the points)
Let’s Start Exploratory Data Analysis on Flights Dataset using Seaborn Library of Python Pandas
import seaborn as sns
flights = sns.load_dataset('flights')
flights
year | month | passengers | |
---|---|---|---|
0 | 1949 | January | 112 |
1 | 1949 | February | 118 |
2 | 1949 | March | 132 |
3 | 1949 | April | 129 |
4 | 1949 | May | 121 |
5 | 1949 | June | 135 |
6 | 1949 | July | 148 |
7 | 1949 | August | 148 |
8 | 1949 | September | 136 |
9 | 1949 | October | 119 |
10 | 1949 | November | 104 |
11 | 1949 | December | 118 |
12 | 1950 | January | 115 |
13 | 1950 | February | 126 |
14 | 1950 | March | 141 |
15 | 1950 | April | 135 |
16 | 1950 | May | 125 |
17 | 1950 | June | 149 |
18 | 1950 | July | 170 |
19 | 1950 | August | 170 |
20 | 1950 | September | 158 |
21 | 1950 | October | 133 |
22 | 1950 | November | 114 |
23 | 1950 | December | 140 |
24 | 1951 | January | 145 |
25 | 1951 | February | 150 |
26 | 1951 | March | 178 |
27 | 1951 | April | 163 |
28 | 1951 | May | 172 |
29 | 1951 | June | 178 |
… | … | … | … |
114 | 1958 | July | 491 |
115 | 1958 | August | 505 |
116 | 1958 | September | 404 |
117 | 1958 | October | 359 |
118 | 1958 | November | 310 |
119 | 1958 | December | 337 |
120 | 1959 | January | 360 |
121 | 1959 | February | 342 |
122 | 1959 | March | 406 |
123 | 1959 | April | 396 |
124 | 1959 | May | 420 |
125 | 1959 | June | 472 |
126 | 1959 | July | 548 |
127 | 1959 | August | 559 |
128 | 1959 | September | 463 |
129 | 1959 | October | 407 |
130 | 1959 | November | 362 |
131 | 1959 | December | 405 |
132 | 1960 | January | 417 |
133 | 1960 | February | 391 |
134 | 1960 | March | 419 |
135 | 1960 | April | 461 |
136 | 1960 | May | 472 |
137 | 1960 | June | 535 |
138 | 1960 | July | 622 |
139 | 1960 | August | 606 |
140 | 1960 | September | 508 |
141 | 1960 | October | 461 |
142 | 1960 | November | 390 |
143 | 1960 | December | 432 |
144 rows × 3 columns
sns.lmplot(x='year', y='passengers', data=flights)
<seaborn.axisgrid.FacetGrid at 0x14ef204f208>
sns.lmplot(x='year', y='passengers', data=flights, hue='month')
<seaborn.axisgrid.FacetGrid at 0x14ef3306cc0>
sns.lmplot(x='year', y='passengers', data=flights, hue='month', palette='Set2', fit_reg=False)
<seaborn.axisgrid.FacetGrid at 0x14ef49d8470>
# Selecting Data for June Only
june = flights[(flights['month']=='June') | (flights['month']=='July') ]
sns.lmplot(x='year', y='passengers', data=june, hue='month', palette='Set2', fit_reg=True)
<seaborn.axisgrid.FacetGrid at 0x14ef4c8e898>