2016-10-20 12 views
2

Pythonでmatplotlibに問題があり、2つのプロットを並べて作成しようとしています。私はそれらをお互いに隣に置くことができましたが、正確に同じサイズにする必要があります。右側の各点は、肉眼で左側の位置に簡単にマッピングする必要があります。 代わりに、私の右のもの(散布図)には余白があり、それは左(ヒートマップ)よりわずかに小さく見えます。matplotlib:同じサイズのサブプロット?

enter image description here

私はヒートマップを生成する方法は、次のとおりです。

def plot_map(matrix): 
    # Plot it out 
    #fig, ax = plt.subplots() 
    fig = plt.figure() 
    ax = plt.subplot(1,2,1) 
    c = m.colors.ColorConverter().to_rgb 
    cm = make_colormap([(0,1,0), (1,1,0), 0.1, (1,1,0), (1,0.5,0), 0.66, (1,0.5,0),(1,0,0)]) 
    heatmap = ax.pcolor(matrix, cmap=cm) 

    # Format 
    fig = plt.gcf() 
    fig.set_size_inches(20, 20) 
    plt.gca().set_aspect('equal') 
    # turn off the frame 
    ax.set_frame_on(False) 

    # put the major ticks at the middle of each cell 
    ax.set_yticks(np.arange(matrix.shape[0]) + 0.5, minor=False) 
    ax.set_xticks(np.arange(matrix.shape[1]) + 0.5, minor=False) 

    # want a more natural, table-like display 
    ax.invert_yaxis() 
    ax.xaxis.tick_top() 

    # note I could have used matrix.columns but made "labels" instead 
    ax.set_xticklabels(range(0,matrix.shape[0]), minor=False) 
    ax.set_yticklabels(range(0,matrix.shape[1]), minor=False) 
    ax.grid(False) 

    # Turn off all the ticks 
    ax = plt.gca() 

    for t in ax.xaxis.get_major_ticks(): 
     t.tick1On = False 
     t.tick2On = False 
    for t in ax.yaxis.get_major_ticks(): 
     t.tick1On = False 
     t.tick2On = False 

    divider = make_axes_locatable(ax)  
    cax = divider.append_axes("right", size="5%", pad=0.05) 
    plt.colorbar(heatmap,cax=cax) 

    return (fig,ax) 

その後斧は、地図上の小さな青い線プロットするために別の関数に渡されます。

def plot_chosen(edges,endnodes,side,ax): 
    for e in edges: 
     u = endnodes[e - 1][0] 
     v = endnodes[e - 1][1] 
     xu, yu = get_center(u,side) 
     xv, yv = get_center(v,side) 
     ax.plot([xu+0.5, xv+0.5], [yu+0.5, yv+0.5], 'k-', lw=4, color='blue',alpha=0.5) 

を最後に、このような散布図をプロットします。

def plot_satter(edges,endnodes,side,xs,ys,data): 
    plt.margins(0) 
    ax = plt.subplot(1, 2, 2) 
     # Format 
    fig = plt.gcf() 
    fig.set_size_inches(20, 20) 
    plt.gca().set_aspect('equal') 
    # turn off the frame 
    ax.set_frame_on(False) 

    # put the major ticks at the middle of each cell 
    ax.set_yticks(np.arange(side) + 0.5, minor=False) 
    ax.set_xticks(np.arange(side) + 0.5, minor=False) 

    # want a more natural, table-like display 
    ax.invert_yaxis() 
    ax.xaxis.tick_top() 

    ax.set_xmargin(0) 
    ax.set_ymargin(0) 

    # note I could have used matrix.columns but made "labels" instead 
    ax.set_xticklabels(range(0,side), minor=False) 
    ax.set_yticklabels(range(0,side), minor=False) 
    ax.grid(False) 

    # Turn off all the ticks 
    ax = plt.gca() 

    for t in ax.xaxis.get_major_ticks(): 
     t.tick1On = False 
     t.tick2On = False 
    for t in ax.yaxis.get_major_ticks(): 
     t.tick1On = False 
     t.tick2On = False 
    cm = make_colormap([(0,0,1), (0,1,1), 0.1, (0,1,1), (1,1,0), 0.66, (1,1,0),(1,0,0)]) 
    resmap = plt.scatter(xs,ys, c=data,cmap=cm,edgecolors='none',alpha=0.5,s=data) 
    divider = make_axes_locatable(ax)  
    cax = divider.append_axes("right", size="5%", pad=0.05) 
    plt.colorbar(resmap,cax=cax) 

