How to deal with the “pixelization” of overlapping curves in Matplotlib












0















I have the following problem: when I plot two or more curves (of the same linewidth) they look "pixelized" at the regions where they overlap.
Here's an example:



x = np.linspace(0, np.pi, 100)
y1 = np.sin(x)
y2 = np.zeros(x.shape)
y2[:51] = np.sin(x[:51])
y2[51:] = 2 - np.sin(x[51:])

fig, ax = plt.subplots(1, 1, figsize=(3,3))
ax.plot(x, y1, lw=4, color='k')
ax.plot(x, y2, lw=4, color='yellow')
plt.show()


enter image description here



To my understanding, this is happening because the pixels at the curve edges have some level of transparency which makes them more smooth-looking. However, there is this side effect in the case of overlapping curves: the edge-pixels of the first curve are visible through the edge-pixels of the second curve.



How to deal with this side effect? One idea is to increase the width of the last curve, however this solution is not ideal for me because I would like the curves to have exactly the same width at the regions where they do not overlap.










share|improve this question





























    0















    I have the following problem: when I plot two or more curves (of the same linewidth) they look "pixelized" at the regions where they overlap.
    Here's an example:



    x = np.linspace(0, np.pi, 100)
    y1 = np.sin(x)
    y2 = np.zeros(x.shape)
    y2[:51] = np.sin(x[:51])
    y2[51:] = 2 - np.sin(x[51:])

    fig, ax = plt.subplots(1, 1, figsize=(3,3))
    ax.plot(x, y1, lw=4, color='k')
    ax.plot(x, y2, lw=4, color='yellow')
    plt.show()


    enter image description here



    To my understanding, this is happening because the pixels at the curve edges have some level of transparency which makes them more smooth-looking. However, there is this side effect in the case of overlapping curves: the edge-pixels of the first curve are visible through the edge-pixels of the second curve.



    How to deal with this side effect? One idea is to increase the width of the last curve, however this solution is not ideal for me because I would like the curves to have exactly the same width at the regions where they do not overlap.










    share|improve this question



























      0












      0








      0








      I have the following problem: when I plot two or more curves (of the same linewidth) they look "pixelized" at the regions where they overlap.
      Here's an example:



      x = np.linspace(0, np.pi, 100)
      y1 = np.sin(x)
      y2 = np.zeros(x.shape)
      y2[:51] = np.sin(x[:51])
      y2[51:] = 2 - np.sin(x[51:])

      fig, ax = plt.subplots(1, 1, figsize=(3,3))
      ax.plot(x, y1, lw=4, color='k')
      ax.plot(x, y2, lw=4, color='yellow')
      plt.show()


      enter image description here



      To my understanding, this is happening because the pixels at the curve edges have some level of transparency which makes them more smooth-looking. However, there is this side effect in the case of overlapping curves: the edge-pixels of the first curve are visible through the edge-pixels of the second curve.



      How to deal with this side effect? One idea is to increase the width of the last curve, however this solution is not ideal for me because I would like the curves to have exactly the same width at the regions where they do not overlap.










      share|improve this question
















      I have the following problem: when I plot two or more curves (of the same linewidth) they look "pixelized" at the regions where they overlap.
      Here's an example:



      x = np.linspace(0, np.pi, 100)
      y1 = np.sin(x)
      y2 = np.zeros(x.shape)
      y2[:51] = np.sin(x[:51])
      y2[51:] = 2 - np.sin(x[51:])

      fig, ax = plt.subplots(1, 1, figsize=(3,3))
      ax.plot(x, y1, lw=4, color='k')
      ax.plot(x, y2, lw=4, color='yellow')
      plt.show()


      enter image description here



      To my understanding, this is happening because the pixels at the curve edges have some level of transparency which makes them more smooth-looking. However, there is this side effect in the case of overlapping curves: the edge-pixels of the first curve are visible through the edge-pixels of the second curve.



      How to deal with this side effect? One idea is to increase the width of the last curve, however this solution is not ideal for me because I would like the curves to have exactly the same width at the regions where they do not overlap.







      python matplotlib plot curve






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 8:35







      AndyK

















      asked Nov 14 '18 at 23:21









      AndyKAndyK

      995818




      995818
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The effect you describe is called antialiasing. It's usually wanted to obtain smoother images. You can turn antialiasing off on a per artist basis. (However, not all artists really obey to the setting in every backend.)



          For the plot this could look like



          import numpy as np
          import matplotlib.pyplot as plt

          x = np.linspace(0, np.pi, 100)
          y1 = np.sin(x)
          y2 = np.zeros(x.shape)
          y2[:51] = np.sin(x[:51])
          y2[51:] = 2 - np.sin(x[51:])

          fig, axes = plt.subplots(1,2, figsize=(6,3))
          for aa, ax in zip([True, False], axes.flat):
          ax.plot(x, y1, lw=4, color='k', antialiased=aa)
          ax.plot(x, y2, lw=4, color='yellow', antialiased=aa)
          plt.show()


          enter image description here



          As consequence when antialiasing is turned off, a pixel is either occupied by the respective color of the line, or not.






          share|improve this answer
























          • Thanks, I didn't know that. However setting antialiasing to False makes the curves look pixelated, so this solves one problem but creates another one. It seems that the best option is to increase the width of the second curve and keep the antialiasing.

            – AndyK
            Nov 15 '18 at 7:57













          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%2f53310246%2fhow-to-deal-with-the-pixelization-of-overlapping-curves-in-matplotlib%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









          1














          The effect you describe is called antialiasing. It's usually wanted to obtain smoother images. You can turn antialiasing off on a per artist basis. (However, not all artists really obey to the setting in every backend.)



          For the plot this could look like



          import numpy as np
          import matplotlib.pyplot as plt

          x = np.linspace(0, np.pi, 100)
          y1 = np.sin(x)
          y2 = np.zeros(x.shape)
          y2[:51] = np.sin(x[:51])
          y2[51:] = 2 - np.sin(x[51:])

          fig, axes = plt.subplots(1,2, figsize=(6,3))
          for aa, ax in zip([True, False], axes.flat):
          ax.plot(x, y1, lw=4, color='k', antialiased=aa)
          ax.plot(x, y2, lw=4, color='yellow', antialiased=aa)
          plt.show()


          enter image description here



          As consequence when antialiasing is turned off, a pixel is either occupied by the respective color of the line, or not.






          share|improve this answer
























          • Thanks, I didn't know that. However setting antialiasing to False makes the curves look pixelated, so this solves one problem but creates another one. It seems that the best option is to increase the width of the second curve and keep the antialiasing.

            – AndyK
            Nov 15 '18 at 7:57


















          1














          The effect you describe is called antialiasing. It's usually wanted to obtain smoother images. You can turn antialiasing off on a per artist basis. (However, not all artists really obey to the setting in every backend.)



          For the plot this could look like



          import numpy as np
          import matplotlib.pyplot as plt

          x = np.linspace(0, np.pi, 100)
          y1 = np.sin(x)
          y2 = np.zeros(x.shape)
          y2[:51] = np.sin(x[:51])
          y2[51:] = 2 - np.sin(x[51:])

          fig, axes = plt.subplots(1,2, figsize=(6,3))
          for aa, ax in zip([True, False], axes.flat):
          ax.plot(x, y1, lw=4, color='k', antialiased=aa)
          ax.plot(x, y2, lw=4, color='yellow', antialiased=aa)
          plt.show()


          enter image description here



          As consequence when antialiasing is turned off, a pixel is either occupied by the respective color of the line, or not.






          share|improve this answer
























          • Thanks, I didn't know that. However setting antialiasing to False makes the curves look pixelated, so this solves one problem but creates another one. It seems that the best option is to increase the width of the second curve and keep the antialiasing.

            – AndyK
            Nov 15 '18 at 7:57
















          1












          1








          1







          The effect you describe is called antialiasing. It's usually wanted to obtain smoother images. You can turn antialiasing off on a per artist basis. (However, not all artists really obey to the setting in every backend.)



          For the plot this could look like



          import numpy as np
          import matplotlib.pyplot as plt

          x = np.linspace(0, np.pi, 100)
          y1 = np.sin(x)
          y2 = np.zeros(x.shape)
          y2[:51] = np.sin(x[:51])
          y2[51:] = 2 - np.sin(x[51:])

          fig, axes = plt.subplots(1,2, figsize=(6,3))
          for aa, ax in zip([True, False], axes.flat):
          ax.plot(x, y1, lw=4, color='k', antialiased=aa)
          ax.plot(x, y2, lw=4, color='yellow', antialiased=aa)
          plt.show()


          enter image description here



          As consequence when antialiasing is turned off, a pixel is either occupied by the respective color of the line, or not.






          share|improve this answer













          The effect you describe is called antialiasing. It's usually wanted to obtain smoother images. You can turn antialiasing off on a per artist basis. (However, not all artists really obey to the setting in every backend.)



          For the plot this could look like



          import numpy as np
          import matplotlib.pyplot as plt

          x = np.linspace(0, np.pi, 100)
          y1 = np.sin(x)
          y2 = np.zeros(x.shape)
          y2[:51] = np.sin(x[:51])
          y2[51:] = 2 - np.sin(x[51:])

          fig, axes = plt.subplots(1,2, figsize=(6,3))
          for aa, ax in zip([True, False], axes.flat):
          ax.plot(x, y1, lw=4, color='k', antialiased=aa)
          ax.plot(x, y2, lw=4, color='yellow', antialiased=aa)
          plt.show()


          enter image description here



          As consequence when antialiasing is turned off, a pixel is either occupied by the respective color of the line, or not.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 23:57









          ImportanceOfBeingErnestImportanceOfBeingErnest

          134k13148224




          134k13148224













          • Thanks, I didn't know that. However setting antialiasing to False makes the curves look pixelated, so this solves one problem but creates another one. It seems that the best option is to increase the width of the second curve and keep the antialiasing.

            – AndyK
            Nov 15 '18 at 7:57





















          • Thanks, I didn't know that. However setting antialiasing to False makes the curves look pixelated, so this solves one problem but creates another one. It seems that the best option is to increase the width of the second curve and keep the antialiasing.

            – AndyK
            Nov 15 '18 at 7:57



















          Thanks, I didn't know that. However setting antialiasing to False makes the curves look pixelated, so this solves one problem but creates another one. It seems that the best option is to increase the width of the second curve and keep the antialiasing.

          – AndyK
          Nov 15 '18 at 7:57







          Thanks, I didn't know that. However setting antialiasing to False makes the curves look pixelated, so this solves one problem but creates another one. It seems that the best option is to increase the width of the second curve and keep the antialiasing.

          – AndyK
          Nov 15 '18 at 7:57






















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310246%2fhow-to-deal-with-the-pixelization-of-overlapping-curves-in-matplotlib%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python