%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset("iris")
iris_copy = iris.copy()
iris_copy["sepal_length_round"] = iris_copy.sepal_length.round()
# http://matplotlib.org/users/customizing.html
plot_context = {
'figure.figsize': (10, 8),
'font.size': 18,
'font.stretch': 18,
'figure.dpi': 100,
'axes.labelsize': 18,
'axes.titlesize': 20,
'xtick.labelsize': 15,
'ytick.labelsize': 20,
'legend.fontsize': 15,
'lines.markersize': 15,
}
# それっぽいものがあるかは下記で調べた
{k for k in plt.rcParams.keys() if "text" in k} | {k for k in plt.rcParams.keys() if "size" in k}
# http://statsmodels.sourceforge.net/stable/generated/statsmodels.graphics.mosaicplot.mosaic.html
from statsmodels.graphics.mosaicplot import mosaic
with plt.rc_context(plot_context):
fig, rects = mosaic(iris_copy, ["sepal_length_round", "species"], gap=0.01)
plt.savefig("mosaic_cm.png", dpi=200)
# 結果のDataFrame Indexが示す値は何か書いてない…
# 0, 1, : ??
# 2: 横軸を全体とした時の構成比率
# 3: 各横軸内での構成比率
pd.DataFrame(rects)
# 各IndexでのPlot
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
index = ["sepal_length_round", "species"]
mosaic(iris_copy, index, gap=0.01, ax=axes[0])
mosaic(iris_copy, index[::-1], gap=0.01, ax=axes[1])
plt.tight_layout()