しかし、散布図をヒートマップほど大きくする方法はありません。実際には、カラーバーと同じくらい大きいと思われますが、どちらでも動作しません。これは、散布の周囲に余裕があると思います。

また、私はできる方法がありますかPNGファイル全体を正方形にしないでください。それは長方形ですか?

ありがとうございます!

+1

多くのコードがあります。できるだけ離れて実行可能にすることができますか?[MCVE](http://stackoverflow.com/help/mcve)-example? – pathoren

答えて

2

ヘルプを利用するには、minimal working exampleを提供する必要があります。あなたはそのようなminimal working exampleほとんど常にを生成することが問題とそれに対応する解決策を自分で見つけることがわかります。
また、あなたのコードを構造化してください!

私たちはあなたのデータと使用している変数について必要な知識を持っていないので、解決策を思いつくことはほとんど不可能です。

あなたがする必要があるのは、問題を解消することです。あなたは2つのものの違いを探しています - できるだけ均等にしてください。両方を両方のプロットに同時に適用すると、結局どのように違うのですか?

次のコードは、これを行う方法を示しており、2つのプロットのサイズには違いはありません。そこでそこから始めて、それに応じて必要なものを追加してください。問題の原因となるコードを見つけるまで、一度に1ステップ。 fig = plt.figure(figsize=(20,20))は正方形を生成しながら、

import matplotlib.pyplot as plt 
import numpy as np 
from mpl_toolkits.axes_grid1 import make_axes_locatable 

x = np.linspace(0,100,101) 
X, Y = np.meshgrid(x,x) 
data = np.sin(X/2.3)*np.cos(Y/2.7)*np.cos(X*Y/20.) 

fig = plt.figure(figsize=(10,5)) 
ax1=fig.add_subplot(121) 
ax2=fig.add_subplot(122) 
plt.subplots_adjust(wspace = 0.33) 

heatmap = ax1.pcolor(data, cmap="RdPu") 
resmap = ax2.scatter(X,Y, c=data, cmap="YlGnBu",edgecolors='none',alpha=0.5) 


for ax in [ax1,ax2]: 
    ax.set_frame_on(False) 

    ax.set_aspect('equal') 

    ax.invert_yaxis() 
    ax.xaxis.tick_top() 

    ax.set_xmargin(0) 
    ax.set_ymargin(0) 

    ax.grid(False) 

    for t in ax.xaxis.get_major_ticks(): 
     t.tick1On = False 
     t.tick2On = False 
    for t in ax.yaxis.get_major_ticks(): 
     t.tick1On = False 
     t.tick2On = False 

    ax.set_xlim([x[0],x[-1]]) 
    ax.set_ylim([x[0],x[-1]]) 


divider1 = make_axes_locatable(ax1)  
cax1 = divider1.append_axes("right", size="5%", pad=0.05) 
plt.colorbar(heatmap,cax=cax1) 

divider2 = make_axes_locatable(ax2)  
cax2 = divider2.append_axes("right", size="5%", pad=0.05) 
plt.colorbar(resmap,cax=cax2) 

plt.show() 

そして、ところで、fig = plt.figure(figsize=(10,5))は、矩形を生成します。

+0

こんにちは、助けてくれてありがとう!私は小さなインスタンスを作ることは考えていませんでしたが、matplotlibを使うことは人には明らかですが、すべての場合、私の問題は私が行方不明だったということでした: 'ax.set_xlim([x [0]、x [-1]])ax.set_ylim([x [0]、x [-1]])'散布図。ありがとう! – ddeunagomez

関連する問題