简体   繁体   中英

How to plot density plot by label (categorical variable) for each numeric column?

I have tried to use the mines and rocks data ( http://archive.ics.uci.edu/ml/datasets/connectionist+bench+(sonar,+mines+vs.+rocks) ) to do EDA. I have put the following code that can plot the density plot for each numeric column.

Is there a way to plot the same chart for each numeric variable in the data set but with two lines in each density plot based on if it is M or R (the last column). Therefore we can see which variable shows different distribution for the label M vs R.

import pandas as pd

# import file
file = 'https://archive.ics.uci.edu/ml/machine-learning- 
databases/undocumented/connectionist-bench/sonar/sonar.all-data'
mr_df = pd.read_table(file, sep=',', header=None)

mr_df.plot(kind='density', subplots=True, layout=(8,8), sharex=False, legend=False, fontsize=1, figsize=(12,12))
plt.savefig('density plot.png')

在此处输入图片说明

plt.subplots(nrows=8, ncols=8, figsize=(12,12))
for i in range(1, 61):
    plt.subplot(8, 8, i)
    mr_df.loc[mr_df[60] == 'R', i-1].plot(kind='density')
    mr_df.loc[mr_df[60] == 'M', i-1].plot(kind='density')

plt.subplot_tool() # allows easy adjustment of the subplot spacing

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM