Saturday, September 21, 2024
Google search engine
HomeLanguagesScatter Plot with Marginal Histograms in Python with Seaborn

Scatter Plot with Marginal Histograms in Python with Seaborn

Prerequisites: SeabornĀ 

Scatter Plot with Marginal Histograms is basically a joint distribution plot with the marginal distributions of the two variables. In data visualization, we often plot the joint behavior of two random variables (bi-variate distribution) or any number of random variables. But if data is too large, overlapping can be an issue. Hence, to distinguish between variables it is useful to have the probability distribution of each variable on the side along with the joint plot. This individual probability distribution of a random variable is referred to as its marginal probability distribution.Ā 

In seaborn, this is facilitated with jointplot(). It represents the bi-variate distribution using scatterplot() and the marginal distributions using histplot().Ā 

Approach

  • Import seaborn library
  • Load dataset of your choice
  • Use jointplot() on variables of your dataset

Example 1:Ā 

Python3




# importing and creating alias for seaborn
import seaborn as sns
Ā Ā 
# loading tips dataset
tips = sns.load_dataset("tips")
Ā Ā 
# plotting scatterplot with histograms for features total bill and tip.
sns.jointplot(data=tips, x="total_bill", y="tip")


Output :Ā 

<seaborn.axisgrid.JointGrid at 0x26203152688>

jointplot_with_histograms

Example 2: Using kind=ā€regā€ attribute you can add a linear regression fit and univariate KDE curves.

Python3




import seaborn as sns
Ā Ā 
tips = sns.load_dataset("tips")
Ā Ā 
# here "*" is used as a marker for scatterplot
sns.jointplot(data=tips, x="total_bill", y="tip", kind="reg", marker="*")


Output :

scatterplot with a linear regression fit

Example3: To add conditional colors to the scatterplot you can use hue attribute but it draws separate density curves (using kdeplot()) on the marginal axes.

Python3




import seaborn as sns
Ā Ā 
tips = sns.load_dataset("tips")
Ā Ā 
sns.jointplot(data=tips, x="total_bill", y="tip", hue="time")


Output :Ā 

scatterplot3

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?