2012-08-22 116 views
6

我一直在網上查找很長時間,但無法弄清楚如何製作它。我需要畫出幾個xticks被定義爲numpy.arange(1,N)的數字,N對於每個數字都不相同。我希望所有數字(例如1釐米)上的xticks之間的間距相同,也就是說,每個數字的寬度必須取決於numpy.arange(1,N)的大小。任何想法如何做到這一點?Matplotlib - 如何設置xticks之間的距離(以mm/cm /點爲單位)

回答

2

我認爲你可以通過仔細控制你的座標軸大小(作爲圖的一部分),ax.set_xlimfig.set_size_inches(doc)的組合來設置圖的實際大小。

fig = plt.figure() 
ax = fig.add_axes([0,0,1,1]) 
ax.set_xlim([0,N]) 
fig.set_size_inches([N/2.54,h]) 
0

要在@ tcaswell的答案擴大,這裏是我如何做到這一點時,我想微觀管理我的軸和刻度間距離的實際尺寸。

import numpy as np 
import matplotlib.pyplot as plt 

plt.close('all') 

#------------------------------------------------------ define xticks setup ---- 

xticks_pos = np.arange(11) # xticks relative position in xaxis 
N = np.max(xticks_pos) - np.min(xticks_pos) # numbers of space between ticks 
dx = 1/2.54 # fixed space between xticks in inches 
xaxis_length = N * dx 

#------------------------------------------------------------ create figure ---- 

#---- define margins size in inches ---- 

left_margin = 0.5 
right_margin = 0.2 
bottom_margin = 0.5 
top_margin = 0.25 

#--- calculate total figure size in inches ---- 

fwidth = left_margin + right_margin + xaxis_length 
fheight = 3 

fig = plt.figure(figsize=(fwidth, fheight)) 
fig.patch.set_facecolor('white') 

#---------------------------------------------------------------- create axe---- 

#---- axes relative size ---- 

axw = 1 - (left_margin + right_margin)/fwidth 
axh = 1 - (bottom_margin + top_margin)/fheight 

x0 = left_margin/fwidth 
y0 = bottom_margin/fheight 

ax0 = fig.add_axes([x0, y0, axw, axh], frameon=True) 

#---------------------------------------------------------------- set xticks---- 

ax0.set_xticks(xticks_pos) 

plt.show(block=False) 
fig.savefig('axis_ticks_cm.png') 

這導致11.8釐米10釐米的x軸的圖與每個蜱之間1釐米空間:

enter image description here

相關問題