2016-10-20 5 views
2

파이썬에서 matplotlib에 문제가있어 두 개의 플롯을 나란히 만들려고합니다. 나는 그 (것)들을 서로 옆에 머물게 할 수 있었다, 그러나 나는 정확한 크기가 있기 위하여 그 (것)들을 필요로한다 : 우측 것에있는 각 점은 육안을 가진 좌측 것에 위치에 쉽게 매식되어야한다. 대신, 오른쪽 (분산 형 플롯)에는 약간의 여백이있어 왼쪽보다 약간 작게 표시됩니다 (히트 맵).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, 등 거의 항상을 생성하면 문제와 해결책을 직접 찾을 수 있습니다.
또한 코드를 구조화하십시오!

데이터 및 사용중인 변수에 대한 지식이 없기 때문에 솔루션을 찾는 것이 거의 불가능합니다.

문제를 해결하기 만하면됩니다. 당신은 두 가지의 차이를 찾고 있습니다 - 가능한 한 동등한 것으로 만드십시오. 모든 것을 동시에 두 플롯 모두에 적용하면 결국 어떻게 다를 수 있습니까?

다음 코드는이를 수행하는 방법을 보여주고 두 플롯의 크기에는 차이가 없음을 보여줍니다. 그래서 거기에서 시작하여 그에 따라 필요할 수도있는 것을 추가하십시오. 한 번에 한 단계 씩, 문제를 일으키는 코드 조각을 찾을 때까지. 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() 

그리고 BTW, fig = plt.figure(figsize=(10,5))는 사각형을 생성합니다.

+0

안녕하세요, 도움 주셔서 감사합니다! matplotlib에 익숙한 사람들에게는 어느 정도 명백 할지라도 나는 더 작은 인스턴스를 만드는 것을 생각하지 않았습니다. 모든 경우에 내 문제는 내가 빠져 있다는 것이었다. ax.set_xlim ([x [0], x [-1])) ax.set_ylim ([x [0], x [-1]])' 산포도. 고마워! – ddeunagomez