How do I draw a line connecting subplots in pyplot?









up vote
0
down vote

favorite












I would like to create lines between subplots in pyplot as illustrated by the red dashed lines here (added in a pdf editor).



enter image description here



I've read documentation on connectionpatch, but I'm having difficulty making sense of the examples well enough to translate to my particular case. For my case, I've included a simplified version of my code, using the same axes structures in case that is relevant. How do I create these dashed lines between subplots?



import numpy as np
import matplotlib.pyplot as plt

# Create a 2 x 2 grid: (row, column)
fig, ax = plt.subplots(2,2)

# Create a subplot to share common x and y labels
fig.add_subplot(111, frameon=False)
plt.tick_params(
top='off',
bottom='off',
left='off',
right='off')
plt.grid(False)
plt.xlabel('x')
plt.ylabel('function(x)')

# x-axis
x = np.linspace(0,2*np.pi,100)

# Top left
ax[0,0].tick_params(
axis='both',
which='both',
bottom=False,
left=False,
top=False,
right=False,
labelbottom=False,
labelleft=False,)
ax[0,0].plot(x,np.sin(x),color='grey')

# Top Right
ax[0,1].tick_params(
axis='both',
which='both',
bottom=False,
left=False,
top=False,
right=False,
labelbottom=False,
labelleft=False,)
ax[0,1].plot(x,np.sin(2*x),color='grey')


# Bottom Left
ax[1,0].tick_params(
axis='both',
which='both',
bottom=False,
left=False,
top=False,
right=False,
labelbottom=False,
labelleft=False,)
ax[1,0].plot(x,np.cos(x), color='black')

# Bottom Right
ax[1,1].tick_params(
axis='both',
which='both',
bottom=False,
left=False,
top=False,
right=False,
labelbottom=False,
labelleft=False,)
ax[1,1].plot(x,np.cos(2*x), color='black')

plt.tight_layout(h_pad=2.5)









