2011-06-27 44 views
4

爲了簡化我的問題(它不完全是這樣,但我更喜歡簡單的問題的簡單答案):如何在長度不同的軸上添加恆定間隔的刻度? [Python]

我有幾個2D地圖描繪矩形區域區域。我想添加地圖座標軸和刻度以顯示此地圖上的距離(使用matplotlib,因爲舊的代碼與它一起),但問題是這些區域大小不同。我想把斧頭放在很好,清晰的蜱蟲上,但是地圖的寬度和高度可以是任何東西...

試圖解釋我的意思:假設我有一個地區的地圖,大小是4.37公里* 6.42公里。我希望在0,1,2,3和4 km上有x軸刻度,在0,1,2,3,4,5和6 km上有y軸刻度:s。然而,由於該地區大於4公里* 6公里,因此圖像和座標軸進一步長達4公里和6公里。

蜱蟲之間的間隔可以是恆定的,1公里。但是,地圖的大小差異很大(比如,在5-15公里之間),它們是浮點值。我當前的腳本知道區域的大小,並可以將圖像縮放到正確的高度/寬度比例,但如何告訴它在哪裏放置刻度?

有可能是這個問題已經解決,但因爲我無法找到我的問題合適的搜索詞,我不得不問在這裏......

+0

相關:http://stackoverflow.com/questions/4947682/intelligently-calculating-chart-tick-positions –

回答

0

這應該給你的所有整數值蜱內您在x軸上的當前軸限制:

from matplotlib import pylab as plt 
import math 

# get values for the axis limits (unless you already have them) 
xmin,xmax = plt.xlim() 

# get the outermost integer values using floor and ceiling 
# (I need to convert them to int to avoid a DeprecationWarning), 
# then get all the integer values between them using range 
new_xticks = range(int(math.ceil(xmin)),int(math.floor(xmax)+1)) 
plt.xticks(new_xticks,new_xticks) 
# passing the same argment twice here because the first gives the tick locations 
# and the second gives the tick labels, which should just be the numbers 

對y軸重複。

出於好奇:默認情況下你會得到什麼樣的刻度?

+0

謝謝很多!我會檢查你的和上面的人的解決方案,並試圖找出他的問題最好的方法。 :)由於我對mathplotlib不是很有經驗,所以我使用了一個高年級學生留下的難看的滴答聲。那麼,結果就像醜陋......'locs,labels = mpl.yticks(yt,['%.2f'%height,'%.2f'%(height * 0.75),'%.2f'%(height * 0.5),'%.2f'%(高度* 0.25),'0',''])' – mmyntti

5

只需將刻度線定位器設置爲使用matplotlib.ticker.MultipleLocator(x)其中x是您想要的間距(例如上面示例中的1.0)。

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 

x = np.arange(20) 
y = x * 0.1 

fig, ax = plt.subplots() 
ax.plot(x, y) 

ax.xaxis.set_major_locator(MultipleLocator(1.0)) 
ax.yaxis.set_major_locator(MultipleLocator(1.0)) 

# Forcing the plot to be labeled with "plain" integers instead of scientific notation 
ax.xaxis.set_major_formatter(FormatStrFormatter('%i')) 

plt.show() 

這樣做的好處是,無論我們如何縮放或與繪圖交互,它總會被標記爲相隔1單位的刻度。 enter image description here

0

好吧,我想你的版本,但遺憾的是我不能讓他們的工作,因爲有一些縮放和PDF定位的東西,讓我(和你的代碼的建議)嚴重混亂。但通過測試他們,我又學到了很多python,謝謝!

我終於找到了一個解決方案,它不是非常準確,但滿足了我的需求。這是我如何做到的。

在我的版本中,一公里除以名爲STEP_PART的合適整數常量。 STEP_PART越大,軸數值越精確(如果數值太大,軸會變得雜亂無章)。例如,如果STEP_PART爲5,則精確度爲1 km/5 = 200 m,並且每隔200 m標記一次。

STEP_PART = 5    # In the start of the program. 

height = 6.42    # These are actually given elsewhere, 
width = 4.37    # but just as example... 

vHeight = range(0, int(STEP_PART*height), 1) # Make tick vectors, now in format 
               # 0, 1, 2... instead of 0, 0.2... 
vWidth = range(0, int(STEP_PART*width), 1) # Should be divided by STEP_PART 
               # later to get right values. 

爲了避免過多的軸標籤(0,1,2 ...足夠,0,0.2,0.4 ... ...是太多了),我們替換非整公里值與字符串「 」。同時,我們通過STEP_PART分割整數km值以獲得正確的值。

for j in range(len(vHeight)): 
    if (j % STEP_PART != 0): 
     vHeight[j] = "" 
    else: 
     vHeight[j] = int(vHeight[j]/STEP_PART) 

for i in range(len(vWidth)): 
    if (i % STEP_PART != 0): 
     vWidth[i] = "" 
    else: 
     vWidth[i] = int(vWidth[i]/STEP_PART) 

後來,創建圖形和座標軸後,以這種方式放置刻度(以x軸爲例)。在那裏,x是圖片的實際寬度,用shape()命令得到了(我不完全明白......在我修改的代碼中有很多縮放和東西)。

xt = np.linspace(0,x-1,len(vWidth)+1) # For locating those ticks on the same distances. 
locs, labels = mpl.xticks(xt, vWidth, fontsize=9) 

重複y軸。結果是一個圖表,其中每200米標記一個數字標籤,而整數km值標記數據標籤。無論如何,這些軸的精度是200米,這不是確切的,但它對我來說已經足夠了。這個腳本會更好,如果我找到如何增長整數蜱的大小...

相關問題