2017-05-09 175 views
0

我用Python繪製了一些數據,並試圖用FuncFormatter來改變刻度。現在我想將分割改爲圓形數字。我也希望在同一陣型中有較小的蜱蟲,在我的情況下,這將是一個1/x細分。我希望擴大規模。圖片將幫助你想象我的問題。python 1/x繪圖刻度格式化,刻度位置

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.ticker as tick 
x = np.array([805.92055,978.82006,564.88627,813.70311,605.73361,263.27184,169.40317]) 
y = np.array([10,9,8,7,6,3,2]) 
fig, ax = plt.subplots(figsize =(3.6,2.5)) 
plt.plot(1/x,y,linestyle ='None',marker='1') 
a=0.001 
b=0.005 
plt.xlim(a,b) 
def my_formatter_fun(x, p): 
    return "%.0f" % (1/x)   
ax.get_xaxis().set_major_formatter(tick.FuncFormatter(my_formatter_fun)) 

plot with changed xtick

我怎樣才能改變segmentaion讓我得到這樣的事情?我認爲可以通過my_formatter_fun添加我的願望,但我不知道如何。我怎樣才能在1/x分佈中添加次要蜱?我試過plt.minorticks_on(),但這不起作用,因爲它們處於線性位置。

desired plot

回答

0

蜱的位置可以與matplotlib.ticker.Locator來控制。對於1/x蜱,你需要定義自己的定位:通過調用

ax.get_xaxis().set_major_locator(ReciprocalLocator(numticks=4)) 
ax.get_xaxis().set_minor_locator(ReciprocalLocator(numticks=20)) 

這需要更多的調整到位置移動到漂亮的數字

class ReciprocalLocator(tick.Locator): 
    def __init__(self, numticks = 5): 
     self.numticks = numticks 
    def __call__(self): 
     vmin, vmax = self.axis.get_view_interval() 
     ticklocs = np.reciprocal(np.linspace(1/vmax, 1/vmin, self.numticks)) 
     return self.raise_if_exceeds(ticklocs) 

您可以在劇情中使用它。有關靈感,請參閱matplotlib.ticker.MaxNLocator的源代碼。