2017-05-09 88 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的源代码。