为了账号安全,请及时绑定邮箱和手机立即绑定

Python/Matplotlib-有没有一种方法可以创建一个不连续的轴?

Python/Matplotlib-有没有一种方法可以创建一个不连续的轴?

GCT1015 2019-07-03 16:24:36
Python/Matplotlib-有没有一种方法可以创建一个不连续的轴?我试着用不连续的x轴来创建一个图形。通常的绘制方法是,轴上有这样的东西:(数值)-/-(后来的数值)其中/指示您正在跳过(值)和(后面的值)之间的所有内容。我还没有找到这方面的任何例子,所以我想知道这是否可能。我知道你可以加入一个不连续的数据,例如,金融数据,但我想让轴上的跳跃更加明确。目前,我只是在使用子图,但我真的很想让所有的东西最终都在同一张图上结束。
查看完整描述

3 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

我看到了很多关于这个特性的建议,但是没有迹象表明它已经实现了。这是目前可行的解决办法。它将阶跃函数变换应用于x轴.这是很多代码,但它相当简单,因为它大部分是样板定制规模的东西。我没有添加任何图形来表示中断的位置,因为这是一个风格问题。祝你顺利完成这项工作。

from matplotlib import pyplot as pltfrom matplotlib import scale as mscalefrom matplotlib 
import transforms as mtransformsimport numpy as npdef CustomScaleFactory(l, u):
    class CustomScale(mscale.ScaleBase):
        name = 'custom'

        def __init__(self, axis, **kwargs):
            mscale.ScaleBase.__init__(self)
            self.thresh = None #thresh

        def get_transform(self):
            return self.CustomTransform(self.thresh)

        def set_default_locators_and_formatters(self, axis):
            pass

        class CustomTransform(mtransforms.Transform):
            input_dims = 1
            output_dims = 1
            is_separable = True
            lower = l
            upper = u            def __init__(self, thresh):
                mtransforms.Transform.__init__(self)
                self.thresh = thresh            def transform(self, a):
                aa = a.copy()
                aa[a>self.lower] = a[a>self.lower]-(self.upper-self.lower)
                aa[(a>self.lower)&(a<self.upper)] = self.lower                return aa            
                def inverted(self):
                return CustomScale.InvertedCustomTransform(self.thresh)

        class InvertedCustomTransform(mtransforms.Transform):
            input_dims = 1
            output_dims = 1
            is_separable = True
            lower = l
            upper = u            def __init__(self, thresh):
                mtransforms.Transform.__init__(self)
                self.thresh = thresh            def transform(self, a):
                aa = a.copy()
                aa[a>self.lower] = a[a>self.lower]+(self.upper-self.lower)
                return aa            def inverted(self):
                return CustomScale.CustomTransform(self.thresh)

    return CustomScalemscale.register_scale(CustomScaleFactory(1.12, 8.88))x = np.concatenate((np.linspace(0,1,10), 
    np.linspace(9,10,10)))xticks = np.concatenate((np.linspace(0,1,6), np.linspace(9,10,6)))y = np.sin(x)plt.plot(x, y, '.')
    ax = plt.gca()ax.set_xscale('custom')ax.set_xticks(xticks)plt.show()



查看完整回答
反对 回复 2019-07-03
?
梦里花落0921

TA贡献1772条经验 获得超6个赞

检查折轴一揽子:

import matplotlib.pyplot as pltfrom brokenaxes import brokenaxesimport numpy as np

fig = plt.figure(figsize=(5,2))bax = brokenaxes(xlims=((0, .1), (.4, .7)), ylims=((-1, .7), (.79, 1)), 
hspace=.05)x = np.linspace(0, 1, 100)bax.plot(x, np.sin(10 * x), label='sin')bax.plot(x, np.cos(10 * x),
 label='cos')bax.legend(loc=3)bax.set_xlabel('time')bax.set_ylabel('value')


查看完整回答
反对 回复 2019-07-03
  • 3 回答
  • 0 关注
  • 1475 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信