First, we need to load a few libraries:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
Dataset
Since scatter plot are made for visualizing relationships between two numerical variables, we need a dataset that contains at least two numerical columns.
Here, we will use the iris
dataset that we load directly from the gallery:
path = 'https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/iris.csv'
df = pd.read_csv(path)
df.head()
sepal_length sepal_width petal_length petal_width species 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa Color and width
You can custom the appearance of the regression fit in a scatterplot built with seaborn thanks to the line_kws
argument.
This argument accepts a dictionary of argument names and their values. These values will be exclusively used to style the line. For example, line_kws={"color" : "red", "linewidth" : 1.5}
will render the line red and with a width of 1.5.
fig, ax = plt.subplots(figsize=(8, 6))
sns.regplot(
x=df["sepal_length"],
y=df["sepal_width"],
line_kws={"color": "red", "linewidth": 1.5},
ax=ax
)
plt.show()
Opacity
You can also custom the opacity of the line with the alpha
value:
fig, ax = plt.subplots(figsize=(8, 6))
sns.regplot(
x=df["sepal_length"],
y=df["sepal_width"],
line_kws={
"color": "r",
"alpha": 0.4
},
ax=ax
)
plt.show()
Line width and style
You can also custom the line width and style with the linewidth
and linestyle
values:
fig, ax = plt.subplots(figsize=(8, 6))
sns.regplot(
x=df["sepal_length"],
y=df["sepal_width"],
line_kws={
"color": "darkred",
"alpha": 0.4,
"lw": 5,
"ls": "--"
},
ax=ax
)
plt.show()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4