share|improve this question



























    up vote
    0
    down vote

    favorite












    I would like to create lines between subplots in pyplot as illustrated by the red dashed lines here (added in a pdf editor).



    enter image description here



    I've read documentation on connectionpatch, but I'm having difficulty making sense of the examples well enough to translate to my particular case. For my case, I've included a simplified version of my code, using the same axes structures in case that is relevant. How do I create these dashed lines between subplots?



    import numpy as np
    import matplotlib.pyplot as plt

    # Create a 2 x 2 grid: (row, column)
    fig, ax = plt.subplots(2,2)

    # Create a subplot to share common x and y labels
    fig.add_subplot(111, frameon=False)
    plt.tick_params(
    top='off',
    bottom='off',
    left='off',
    right='off')
    plt.grid(False)
    plt.xlabel('x')
    plt.ylabel('function(x)')

    # x-axis
    x = np.linspace(0,2*np.pi,100)

    # Top left
    ax[0,0].tick_params(
    axis='both',
    which='both',
    bottom=False,
    left=False,
    top=False,
    right=False,
    labelbottom=False,
    labelleft=False,)
    ax[0,0].plot(x,np.sin(x),color='grey')

    # Top Right
    ax[0,1].tick_params(
    axis='both',
    which='both',
    bottom=False,
    left=False,
    top=False,
    right=False,
    labelbottom=False,
    labelleft=False,)
    ax[0,1].plot(x,np.sin(2*x),color='grey')


    # Bottom Left
    ax[1,0].tick_params(
    axis='both',
    which='both',
    bottom=False,
    left=False,
    top=False,
    right=False,
    labelbottom=False,
    labelleft=False,)
    ax[1,0].plot(x,np.cos(x), color='black')

    # Bottom Right
    ax[1,1].tick_params(
    axis='both',
    which='both',
    bottom=False,
    left=False,
    top=False,
    right=False,
    labelbottom=False,
    labelleft=False,)
    ax[1,1].plot(x,np.cos(2*x), color='black')

    plt.tight_layout(h_pad=2.5)









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I would like to create lines between subplots in pyplot as illustrated by the red dashed lines here (added in a pdf editor).



      enter image description here



      I've read documentation on connectionpatch, but I'm having difficulty making sense of the examples well enough to translate to my particular case. For my case, I've included a simplified version of my code, using the same axes structures in case that is relevant. How do I create these dashed lines between subplots?



      import numpy as np
      import matplotlib.pyplot as plt

      # Create a 2 x 2 grid: (row, column)
      fig, ax = plt.subplots(2,2)

      # Create a subplot to share common x and y labels
      fig.add_subplot(111, frameon=False)
      plt.tick_params(
      top='off',
      bottom='off',
      left='off',
      right='off')
      plt.grid(False)
      plt.xlabel('x')
      plt.ylabel('function(x)')

      # x-axis
      x = np.linspace(0,2*np.pi,100)

      # Top left
      ax[0,0].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[0,0].plot(x,np.sin(x),color='grey')

      # Top Right
      ax[0,1].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[0,1].plot(x,np.sin(2*x),color='grey')


      # Bottom Left
      ax[1,0].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[1,0].plot(x,np.cos(x), color='black')

      # Bottom Right
      ax[1,1].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[1,1].plot(x,np.cos(2*x), color='black')

      plt.tight_layout(h_pad=2.5)









      share|improve this question















      I would like to create lines between subplots in pyplot as illustrated by the red dashed lines here (added in a pdf editor).



      enter image description here



      I've read documentation on connectionpatch, but I'm having difficulty making sense of the examples well enough to translate to my particular case. For my case, I've included a simplified version of my code, using the same axes structures in case that is relevant. How do I create these dashed lines between subplots?



      import numpy as np
      import matplotlib.pyplot as plt

      # Create a 2 x 2 grid: (row, column)
      fig, ax = plt.subplots(2,2)

      # Create a subplot to share common x and y labels
      fig.add_subplot(111, frameon=False)
      plt.tick_params(
      top='off',
      bottom='off',
      left='off',
      right='off')
      plt.grid(False)
      plt.xlabel('x')
      plt.ylabel('function(x)')

      # x-axis
      x = np.linspace(0,2*np.pi,100)

      # Top left
      ax[0,0].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[0,0].plot(x,np.sin(x),color='grey')

      # Top Right
      ax[0,1].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[0,1].plot(x,np.sin(2*x),color='grey')


      # Bottom Left
      ax[1,0].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[1,0].plot(x,np.cos(x), color='black')

      # Bottom Right
      ax[1,1].tick_params(
      axis='both',
      which='both',
      bottom=False,
      left=False,
      top=False,
      right=False,
      labelbottom=False,
      labelleft=False,)
      ax[1,1].plot(x,np.cos(2*x), color='black')

      plt.tight_layout(h_pad=2.5)






      python python-3.x matplotlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 3:42









      eyllanesc

      72.4k93054




      72.4k93054










      asked Nov 11 at 3:29









      astromonerd

      410820




      410820






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          The ConnectionPatch example shows how to use a ConnectionPatch to connect two axes. For your case you would do it like this:



          import numpy as np
          import matplotlib.pyplot as plt
          from matplotlib.patches import ConnectionPatch

          fig, axes = plt.subplots(2,2)

          # Create a subplot to share common x and y labels
          frameax = fig.add_subplot(111, frameon=False)
          frameax.grid(False)
          frameax.set_xlabel('x', labelpad=10)
          frameax.set_ylabel('function(x)',labelpad=10)

          for ax in list(axes.flat) + [frameax]:
          ax.tick_params(axis='both', which='both',
          bottom=False, left=False, top=False, right=False,
          labelbottom=False, labelleft=False)

          # x-axis
          x = np.linspace(0,2*np.pi,100)

          axes[0,0].plot(x,np.sin(x),color='grey')
          axes[0,1].plot(x,np.sin(2*x),color='grey')
          axes[1,0].plot(x,np.cos(x), color='black')
          axes[1,1].plot(x,np.cos(2*x), color='black')

          kw = dict(linestyle="--", color="red")
          cp1 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,0], axesB=axes[1,0], **kw)
          cp2 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,1], axesB=axes[1,1], **kw)

          for cp in (cp1, cp2):
          axes[1,1].add_artist(cp)

          plt.show()


          enter image description here






          share|improve this answer






















          • This works, thank you. And for cleaning up my tick_params.
            – astromonerd
            Nov 11 at 16:25










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245596%2fhow-do-i-draw-a-line-connecting-subplots-in-pyplot%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          The ConnectionPatch example shows how to use a ConnectionPatch to connect two axes. For your case you would do it like this:



          import numpy as np
          import matplotlib.pyplot as plt
          from matplotlib.patches import ConnectionPatch

          fig, axes = plt.subplots(2,2)

          # Create a subplot to share common x and y labels
          frameax = fig.add_subplot(111, frameon=False)
          frameax.grid(False)
          frameax.set_xlabel('x', labelpad=10)
          frameax.set_ylabel('function(x)',labelpad=10)

          for ax in list(axes.flat) + [frameax]:
          ax.tick_params(axis='both', which='both',
          bottom=False, left=False, top=False, right=False,
          labelbottom=False, labelleft=False)

          # x-axis
          x = np.linspace(0,2*np.pi,100)

          axes[0,0].plot(x,np.sin(x),color='grey')
          axes[0,1].plot(x,np.sin(2*x),color='grey')
          axes[1,0].plot(x,np.cos(x), color='black')
          axes[1,1].plot(x,np.cos(2*x), color='black')

          kw = dict(linestyle="--", color="red")
          cp1 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,0], axesB=axes[1,0], **kw)
          cp2 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,1], axesB=axes[1,1], **kw)

          for cp in (cp1, cp2):
          axes[1,1].add_artist(cp)

          plt.show()


          enter image description here






          share|improve this answer






















          • This works, thank you. And for cleaning up my tick_params.
            – astromonerd
            Nov 11 at 16:25














          up vote
          2
          down vote



          accepted










          The ConnectionPatch example shows how to use a ConnectionPatch to connect two axes. For your case you would do it like this:



          import numpy as np
          import matplotlib.pyplot as plt
          from matplotlib.patches import ConnectionPatch

          fig, axes = plt.subplots(2,2)

          # Create a subplot to share common x and y labels
          frameax = fig.add_subplot(111, frameon=False)
          frameax.grid(False)
          frameax.set_xlabel('x', labelpad=10)
          frameax.set_ylabel('function(x)',labelpad=10)

          for ax in list(axes.flat) + [frameax]:
          ax.tick_params(axis='both', which='both',
          bottom=False, left=False, top=False, right=False,
          labelbottom=False, labelleft=False)

          # x-axis
          x = np.linspace(0,2*np.pi,100)

          axes[0,0].plot(x,np.sin(x),color='grey')
          axes[0,1].plot(x,np.sin(2*x),color='grey')
          axes[1,0].plot(x,np.cos(x), color='black')
          axes[1,1].plot(x,np.cos(2*x), color='black')

          kw = dict(linestyle="--", color="red")
          cp1 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,0], axesB=axes[1,0], **kw)
          cp2 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,1], axesB=axes[1,1], **kw)

          for cp in (cp1, cp2):
          axes[1,1].add_artist(cp)

          plt.show()


          enter image description here






          share|improve this answer






















          • This works, thank you. And for cleaning up my tick_params.
            – astromonerd
            Nov 11 at 16:25












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The ConnectionPatch example shows how to use a ConnectionPatch to connect two axes. For your case you would do it like this:



          import numpy as np
          import matplotlib.pyplot as plt
          from matplotlib.patches import ConnectionPatch

          fig, axes = plt.subplots(2,2)

          # Create a subplot to share common x and y labels
          frameax = fig.add_subplot(111, frameon=False)
          frameax.grid(False)
          frameax.set_xlabel('x', labelpad=10)
          frameax.set_ylabel('function(x)',labelpad=10)

          for ax in list(axes.flat) + [frameax]:
          ax.tick_params(axis='both', which='both',
          bottom=False, left=False, top=False, right=False,
          labelbottom=False, labelleft=False)

          # x-axis
          x = np.linspace(0,2*np.pi,100)

          axes[0,0].plot(x,np.sin(x),color='grey')
          axes[0,1].plot(x,np.sin(2*x),color='grey')
          axes[1,0].plot(x,np.cos(x), color='black')
          axes[1,1].plot(x,np.cos(2*x), color='black')

          kw = dict(linestyle="--", color="red")
          cp1 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,0], axesB=axes[1,0], **kw)
          cp2 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,1], axesB=axes[1,1], **kw)

          for cp in (cp1, cp2):
          axes[1,1].add_artist(cp)

          plt.show()


          enter image description here






          share|improve this answer














          The ConnectionPatch example shows how to use a ConnectionPatch to connect two axes. For your case you would do it like this:



          import numpy as np
          import matplotlib.pyplot as plt
          from matplotlib.patches import ConnectionPatch

          fig, axes = plt.subplots(2,2)

          # Create a subplot to share common x and y labels
          frameax = fig.add_subplot(111, frameon=False)
          frameax.grid(False)
          frameax.set_xlabel('x', labelpad=10)
          frameax.set_ylabel('function(x)',labelpad=10)

          for ax in list(axes.flat) + [frameax]:
          ax.tick_params(axis='both', which='both',
          bottom=False, left=False, top=False, right=False,
          labelbottom=False, labelleft=False)

          # x-axis
          x = np.linspace(0,2*np.pi,100)

          axes[0,0].plot(x,np.sin(x),color='grey')
          axes[0,1].plot(x,np.sin(2*x),color='grey')
          axes[1,0].plot(x,np.cos(x), color='black')
          axes[1,1].plot(x,np.cos(2*x), color='black')

          kw = dict(linestyle="--", color="red")
          cp1 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,0], axesB=axes[1,0], **kw)
          cp2 = ConnectionPatch((.5, 0), (.5, 1), "axes fraction", "axes fraction",
          axesA=axes[0,1], axesB=axes[1,1], **kw)

          for cp in (cp1, cp2):
          axes[1,1].add_artist(cp)

          plt.show()


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 4:01

























          answered Nov 11 at 3:54









          ImportanceOfBeingErnest

          124k10127203




          124k10127203











          • This works, thank you. And for cleaning up my tick_params.
            – astromonerd
            Nov 11 at 16:25
















          • This works, thank you. And for cleaning up my tick_params.
            – astromonerd
            Nov 11 at 16:25















          This works, thank you. And for cleaning up my tick_params.
          – astromonerd
          Nov 11 at 16:25




          This works, thank you. And for cleaning up my tick_params.
          – astromonerd
          Nov 11 at 16:25

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245596%2fhow-do-i-draw-a-line-connecting-subplots-in-pyplot%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo