1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
#!/usr/bin/env python
# DO NOT EDIT
# Autogenerated from the notebook plots_boxplots.ipynb.
# Edit the notebook and then sync the output with this file.
#
# flake8: noqa
# DO NOT EDIT
# # Box Plots
# The following illustrates some options for the boxplot in statsmodels.
# These include `violin_plot` and `bean_plot`.
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
# ## Bean Plots
# The following example is taken from the docstring of `beanplot`.
#
# We use the American National Election Survey 1996 dataset, which has
# Party
# Identification of respondents as independent variable and (among other
# data) age as dependent variable.
data = sm.datasets.anes96.load_pandas()
party_ID = np.arange(7)
labels = [
"Strong Democrat",
"Weak Democrat",
"Independent-Democrat",
"Independent-Independent",
"Independent-Republican",
"Weak Republican",
"Strong Republican",
]
# Group age by party ID, and create a violin plot with it:
plt.rcParams["figure.subplot.bottom"] = 0.23 # keep labels visible
plt.rcParams["figure.figsize"] = (10.0, 8.0) # make plot larger in notebook
age = [data.exog["age"][data.endog == id] for id in party_ID]
fig = plt.figure()
ax = fig.add_subplot(111)
plot_opts = {
"cutoff_val": 5,
"cutoff_type": "abs",
"label_fontsize": "small",
"label_rotation": 30,
}
sm.graphics.beanplot(age, ax=ax, labels=labels, plot_opts=plot_opts)
ax.set_xlabel("Party identification of respondent.")
ax.set_ylabel("Age")
# plt.show()
def beanplot(data, plot_opts={}, jitter=False):
"""helper function to try out different plot options"""
fig = plt.figure()
ax = fig.add_subplot(111)
plot_opts_ = {
"cutoff_val": 5,
"cutoff_type": "abs",
"label_fontsize": "small",
"label_rotation": 30,
}
plot_opts_.update(plot_opts)
sm.graphics.beanplot(data,
ax=ax,
labels=labels,
jitter=jitter,
plot_opts=plot_opts_)
ax.set_xlabel("Party identification of respondent.")
ax.set_ylabel("Age")
fig = beanplot(age, jitter=True)
fig = beanplot(age, plot_opts={"violin_width": 0.5, "violin_fc": "#66c2a5"})
fig = beanplot(age, plot_opts={"violin_fc": "#66c2a5"})
fig = beanplot(age,
plot_opts={
"bean_size": 0.2,
"violin_width": 0.75,
"violin_fc": "#66c2a5"
})
fig = beanplot(age, jitter=True, plot_opts={"violin_fc": "#66c2a5"})
fig = beanplot(age,
jitter=True,
plot_opts={
"violin_width": 0.5,
"violin_fc": "#66c2a5"
})
# ## Advanced Box Plots
# Based of example script `example_enhanced_boxplots.py` (by Ralf Gommers)
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
# Necessary to make horizontal axis labels fit
plt.rcParams["figure.subplot.bottom"] = 0.23
data = sm.datasets.anes96.load_pandas()
party_ID = np.arange(7)
labels = [
"Strong Democrat",
"Weak Democrat",
"Independent-Democrat",
"Independent-Independent",
"Independent-Republican",
"Weak Republican",
"Strong Republican",
]
# Group age by party ID.
age = [data.exog["age"][data.endog == id] for id in party_ID]
# Create a violin plot.
fig = plt.figure()
ax = fig.add_subplot(111)
sm.graphics.violinplot(
age,
ax=ax,
labels=labels,
plot_opts={
"cutoff_val": 5,
"cutoff_type": "abs",
"label_fontsize": "small",
"label_rotation": 30,
},
)
ax.set_xlabel("Party identification of respondent.")
ax.set_ylabel("Age")
ax.set_title("US national election '96 - Age & Party Identification")
# Create a bean plot.
fig2 = plt.figure()
ax = fig2.add_subplot(111)
sm.graphics.beanplot(
age,
ax=ax,
labels=labels,
plot_opts={
"cutoff_val": 5,
"cutoff_type": "abs",
"label_fontsize": "small",
"label_rotation": 30,
},
)
ax.set_xlabel("Party identification of respondent.")
ax.set_ylabel("Age")
ax.set_title("US national election '96 - Age & Party Identification")
# Create a jitter plot.
fig3 = plt.figure()
ax = fig3.add_subplot(111)
plot_opts = {
"cutoff_val": 5,
"cutoff_type": "abs",
"label_fontsize": "small",
"label_rotation": 30,
"violin_fc": (0.8, 0.8, 0.8),
"jitter_marker": ".",
"jitter_marker_size": 3,
"bean_color": "#FF6F00",
"bean_mean_color": "#009D91",
}
sm.graphics.beanplot(age,
ax=ax,
labels=labels,
jitter=True,
plot_opts=plot_opts)
ax.set_xlabel("Party identification of respondent.")
ax.set_ylabel("Age")
ax.set_title("US national election '96 - Age & Party Identification")
# Create an asymmetrical jitter plot.
ix = data.exog["income"] < 16 # incomes < $30k
age = data.exog["age"][ix]
endog = data.endog[ix]
age_lower_income = [age[endog == id] for id in party_ID]
ix = data.exog["income"] >= 20 # incomes > $50k
age = data.exog["age"][ix]
endog = data.endog[ix]
age_higher_income = [age[endog == id] for id in party_ID]
fig = plt.figure()
ax = fig.add_subplot(111)
plot_opts["violin_fc"] = (0.5, 0.5, 0.5)
plot_opts["bean_show_mean"] = False
plot_opts["bean_show_median"] = False
plot_opts["bean_legend_text"] = r"Income < \$30k"
plot_opts["cutoff_val"] = 10
sm.graphics.beanplot(
age_lower_income,
ax=ax,
labels=labels,
side="left",
jitter=True,
plot_opts=plot_opts,
)
plot_opts["violin_fc"] = (0.7, 0.7, 0.7)
plot_opts["bean_color"] = "#009D91"
plot_opts["bean_legend_text"] = r"Income > \$50k"
sm.graphics.beanplot(
age_higher_income,
ax=ax,
labels=labels,
side="right",
jitter=True,
plot_opts=plot_opts,
)
ax.set_xlabel("Party identification of respondent.")
ax.set_ylabel("Age")
ax.set_title("US national election '96 - Age & Party Identification")
# Show all plots.
# plt.show()
|