2017-01-30 4 views
3

figのタイトルとサブプロットのタイトルの間に空白が多くなるように、以下の円グラフのサブプロットをフォーマットするにはどうすればよいでしょうか。理想的には、サブプロットのタイトルは、実際の円グラフ自体のより近くにあります。Matplotlibサブプロットタイトル、Figureのタイトルフォーマット

これを有効にするかもしれないドキュメントで何かを見つけることができないかもしれませんが、私はmatplotlibを初めて使っています。

matplotlib figure

import matplotlib.pyplot as plt 
import pandas as pd 
from pandas import DataFrame, Series 

m = {"Men" : {"Yes": 2, "No": 8}} 
w = {"Women": {"Yes": 3, "No": 7}} 
data = {**m, **w} 
df = DataFrame(data) 

fig, axes = plt.subplots(1, len(df.columns)) 
fig.suptitle("Would you prefer to work from home?", fontsize=18) 
logging.debug("fig.axes: {}".format(fig.axes)) 

for i, ax in enumerate(fig.axes): 
    col = df.ix[:, i] 
    ax = fig.axes[i] 
    pcnt_col = col/col.sum() * 100 
    ax.set_title("{} (n={})".format(pcnt_col.name, col.sum())) 
    ax.pie(pcnt_col.values, labels=pcnt_col.index, 
        autopct="%1.1f%%", startangle=90) 
    ax.axis("equal") 
    plt.legend(loc="lower right", title="Answer", fancybox=True, 
       ncol=1, shadow=True) 
plt.show() 

答えて

5

使用subplots_adjustもかかわらず、凡例をシフトしているように見える2

plt.subplots_adjust(top=0.75) 

import matplotlib.pyplot as plt 
import pandas as pd 
from pandas import DataFrame, Series 

m = {"Men" : {"Yes": 2, "No": 8}} 
w = {"Women": {"Yes": 3, "No": 7}} 
data = {**m, **w} 
df = DataFrame(data) 

fig, axes = plt.subplots(1, len(df.columns)) 
fig.suptitle("Would you prefer to work from home?", fontsize=18) 
logging.debug("fig.axes: {}".format(fig.axes)) 

for i, ax in enumerate(fig.axes): 
    col = df.ix[:, i] 
    ax = fig.axes[i] 
    pcnt_col = col/col.sum() * 100 
    ax.set_title("{} (n={})".format(pcnt_col.name, col.sum())) 
    ax.pie(pcnt_col.values, labels=pcnt_col.index, 
        autopct="%1.1f%%", startangle=90) 
    ax.axis("equal") 
    plt.legend(loc="lower right", title="Answer", fancybox=True, 
       ncol=1, shadow=True) 
plt.subplots_adjust(top=0.55) 
plt.show() 

enter image description here

+0

を分離する。.. – SiHa

+0

@SiHaの金額は異なる場合があります...凡例はOPコードのOPと同じ位置にありませんでした。 – piRSquared

+0

OK、fair enough :) – SiHa

関連する問題