OpenSesame
Rapunzel Code Editor
DataMatrix
Support forum
Python Tutorials
MindProbe
Python videos

Plotting: Exercises and Solutions

Plotting heart-rate distributions in subplots

Exercise

Use this dataset from Moore, McCabe, & Craig to create a two-by-two plot, and in each subplot show the distribution of heart rate for one combination of gender and group of participants (Men Runners, Men Control, Female Runners, Female Control).

Solution

from matplotlib import pyplot as plt
import seaborn as sns
from datamatrix import io, operations as ops

dm = io.readtxt('data/heartrate.csv')
plt.subplot(2, 2, 1)
plt.subplots_adjust(wspace=.4, hspace=.8)
plt.title('Female Runners')
dm_female_runner = (dm.Gender == 'Female') & (dm.Group == 'Runners')
sns.distplot(dm_female_runner['Heart Rate'])
plt.subplot(2, 2, 2)
plt.title('Female Control')
dm_female_control = (dm.Gender == 'Female') & (dm.Group == 'Control')
sns.distplot(dm_female_control['Heart Rate'])
plt.subplot(2, 2, 3)
plt.title('Male Runners')
dm_male_runner = (dm.Gender == 'Male') & (dm.Group == 'Runners')
sns.distplot(dm_male_runner['Heart Rate'])
plt.subplot(2, 2, 4)
plt.title('Male Control')
dm_male_control = (dm.Gender == 'Male') & (dm.Group == 'Control')
sns.distplot(dm_male_control['Heart Rate'])
plt.show()

Output:

<string>:13: UserWarning: 

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
https://gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751

â ¦ Generating...<string>:17: UserWarning: 

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
https://gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751

â ¦ Generating...<string>:21: UserWarning: 

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
https://gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751

â § Generating...<string>:25: UserWarning: 

`distplot` is a deprecated function and will be removed in seaborn v0.14.0.

Please adapt your code to use either `displot` (a figure-level function with
similar flexibility) or `histplot` (an axes-level function for histograms).

For a guide to updating your code to use the new functions, please see
https://gist.github.com/mwaskom/de44147ed2974457ad6372750bbe5751

â § Generating...

You're done with this section!

Continue with Plotting >>