2015-01-13 2 views
-1

축 단위와 독립적 인 사각형을 만들고 싶습니다. 예를 들어 figure(figsize=(10, 10))과 같은 수치 치수를 설정할 수 있으며 축 배율 비율을 set_aspect('equal')과 같게 설정할 수는 있지만 실제로 축 길이를 동일하게 설정하려면 어떻게해야합니까? 예 : xaxis 및 yaxis를 각각 10 인치 길이로 만들 수 있습니까?축을 matplotlib pyplot에서 같은 길이로 만듭니다

EDIT 예 코드 plt.subplots_adjust() 기능

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
from matplotlib.ticker import MaxNLocator 
import numpy as np 

x1 = 1 
y1 = [10., 1000.] 
err1 = 0.00865 
x2 = 2 
y2 = [9., 900.] 
err2 = 0.00658 

len_xaxis,len_yaxis = 5.,5. #fix here your numbers 
xspace, yspace = .9, .9 # change the size of the void border here. 
x_fig,y_fig = len_xaxis/xspace, len_yaxis/yspace 

for i in range(2): 
    plt.clf() 
    # fig = plt.figure(figsize=(6, 6)) 
    fig = plt.figure(figsize=(x_fig,y_fig)) 
    plt.subplots_adjust(left=1-xspace, right = xspace, top=yspace, bottom = 1-yspace) 
    gs = gridspec.GridSpec(3, 1) 
    gs.update(hspace=0., wspace=0.) 
    ax1 = plt.subplot(gs[0:2, 0]) 

    ax1.errorbar(x1, y1[i], yerr=err1) 
    ax1.errorbar(x2, y2[i], yerr=err2) 

    ax1.invert_yaxis() 

    plt.setp(ax1.get_xticklabels(), visible=False) # Remove x-labels between the plots 
    plt.xlim(0, 3) 

    ax2 = plt.subplot(gs[2, 0], sharex=ax1) 

    nbins = len(ax1.get_yticklabels()) 
    ax1.yaxis.set_major_locator(MaxNLocator(nbins=8, prune='both')) 
    nbins = len(ax2.get_yticklabels()) 
    ax2.yaxis.set_major_locator(MaxNLocator(nbins=6, prune='both')) 

    plt.tight_layout() 
    plt.savefig('prune_%d.png' % i) 
    plt.close() 

답변

1

플레이. 10 대신 5 인치의 예 :

len_xaxis,len_yaxis = 5.,5. #fix here your numbers 

xspace, yspace = .9, .9 # change the size of the void border here. 

x_fig,y_fig = len_xaxis/xspace, len_yaxis/yspace 
figure(figsize=(x_fig,y_fig)) 
plt.subplots_adjust(left=1-xspace, right = xspace, top=yspace, bottom = 1-yspace) 
plot([1,2,3],[-1,3,5]) 
+0

음, 작동하지 않습니다. 데이터에 따라 (따라서 축 레이블의 길이에 따라) 다른 플롯에서 모든 플롯에서 동일한 높이가되지만 같은 너비는 아닙니다. x 축과 y 축은 고정 길이 비율이 없습니다. – frixhax

+0

나는 본다. 작업 할 예제를 게시 할 수 있습니까? – gg349

+0

나는이 예제를 사용하여 코드를 시도했다. [여기] (http://stackoverflow.com/q/27910125/1812066). 사용하는 y 값 (즉, y 눈금 레이블의 자릿수)에 따라 축 길이 비율이 변경되고 코드가 추가됩니다. – frixhax