2017-03-28 11 views
1

3x3の例の赤い線のように、さまざまな数のサブプロットからなる図に線を描きたい。 matplotlibでこれをどうすればできますか?matplotlibのサブプロット行列の図を通って線を描く

このコードは、基本的に、4Dの例の多次元データ(scatter matrix plotの右上半分)の2D投影の配列です。ここ

tabular matrix

from matplotlib import pyplot as plt 
import numpy as np 
data = np.random.random_sample((10,4)) 
labels = ['p1','p2','p3','p4'] 
fig, axarr = plt.subplots(3,3, sharex='col', sharey='row') 
# Iterate over rows of subplots array 
for row in range(axarr.shape[0]): 
    i = row # data index corresponds to row index 
    # Iterate over columns of subplots array 
    for col in range(axarr.shape[1]): 
     j = col+1 # data index corresponds to column index +1 
     # Do what's needed in lower-left half of array and leave 
     if row>col: 
      if col==0: 
       axarr[row,col].set_ylabel(labels[i],labelpad=5) 
      axarr[row,col].spines['left'].set_visible(False) 
      axarr[row,col].spines['right'].set_visible(False) 
      axarr[row,col].spines['bottom'].set_visible(False) 
      axarr[row,col].spines['top'].set_visible(False) 
      axarr[row,col].xaxis.set_ticks_position('none') 
      axarr[row,col].yaxis.set_ticks_position('none') 
      axarr[row,col].tick_params(labelleft=False) 
      axarr[row,col].tick_params(labelbottom=False) 
      continue 
     # Proceed with upper-right half of array 
     axarr[row,col].scatter(data[:,i],data[:,j], s=4) 
     axarr[row,col].tick_params(labelleft=False) 
     axarr[row,col].tick_params(labelbottom=False) 
     if row==0: 
      axarr[row,col].set_xlabel(labels[j],labelpad=5) 
      axarr[row,col].xaxis.set_label_position('top') 
     if col==0: 
      axarr[row,col].set_ylabel(labels[i],labelpad=5) 
      axarr[row,col].yaxis.set_label_position('left') 

答えて

1

実際の図形の大きさとは無関係であり、図と共にスケーリング溶液です。

左上のサブプロットの軸座標で垂直または水平の位置を指定しながら、図形座標の線の長さを指定できるように混合変換を使用します。したがって、垂直線は、y方向のFigure座標で0から1になりますが、最初のサブプロットのaxes座標ではx = 0にバインドされます。
オフセット変換を追加して線幅の半分だけポイント内に移動し、軸スパインに対してぴったりと座るようにします。

ここ

enter image description here

from matplotlib import pyplot as plt 
import matplotlib.lines 
import matplotlib.transforms as transforms 
import numpy as np 

data = np.random.random_sample((10,10)) 
labels = "Some labels around all the subplots" 
fig, axarr = plt.subplots(3,3, sharex='col', sharey='row') 
for i, ax in enumerate(axarr.flatten()): 
    ax.scatter(data[:,i], data[:,i+1]) 
    ax.xaxis.set_label_position('top') 
for i in range(3): 
    axarr[2-i,0].set_ylabel(labels.split()[i]) 
    axarr[0,i].set_xlabel(labels.split()[i+3]) 
    axarr[2-i,0].set_yticklabels([]) 

#### Create lines #### 
lw=4 # linewidth in points 
#vertical line 
offset1 = transforms.ScaledTranslation(-lw/72./2., 0, fig.dpi_scale_trans) 
trans1 = transforms.blended_transform_factory(
    axarr[0,0].transAxes +offset1, fig.transFigure) 
l1 = matplotlib.lines.Line2D([0,0], [0, 1], transform=trans1, 
      figure=fig, color="#dd0000", linewidth=4, zorder=0) 
#horizontal line 
offset2 = transforms.ScaledTranslation(0,lw/72./2., fig.dpi_scale_trans) 
trans2 = transforms.blended_transform_factory(
    fig.transFigure, axarr[0,0].transAxes+offset2) 
l2 = matplotlib.lines.Line2D([0, 1], [1,1], transform=trans2, 
      figure=fig, color="#dd0000", linewidth=4, zorder=0) 
#add lines to canvas 
fig.lines.extend([l1, l2]) 

plt.show() 

ライン位置は、図のサイズに適合することを見ることができるから、小型版、。

enter image description here

+0

これは完璧に機能します。ありがとう!あなたのコードでは、コマンドを使っていくつかの詳細をパーソナライズすることもできます。 – Daniel

関連する問題