Strange plot created based on two numpy arrays and matplotlib












1















Now I just want to plot a line graph based on two numpy arrays. My x and y are both two (150,1) arrays. After running the following code:



plt.plot(x,y)


What I get is:
Line graph based on two numpy arrays



Hence I am so confused. What do those connected lines represent? I just want one line which goes through all of the points. Any help would be appreciated!





For the dataset, X is just a fixed (150,1) numpy array and y is computed based on the following polynomial function:



def PolyCoefficients(x, coeffs):
""" Returns a polynomial for ``x`` values for the ``coeffs`` provided.

The coefficients must be in ascending order (``x**0`` to ``x**o``).
"""
o = len(coeffs)
y =
for i in range(len(x)):
value = 0
for j in range(o):
value += coeffs[j]*x[i]**j
y.append(value)
return y


The coefficients have been computed and what I want is just a line go through each point of (x,y)










share|improve this question





























    1















    Now I just want to plot a line graph based on two numpy arrays. My x and y are both two (150,1) arrays. After running the following code:



    plt.plot(x,y)


    What I get is:
    Line graph based on two numpy arrays



    Hence I am so confused. What do those connected lines represent? I just want one line which goes through all of the points. Any help would be appreciated!





    For the dataset, X is just a fixed (150,1) numpy array and y is computed based on the following polynomial function:



    def PolyCoefficients(x, coeffs):
    """ Returns a polynomial for ``x`` values for the ``coeffs`` provided.

    The coefficients must be in ascending order (``x**0`` to ``x**o``).
    """
    o = len(coeffs)
    y =
    for i in range(len(x)):
    value = 0
    for j in range(o):
    value += coeffs[j]*x[i]**j
    y.append(value)
    return y


    The coefficients have been computed and what I want is just a line go through each point of (x,y)










    share|improve this question



























      1












      1








      1








      Now I just want to plot a line graph based on two numpy arrays. My x and y are both two (150,1) arrays. After running the following code:



      plt.plot(x,y)


      What I get is:
      Line graph based on two numpy arrays



      Hence I am so confused. What do those connected lines represent? I just want one line which goes through all of the points. Any help would be appreciated!





      For the dataset, X is just a fixed (150,1) numpy array and y is computed based on the following polynomial function:



      def PolyCoefficients(x, coeffs):
      """ Returns a polynomial for ``x`` values for the ``coeffs`` provided.

      The coefficients must be in ascending order (``x**0`` to ``x**o``).
      """
      o = len(coeffs)
      y =
      for i in range(len(x)):
      value = 0
      for j in range(o):
      value += coeffs[j]*x[i]**j
      y.append(value)
      return y


      The coefficients have been computed and what I want is just a line go through each point of (x,y)










      share|improve this question
















      Now I just want to plot a line graph based on two numpy arrays. My x and y are both two (150,1) arrays. After running the following code:



      plt.plot(x,y)


      What I get is:
      Line graph based on two numpy arrays



      Hence I am so confused. What do those connected lines represent? I just want one line which goes through all of the points. Any help would be appreciated!





      For the dataset, X is just a fixed (150,1) numpy array and y is computed based on the following polynomial function:



      def PolyCoefficients(x, coeffs):
      """ Returns a polynomial for ``x`` values for the ``coeffs`` provided.

      The coefficients must be in ascending order (``x**0`` to ``x**o``).
      """
      o = len(coeffs)
      y =
      for i in range(len(x)):
      value = 0
      for j in range(o):
      value += coeffs[j]*x[i]**j
      y.append(value)
      return y


      The coefficients have been computed and what I want is just a line go through each point of (x,y)







      python numpy matplotlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 7:08









      Joe

      6,11421630




      6,11421630










      asked Nov 23 '18 at 6:50









      Bright ChangBright Chang

      227




      227
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The pairs of x and y represent the points on your graph. With plt.plot() you join the points with a line. If the array x is not in order, what you have is a line that goes back and forward across the graph. To avoid this you should order the x array, and the y accordly. Try with:



          new_x, new_y = zip(*sorted(zip(x, y)))
          plt.plot(new_x,new_y)





          share|improve this answer


























          • Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?

            – Bright Chang
            Nov 23 '18 at 6:58











          • If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works

            – Joe
            Nov 23 '18 at 7:03











          • You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

            – Joe
            Nov 23 '18 at 7:06











          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%2f53441874%2fstrange-plot-created-based-on-two-numpy-arrays-and-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 pairs of x and y represent the points on your graph. With plt.plot() you join the points with a line. If the array x is not in order, what you have is a line that goes back and forward across the graph. To avoid this you should order the x array, and the y accordly. Try with:



          new_x, new_y = zip(*sorted(zip(x, y)))
          plt.plot(new_x,new_y)





          share|improve this answer


























          • Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?

            – Bright Chang
            Nov 23 '18 at 6:58











          • If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works

            – Joe
            Nov 23 '18 at 7:03











          • You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

            – Joe
            Nov 23 '18 at 7:06
















          1














          The pairs of x and y represent the points on your graph. With plt.plot() you join the points with a line. If the array x is not in order, what you have is a line that goes back and forward across the graph. To avoid this you should order the x array, and the y accordly. Try with:



          new_x, new_y = zip(*sorted(zip(x, y)))
          plt.plot(new_x,new_y)





          share|improve this answer


























          • Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?

            – Bright Chang
            Nov 23 '18 at 6:58











          • If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works

            – Joe
            Nov 23 '18 at 7:03











          • You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

            – Joe
            Nov 23 '18 at 7:06














          1












          1








          1







          The pairs of x and y represent the points on your graph. With plt.plot() you join the points with a line. If the array x is not in order, what you have is a line that goes back and forward across the graph. To avoid this you should order the x array, and the y accordly. Try with:



          new_x, new_y = zip(*sorted(zip(x, y)))
          plt.plot(new_x,new_y)





          share|improve this answer















          The pairs of x and y represent the points on your graph. With plt.plot() you join the points with a line. If the array x is not in order, what you have is a line that goes back and forward across the graph. To avoid this you should order the x array, and the y accordly. Try with:



          new_x, new_y = zip(*sorted(zip(x, y)))
          plt.plot(new_x,new_y)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 7:03

























          answered Nov 23 '18 at 6:53









          JoeJoe

          6,11421630




          6,11421630













          • Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?

            – Bright Chang
            Nov 23 '18 at 6:58











          • If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works

            – Joe
            Nov 23 '18 at 7:03











          • You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

            – Joe
            Nov 23 '18 at 7:06



















          • Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?

            – Bright Chang
            Nov 23 '18 at 6:58











          • If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works

            – Joe
            Nov 23 '18 at 7:03











          • You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

            – Joe
            Nov 23 '18 at 7:06

















          Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?

          – Bright Chang
          Nov 23 '18 at 6:58





          Thank you for your answer. But actually my x is fixed and y is just the output from a polynomial function based on x. The coefficients have already been computed... Does the order of these two arrays change during the computation?

          – Bright Chang
          Nov 23 '18 at 6:58













          If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works

          – Joe
          Nov 23 '18 at 7:03





          If you have just these 2 lists, order them for the graph. I've added some code to the answer, try it and see if it works

          – Joe
          Nov 23 '18 at 7:03













          You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

          – Joe
          Nov 23 '18 at 7:06





          You are welcome, dont forget to acecpt the answer :) meta.stackexchange.com/questions/5234/…

          – Joe
          Nov 23 '18 at 7:06




















          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%2f53441874%2fstrange-plot-created-based-on-two-numpy-arrays-and-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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?