22

Quick Code to Spruce Up Your Histograms & Scatterplots

 4 years ago
source link: https://www.tuicool.com/articles/Zf2qUnn
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Simple graphs are essential visual tools for data analysis. If you are starting to learn how to make visualizations in Python, there are small adjustments to your graph parameters that will make them stand out. To get started, import the Pyplot and Seaborn libraries.

I. Choose Matplotlib colors that brighten up your graph.

3qU7Rnb.png!web

II. Outline your histogram bins with a chosen edgecolor.

import seaborn as sns
from matplotlib import pyplot as plt
%matplotlib inlineplt.rcParams["patch.force_edgecolor"] = True
plt.figure(figsize=(8,8)) #adjust the size of your graphsns.distplot(df["glucose"], bins= 20,color ='tomato',
hist_kws=dict(edgecolor="k", linewidth=2)) 
#modify colors, number of bins, and linewidth for custom looks

7NRnyqV.png!web

III. Create a grid of subplots for a quick visual overview.

sns.set_style('darkgrid',{'axes.edgecolor': '.9'},)
f, ax = plt.subplots(2,3,figsize = (16,7))
plt.rcParams["patch.force_edgecolor"] = True

vis1 = sns.distplot(df["pregnancies"],bins=10,color='mediumturquoise',
hist_kws=dict(edgecolor="magenta", linewidth=2.5),ax= ax[0][0])vis2 =
sns.distplot(df["glucose"],bins=10,color='mediumturquoise', hist_kws=dict(edgecolor="magenta",linewidth=2.5),ax=ax[0][1])vis3 = sns.distplot(df["bloodpressure"],bins=10,color='mediumturquoise', hist_kws=dict(edgecolor="magenta", linewidth=2.5),ax=ax[0][2])vis4 = sns.distplot(df["skinthickness"],bins=10,color='mediumturquoise', hist_kws=dict(edgecolor="magenta", linewidth=2.5), ax=ax[1][0])vis5 =
sns.distplot(df["insulin"],bins=10,color='mediumturquoise', hist_kws=dict(edgecolor="magenta", linewidth=2.5),ax=ax[1][1])vis6 =
sns.distplot(df["bmi"],bins=10,color='mediumturquoise', hist_kws=dict(edgecolor="magenta", linewidth=2.5),ax=ax[1][2])

UnM3eyB.png!web

IV. Choose marker shapes , sizes, and colormaps to increase the readability of scatterplots and regression plots. Determine a “hue” variable input that will add clarity to each data point or use “size” in the same way.

plt.figure(figsize = (8,8))ax = sns.scatterplot(x = df.insulin, y = df.glucose, hue= df.bmi, size=df.bmi, sizes=(0,200), marker = ‘h’, palette=’plasma’, data=df)

MRRjieJ.png!web

plt.figure(figsize = (8,8))ax = sns.scatterplot(x = df.insulin, y = df.glucose, style=df.diabetes, hue=df.diabetes,palette= ‘husl’,
markers=[‘D’,’*’],size= df.diabetes,sizes=(200,100), data=df)

fqEBfun.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK