【问题标题】:How to put different in magnitude values at same distance on x-axis如何在 x 轴上的相同距离处放置不同的幅度值
【发布时间】:2017-07-25 14:21:12
【问题描述】:

我正在使用 Python 在同一个图上绘制几条直线。 我的值幅度比 x 值有很大的可变性(在我的代码中用变量 q 调用),所以我想把它们放在 x 轴上的相同距离,以便在第一部分有一个清晰的视野图的。

这是我的代码:

def c(m,x):
'''
Linear cost function, describing costs for each weight range
:param m: the slope, modelling rates
:param x: the abscissa, modelling quantity ordered
:return: the cost value for each quantity
'''
    return m * x

for i in range(0,9):
    w = np.arange(0., q[9], 0.01)
    plt.plot(w,c(r[i],w),'b--',linewidth=0.3)
    plt.plot( [q[i],breakpoints[i]] , [c(r[i], q[i]), c(r[i], breakpoints[i])], 'r')
    plt.plot(q[i + 1], c(r[i], breakpoints[i]), 'r.')

plt.show()

为了简单起见,这里有我的代码sn-p涉及的所有数据:

如您所见,这是经典的数量折扣研究。 所以,如果我只是简单地绘制这些值,我无法区分第一条直线上发生了什么,因为为前 q 值保留的空间很小,见下文:

现在我向您展示前 q 值的缩放图:

我想到的解决方案是将所有q[] 值绘制在相同的距离上:见下图。 所以我想要实现的只是让所有 q[] 值在 x 轴上的距离相同,独立于它们的值。我该怎么做?

【问题讨论】:

  • 谢谢。我已经编辑了我的问题。
  • 使用像您展示的那样的轴是可能的,但是直线将不再是直线(它们将是弯曲的和/或有扭结)。也许您首先想检查对数缩放是否朝着您想要的方向发展 (plt.gca().set_xscale("log", nonposx='clip'))。
  • 如果我没记错的话,它们应该保持直线,因为将执行单轴上的线性变换。我只是希望 x 轴上的每个间隔都具有相同的长度(例如,间隔 [0,0.1] 为 2 厘米,间隔 [100,250] 为 2 厘米)。顺便说一句,谢谢你的帮助
  • 如果区间 0 到 0.1 所占用的空间与例如10 到 20。
  • 为什么不呢?您只是按一个常数因子进行缩放

标签: python matplotlib plot


【解决方案1】:

正如 cmets 中所说,当操纵比例以显示不均匀的间距时,直线将不再是直线。

以下是实现问题中所需比例的代码。

import numpy as np
from numpy import ma
from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms
from matplotlib.ticker import FixedLocator


class SegmentedScale(mscale.ScaleBase):
    name = 'segmented'

    def __init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        self.points = kwargs.get('points',[0,1])
        self.lb = self.points[0]
        self.ub = self.points[-1]

    def get_transform(self):
        return self.SegTrans(self.lb, self.ub, self.points)

    def set_default_locators_and_formatters(self, axis):
        axis.set_major_locator(FixedLocator(self.points))

    def limit_range_for_scale(self, vmin, vmax, minpos):
        return max(vmin, self.lb), min(vmax, self.ub)

    class SegTrans(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, lb, ub, points):
            mtransforms.Transform.__init__(self)
            self.lb = lb
            self.ub = ub
            self.points = points

        def transform_non_affine(self, a):
            masked = a # ma.masked_where((a < self.lb) | (a > self.ub), a)
            return np.interp(masked, self.points, np.arange(len(self.points)))

        def inverted(self):
            return SegmentedScale.InvertedSegTrans(self.lb, self.ub, self.points)

    class InvertedSegTrans(SegTrans):

        def transform_non_affine(self, a):
            return np.interp(a, np.arange(len(self.points)), self.points)
        def inverted(self):
            return SegmentedScale.SegTrans(self.lb, self.ub, self.points)

# Now that the Scale class has been defined, it must be registered so
# that ``matplotlib`` can find it.
mscale.register_scale(SegmentedScale)


if __name__ == '__main__':

    u=  u"""0, 137.13, 0.082
        0.1, 112.46, 0.175
        0.2, 98.23, 0.368
        0.5, 72.38, 0.838
        1, 60.69, 8.932
        10, 54.21, 17.602
        20, 47.71, 48.355
        50, 46.14, 89.358
        100, 41.23, 241.147
        250, 39.77, 0"""

    import io
    import matplotlib.pyplot as plt

    q,r,breakpoints = np.loadtxt(io.StringIO(u), delimiter=", ", unpack=True)

    c = lambda m,x : m*x

    for i in range(0,9):
        w = np.arange(0., q[9], 0.01)
        plt.plot(w,c(r[i],w),'b--',linewidth=0.3)
        plt.plot( [q[i],breakpoints[i]] , [c(r[i], q[i]), c(r[i], breakpoints[i])], 'r')
        plt.plot(q[i + 1], c(r[i], breakpoints[i]), 'r.')

    plt.gca().set_xscale('segmented', points = q)
    plt.show()

除了线条中的扭结(这可能不是我们想要的,但这是此处使用的比例尺的必然结果)之外,y 轴上的值仍然非常不可读。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多