- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Plotting regression and residual plot in Matplotlib
To establish a simple relationship between the observations of a given joint distribution of a variable, we can create the plot for the regression model using Seaborn.
To fit the dataset using the regression model, we have to first import the necessary libraries in Python.
We will create plots for each regression model, (a) Linear Regression, (b) Polynomial Regression, and (c) Logistic Regression.
In this example, we will use the wine quality dataset which can be accessed from here, https://archive.ics.uci.edu/ml/datasets/wine+quality
Example
import matplotlib.pyplot as plt import seaborn as sns from scipy.stats import pearsonr sns.set(style="dark", color_codes=True) #import the dataset wine_quality = pd.read_csv('winequality-red.csv', delimiter=';') #Plotting Linear Regression R, p = pearsonr(wine_quality['fixed acidity'], wine_quality.pH) g1 = sns.regplot(x='fixed acidity', y='pH', data=wine_quality, truncate=True, ci=99, marker='D', scatter_kws={'color': 'r'}); textstr = '$\mathrm{pearson}\hspace{0.5}\mathrm{R}^2=%.2f$
$\mathrm{pval}=%.2e$ '% (R**2, p) props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) g1.text(0.55, 0.95, textstr, fontsize=14, va='top', bbox=props) plt.title('1. Linear Regression', size=15, color='b', weight='bold') #Let us Plot the Polynomial Regression plot for the wine dataset g2 = sns.regplot(x='fixed acidity', y='pH', data=wine_quality, order=2, ci=None, marker='s', scatter_kws={'color': 'skyblue'}, line_kws={'color': 'red'}); plt.title('2. Polynomial Regression', size=15, color='r', weight='bold') #Now plotting the Logistic Regression wine_quality['Q'] = wine_quality['quality'].map({'Low': 0, 'Med': 0, 'High':1}) g2 = sns.regplot(x='fixed acidity', y='Q', logistic=True, n_boot=750, y_jitter=.03, data=wine_quality, line_kws={'color': 'r'}) plt.show(); #Now plot the residual plot g3 = sns.residplot(x='fixed acidity', y='density', order=2, data=wine_quality, scatter_kws={'color': 'r', 'alpha': 0.5}); plt.show();
Running the above code will generate the output as,
Output
- Related Articles
- Plotting a masked surface plot using Python, Numpy and Matplotlib
- How to plot statsmodels linear regression (OLS) cleanly in Matplotlib?
- How to find residual variance of a linear regression model in R?
- Plotting power spectral density in Matplotlib
- Plotting profile histograms in Python Matplotlib
- Plotting multiple line graphs using Pandas and Matplotlib
- Plotting animated quivers in Python using Matplotlib
- Plotting histograms against classes in Pandas / Matplotlib
- How to find the degrees of freedom of residual from a regression model in R?
- Linear regression with Matplotlib/Numpy
- Plotting an imshow() image in 3d in Matplotlib
- Plotting distance arrows in technical drawing in Matplotlib
- How to create a residual plot in R with better looking aesthetics?
- Plotting grids across the subplots in Python Matplotlib
- Plotting scatter points with clover symbols in Matplotlib

Advertisements