<i id='9JWG3'><tr id='9JWG3'><dt id='9JWG3'><q id='9JWG3'><span id='9JWG3'><b id='9JWG3'><form id='9JWG3'><ins id='9JWG3'></ins><ul id='9JWG3'></ul><sub id='9JWG3'></sub></form><legend id='9JWG3'></legend><bdo id='9JWG3'><pre id='9JWG3'><center id='9JWG3'></center></pre></bdo></b><th id='9JWG3'></th></span></q></dt></tr></i><div id='9JWG3'><tfoot id='9JWG3'></tfoot><dl id='9JWG3'><fieldset id='9JWG3'></fieldset></dl></div>
    1. <legend id='9JWG3'><style id='9JWG3'><dir id='9JWG3'><q id='9JWG3'></q></dir></style></legend>
    2. <small id='9JWG3'></small><noframes id='9JWG3'>

        <bdo id='9JWG3'></bdo><ul id='9JWG3'></ul>
      <tfoot id='9JWG3'></tfoot>

      有没有办法在Matplotlib中创建不连续的轴?

      时间:2024-04-20
      <tfoot id='lEQ2S'></tfoot>
        • <bdo id='lEQ2S'></bdo><ul id='lEQ2S'></ul>
            <tbody id='lEQ2S'></tbody>

          <small id='lEQ2S'></small><noframes id='lEQ2S'>

                <legend id='lEQ2S'><style id='lEQ2S'><dir id='lEQ2S'><q id='lEQ2S'></q></dir></style></legend>
              1. <i id='lEQ2S'><tr id='lEQ2S'><dt id='lEQ2S'><q id='lEQ2S'><span id='lEQ2S'><b id='lEQ2S'><form id='lEQ2S'><ins id='lEQ2S'></ins><ul id='lEQ2S'></ul><sub id='lEQ2S'></sub></form><legend id='lEQ2S'></legend><bdo id='lEQ2S'><pre id='lEQ2S'><center id='lEQ2S'></center></pre></bdo></b><th id='lEQ2S'></th></span></q></dt></tr></i><div id='lEQ2S'><tfoot id='lEQ2S'></tfoot><dl id='lEQ2S'><fieldset id='lEQ2S'></fieldset></dl></div>
                本文介绍了有没有办法在Matplotlib中创建不连续的轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用具有不连续x轴的pylot创建绘图。通常的绘制方式是,轴的形状如下所示:

                (值)-//-(后面的值)

                其中//表示您正在跳过(值)和(后面的值)之间的所有内容。

                我还没有找到任何这样的例子,所以我想知道这是否可能。我知道您可以通过不连续连接数据,例如金融数据,但我想让轴上的跳跃更清晰。目前我只是使用子图,但我真的希望所有东西最终都出现在同一张图上。

                推荐答案

                保罗的回答是这样做的一个非常好的方法。

                但是,如果您不想进行自定义转换,您可以只使用两个子图来创建相同的效果。

                不是从头开始编写示例,matplotlib示例中有an excellent example of this written by Paul Ivanov(它只出现在当前的git技巧中,因为它是几个月前才提交的。它还没有出现在网页上。)

                这只是对此示例的简单修改,使其具有不连续的x轴而不是y轴。(这就是我将此帖子设为CW的原因)

                基本上,您只需这样做:

                import matplotlib.pylab as plt
                import numpy as np
                
                # If you're not familiar with np.r_, don't worry too much about this. It's just 
                # a series with points from 0 to 1 spaced at 0.1, and 9 to 10 with the same spacing.
                x = np.r_[0:1:0.1, 9:10:0.1]
                y = np.sin(x)
                
                fig,(ax,ax2) = plt.subplots(1, 2, sharey=True)
                
                # plot the same data on both axes
                ax.plot(x, y, 'bo')
                ax2.plot(x, y, 'bo')
                
                # zoom-in / limit the view to different portions of the data
                ax.set_xlim(0,1) # most of the data
                ax2.set_xlim(9,10) # outliers only
                
                # hide the spines between ax and ax2
                ax.spines['right'].set_visible(False)
                ax2.spines['left'].set_visible(False)
                ax.yaxis.tick_left()
                ax.tick_params(labeltop='off') # don't put tick labels at the top
                ax2.yaxis.tick_right()
                
                # Make the spacing between the two axes a bit smaller
                plt.subplots_adjust(wspace=0.15)
                
                plt.show()
                

                要添加虚线//效果,我们可以这样做(同样,修改自Paul Ivanov的示例):

                import matplotlib.pylab as plt
                import numpy as np
                
                # If you're not familiar with np.r_, don't worry too much about this. It's just 
                # a series with points from 0 to 1 spaced at 0.1, and 9 to 10 with the same spacing.
                x = np.r_[0:1:0.1, 9:10:0.1]
                y = np.sin(x)
                
                fig,(ax,ax2) = plt.subplots(1, 2, sharey=True)
                
                # plot the same data on both axes
                ax.plot(x, y, 'bo')
                ax2.plot(x, y, 'bo')
                
                # zoom-in / limit the view to different portions of the data
                ax.set_xlim(0,1) # most of the data
                ax2.set_xlim(9,10) # outliers only
                
                # hide the spines between ax and ax2
                ax.spines['right'].set_visible(False)
                ax2.spines['left'].set_visible(False)
                ax.yaxis.tick_left()
                ax.tick_params(labeltop='off') # don't put tick labels at the top
                ax2.yaxis.tick_right()
                
                # Make the spacing between the two axes a bit smaller
                plt.subplots_adjust(wspace=0.15)
                
                # This looks pretty good, and was fairly painless, but you can get that
                # cut-out diagonal lines look with just a bit more work. The important
                # thing to know here is that in axes coordinates, which are always
                # between 0-1, spine endpoints are at these locations (0,0), (0,1),
                # (1,0), and (1,1). Thus, we just need to put the diagonals in the
                # appropriate corners of each of our axes, and so long as we use the
                # right transform and disable clipping.
                
                d = .015 # how big to make the diagonal lines in axes coordinates
                # arguments to pass plot, just so we don't keep repeating them
                kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
                ax.plot((1-d,1+d),(-d,+d), **kwargs) # top-left diagonal
                ax.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-left diagonal
                
                kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
                ax2.plot((-d,d),(-d,+d), **kwargs) # top-right diagonal
                ax2.plot((-d,d),(1-d,1+d), **kwargs) # bottom-right diagonal
                
                # What's cool about this is that now if we vary the distance between
                # ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),
                # the diagonal lines will move accordingly, and stay right at the tips
                # of the spines they are 'breaking'
                
                plt.show()
                

                这篇关于有没有办法在Matplotlib中创建不连续的轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在matplotlib中更新绘图 下一篇:我可以在matplotlib中画一条多色线吗?

                相关文章

                • <bdo id='BaknL'></bdo><ul id='BaknL'></ul>
                  <i id='BaknL'><tr id='BaknL'><dt id='BaknL'><q id='BaknL'><span id='BaknL'><b id='BaknL'><form id='BaknL'><ins id='BaknL'></ins><ul id='BaknL'></ul><sub id='BaknL'></sub></form><legend id='BaknL'></legend><bdo id='BaknL'><pre id='BaknL'><center id='BaknL'></center></pre></bdo></b><th id='BaknL'></th></span></q></dt></tr></i><div id='BaknL'><tfoot id='BaknL'></tfoot><dl id='BaknL'><fieldset id='BaknL'></fieldset></dl></div>
                1. <small id='BaknL'></small><noframes id='BaknL'>

                  <tfoot id='BaknL'></tfoot>

                    <legend id='BaknL'><style id='BaknL'><dir id='BaknL'><q id='BaknL'></q></dir></style></legend>