title: How to make points representing a 2D line denser in Python
body: <pre><code># imports
  import numpy as np
  import matplotlib.pyplot as plt
  </code></pre>
  <hr />
  <p><strong>Problem</strong></p>
  <p>I have  a 2 arrays, representing datapoints on a line. For example:</p>
  <pre><code>x = np.array([0,1,2])
  y = np.array([5,6,4])
  </code></pre>
  <p>Plot the data:</p>
  <pre><code>plt.plot(x,y)
  plt.scatter(x,y,c='r')
  </code></pre>
  <p>Get:</p>
  <p><a href="https://i.stack.imgur.com/pFrcm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pFrcm.png" alt="enter image description here" /></a></p>
  <p>I'd like to have arrays where the datapoints represent the same lines as above, but they are doubled. The desired result is:</p>
  <pre><code>new_x = np.array([0,0.5,1,1.5,2]) # this is the result I am looking for
  new_y = np.array([5,5.5,6,5,4]) # this is the result I am looking for
  </code></pre>
  <p>Plot:</p>
  <pre><code>plt.plot(new_x,new_y)
  plt.scatter(new_x,new_y,c='r')
  </code></pre>
  <p>Result:</p>
  <p><a href="https://i.stack.imgur.com/eJdsg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eJdsg.png" alt="enter image description here" /></a></p>
  <hr />
  <p><strong>My attempt</strong></p>
  <p>My method of doing this:</p>
  <pre><code>x = np.array([0,1,2])
  y = np.array([5,6,4])
  
  new_x=[]
  for index, each in enumerate(x):
      if index!=0:
          new_x.append((each+x[index-1])/2)
  
  new_y=[]
  for index, each in enumerate(y):
      if index!=0:
          new_y.append((each+y[index-1])/2)
  
  new_x.extend(x)
  new_y.extend(y)
  </code></pre>
  <p>Plot:</p>
  <pre><code>plt.plot(new_x,new_y)
  plt.scatter(new_x,new_y,c='r')
  </code></pre>
  <p>Result:</p>
  <p><a href="https://i.stack.imgur.com/zDQeB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zDQeB.png" alt="enter image description here" /></a></p>
  <p>Points are in the right place, the new points added are the correct points, but they are put into the <code>new_x</code> &amp; <code>new_y</code> arrays in the wrong order. It is fixable by paying more attention to the <code>.extend</code>, but this does not seem a good method to me anyway, because of the iteration.</p>
  <hr />
  <p><strong>My research</strong></p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/38282659/linear-interpolation-between-two-data-points">linear interpolation between two data points</a>: doesn't use 2D arrays</li>
  <li><a href="https://stackoverflow.com/questions/69294496/how-to-write-a-function-that-returns-an-interpolated-value-pandas-dataframe">How to write a function that returns an interpolated value (pandas dataframe)?</a>: deals with interpolated values of functions, not resampling lines as above</li>
  </ul>
  <p>And there are some other questions with the <code>[interpolation]</code> tag, but I haven't found one descibing this problem above.</p>
  <hr />
  <p><strong>Question</strong></p>
  <p><strong>How do I get the above described <code>new_x</code> and <code>new_y</code>, preferably without iteration?</strong></p>