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

一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
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()

梦里花落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')
添加回答
举报
0/150
提交
取消