python 1/x绘图比例格式,刻度位置 [英] python 1/x plot scale formatting, tick position

查看:62
本文介绍了python 1/x绘图比例格式,刻度位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python绘制了一些数据,并尝试使用 FuncFormatter 更改刻度.现在,我想将分段更改为整数.我还希望在同一编队中出现小刻度,在我的情况下,它将是 1/x 分段.我希望缩放比例得以传播.这些图片将帮助您想象我的问题.

I have plotted some data with Python and tried to change the ticks with FuncFormatter. Now I want to change the segmentation into round numbers. I also want minor ticks in the same formation, in my case it would be a 1/x segmentation. I want the scaling to spread. The pictures will help you to imagine my issue.

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))

我怎样才能改变segmentaion以获得这样的结果?我以为可以在 my_formatter_fun 中添加我的愿望,但我不知道如何.我怎样才能在 1/x 发行版中添加小滴答声?我试过 plt.minorticks_on() 但这不起作用,因为它们处于线性位置.

How can I change the segmentaion so that I get something like this? I thought there could be a way to add my wishes in my_formatter_fun but I don't know how. How could I add minor-ticks also in a 1/x distribution? I tried plt.minorticks_on() but this is not working, because they are in a linear position.

推荐答案

可用 matplotlib.ticker.Locator 控制刻度线的位置.对于 1/x 刻度,您需要定义自己的自定义定位器:

The location of the ticks can be controlled with a matplotlib.ticker.Locator. For 1/x ticks, you need to define your own custom Locator:

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)

您可以通过致电

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

这需要更多的调整才能将位置移动到合适的数字.为了获得灵感,请查看 matplotlib.ticker.MaxNLocator 的源代码.

This needs more tweaking to move the locations to nice numbers. For inspiration, have a look at the source code of matplotlib.ticker.MaxNLocator.

这篇关于python 1/x绘图比例格式,刻度位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