2016-12-02 7 views
2

プロットしているものの性質上、サブプロットはネストした表と似ています。 私は質問をはっきりと尋ねる方法がわからないので、代わりに写真をいくつか追加して問題を説明してほしい。Matplotlibのタイトルは2つの(または任意の数の)サブプロットの列になります

Matplotlib graphs with title and axes titles

私が欲しいもの:私が持っているもの

Matplotlib graphs with title and axes titles AND wanted sub-titles

電流(短縮)のコードは次のようになります。

fig, axes = plt.subplots(nrows=5, ncols=4) 
fig.suptitle(title, fontsize='x-large') 
data0.plot(x=data0.x, y=data0.y, ax=axes[0,0],kind='scatter') 
data1.plot(x=data1.x, y=data1.y, ax=axes[0,1],kind='scatter') 
axes[0,0].set_title('title 0') 
axes[0,1].set_title('title 1') 

私ができます」のタイトルを設定する方法を理解するxes [0,0]と[0,1]を一緒にします。私はドキュメントの中に何かを見つけることができません。私はこれを達成するためにラテックスのテーブルを騒がすのが好きではない。すべてのポインタ?

これまでに質問されている場合は、実際に問題の名前を付ける方法がわからないため、何も見つかりませんでした。

+0

、一切のテーブルがありません。テーブルはどこに行くべきですか? – ImportanceOfBeingErnest

+0

http://stackoverflow.com/questions/25239933/how-to-add-title-to-subplots-in-matplotlib – Dadep

+0

@ImportanceOfBeingErnestこれはテーブルに関するものではありません(?)。私はちょうど入れ子の考えを与えるためにテーブルの比較を行いました。私の写真を見せてくれてありがとう! – OfOurOwn

答えて

2

fig.suptitle()と軸(サブプロット)のタイトルをax.set_title()を使用して設定することは、かなり簡単です。中間列のタイトルを設定するには、実際にはビルドオプションはありません。

この問題を解決する1つの方法は、plt.figtext()を適切な位置に使用することです。そのタイトルのための追加のスペースを考慮する必要があります。 fig.subplots_adjustを使って、このfigtextの適切な位置を見つけてください。 以下の例では、軸のバウンディングボックスを使用して、中央の水平位置を見つけるためにタイトルがまたがっています。垂直位置は最良の推測です。あなたのイメージで

import matplotlib.pyplot as plt 
import numpy as np 

x = np.arange(10) 
y = np.random.rand(10,8) 

colors=["b", "g", "r", "violet"] 
fig, axes = plt.subplots(nrows=2, ncols=4, sharex=True, sharey=True, figsize=(8,5)) 
#set a figure title on top 
fig.suptitle("Very long figure title over the whole figure extent", fontsize='x-large') 
# adjust the subplots, i.e. leave more space at the top to accomodate the additional titles 
fig.subplots_adjust(top=0.78)  

ext = [] 
#loop over the columns (j) and rows(i) to populate subplots 
for j in range(4): 
    for i in range(2): 
     axes[i,j].scatter(x, y[:,4*i+j], c=colors[j], s=25) 
    # each axes in the top row gets its own axes title 
    axes[0,j].set_title('title {}'.format(j+1)) 
    # save the axes bounding boxes for later use 
    ext.append([axes[0,j].get_window_extent().x0, axes[0,j].get_window_extent().width ]) 

# this is optional 
# from the axes bounding boxes calculate the optimal position of the column spanning title 
inv = fig.transFigure.inverted() 
width_left = ext[0][0]+(ext[1][0]+ext[1][1]-ext[0][0])/2. 
left_center = inv.transform((width_left, 1)) 
width_right = ext[2][0]+(ext[3][0]+ext[3][1]-ext[2][0])/2. 
right_center = inv.transform((width_right, 1)) 

# set column spanning title 
# the first two arguments to figtext are x and y coordinates in the figure system (0 to 1) 
plt.figtext(left_center[0],0.88,"Left column spanning title", va="center", ha="center", size=15) 
plt.figtext(right_center[0],0.88,"Right column spanning title", va="center", ha="center", size=15) 
axes[0,0].set_ylim([0,1]) 
axes[0,0].set_xlim([0,10]) 

plt.show() 

enter image description here

関連する問題