【问题标题】:Matplotlib - Show axis break // in graphMatplotlib - 在图表中显示轴中断 //
【发布时间】:2021-04-12 22:14:21
【问题描述】:

我正在尝试重新创建以下情节的外观:

轴本身的断裂非常简单,如下所述:https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html

但我不知道如何在图表本身中包含 // 中断。有什么想法吗?

这是我的数据目前的样子。

【问题讨论】:

    标签: python matplotlib axis break


    【解决方案1】:

    您知道,brokenaxes 包非常适合轴的中断,但在已发布的包中似乎没有此功能。然而,我已经想出了如何实现你正在寻找的东西。

    首先在终端的环境目录中安装包:

    >>> pip install brokenaxes
    

    >>> conda install -c conda-forge brokenaxes
    

    解决方案:

    >>> from brokenaxes import brokenaxes
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>>
    >>> x_min    = 0
    >>> x_break  = 3
    >>> gap      = .1
    >>> x_max    = 6
    >>>
    >>> def f(x):
    ...     return np.cos(x)
    ...
    >>>
    >>> fig  = plt.figure(figsize=(10,4))
    >>> brax = brokenaxes(xlims=((x_min, x_break), (x_break+gap, x_max)), hspace=gap)
    >>> xs   = np.linspace(x_min, x_max, 150)
    >>>
    >>> brax.plot(xs, f(xs))
    >>> brax.annotate(u'\u2014', (x_break-0.001, f(x_break)), rotation=45, size=25, ha='center', va='center', c='C0')
    >>> brax.annotate(u'\u2014', (x_break+gap, f(x_break)), rotation=45, size=25, ha='center', va='center', c='C0')
    >>>
    >>> plt.show()
    

    如果您从现有数组中绘图,函数 f 可以从数组中检索这些索引值。

    【讨论】:

    • 是的,我也看到了这一点,效果很好,但我仍然看不到在图表本身中包含中断的选项,而不仅仅是在轴中(就像我原始帖子中的示例图一样) .还是我只是瞎了眼?
    • 我很抱歉 - 我一开始误读了你的问题。我删除了最初的回复,并且(在过去 2 小时内)想出了如何实现您正在寻找的内容。我希望这有帮助!也许这个功能可能有用@ben.dichter?
    • 感谢您抽出宝贵时间!我可以让它与您的示例一起使用,但是对于我的数据和几个堆叠图来说,它肯定非常麻烦。希望有更直接的解决方案,但我必须让它像他那样工作。
    【解决方案2】:

    使用示例链接https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html, 实现您的目标:

    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(19680801)
    
    # creat data points
    x = np.arange(20)+1
    pts = x/x
    
    # If we were to simply plot pts, we'd lose most of the interesting
    # details due to the outliers. So let's 'break' or 'cut-out' the y-axis
    # into two portions - use the top (ax1) for the outliers, and the bottom
    # (ax2) for the details of the majority of our data
    fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
    fig.subplots_adjust(hspace=0.05)  # adjust space between axes
    
    # plot the same data on both axes
    ax1.plot(x, pts)
    ax2.plot(x, pts)
    
    # zoom-in / limit the view to different portions of the data
    ax1.set_xlim(0, 10)  # left part plot
    ax2.set_xlim(11, 19)  # right part plot
    
    # hide the spines between ax and ax2
    ax1.spines['right'].set_visible(False)
    ax2.spines['left'].set_visible(False)
    #ax1.xaxis.tick_top()
    #ax1.tick_params(labeltop=False)  # don't put tick labels at the top
    ax2.yaxis.tick_right()
    
    # Now, let's turn towards the cut-out slanted lines.
    # We create line objects in axes coordinates, in which (0,0), (0,1),
    # (1,0), and (1,1) are the four corners of the axes.
    # The slanted lines themselves are markers at those locations, such that the
    # lines keep their angle and position, independent of the axes size or scale
    # Finally, we need to disable clipping.
    
    d = .9  # proportion of vertical to horizontal extent of the slanted line
    kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
                  linestyle="none", color='k', mec='k', mew=1, clip_on=False)
    ax1.plot([1, 1], [0, 1], transform=ax1.transAxes, **kwargs)
    ax2.plot([0, 0], [0, 1], transform=ax2.transAxes, **kwargs)
    
    # add slanted line on the graph
    ax1.plot([1], [0.5], transform=ax1.transAxes, **kwargs)
    ax2.plot([0], [0.5], transform=ax2.transAxes, **kwargs) 
    #fig.savefig('_')
    
    plt.show()
    

    结果像

    【讨论】:

    • 这样做的问题是不同轴的范围需要相同,否则缩放将关闭。上面的断轴包解决了这个问题,但引入了其他复杂性。所以,似乎没有真正的好方法来做到这一点......
    • 好的。希望您尽快解决其他问题。
    猜你喜欢
    • 2020-11-08
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2020-10-15
    • 2019-02-27
    相关资源
    最近更新 更多