3

I am still trying to find a solution to my problem described here: matplotlib: assign color to a radius. I tried it first with contourplot but I think it is a better solution when I try to plot circles on the surface of the disk and assign colors to them. Therefore I have an array:

arr = np.array([[ 114.28, 14],
            [ 128.57, 16],
            [ 142.85,19],
            [ 157.13,20],
            [ 171.41,21],
            [ 185.69,22],
            [ 199.97,24],
            [ 214.25,27],
            [ 228.53,29],
            [ 242.81,30],
            [ 257.09,31],
            [ 271.37,34],
            [ 288.65,35],
            [ 299.93,36],
            [ 300,38]])

I would like to extend this array to an array with about 300 elements (depends on the radius of the disk. If the radius would be e.g. 500, I want to have an array with 500 elements)by interpolating between the values of the array linearly. I need an interpolation of both columns. I found this article: Interpolate between elements in an array of floats but I don't understand the code as they are talking about milliseconds and LED's... Thanks in advance !

sacuL
  • 49,704
  • 8
  • 81
  • 106
roflcopter122
  • 165
  • 1
  • 14

1 Answers1

3

IIUC, I think that you can just use np.interp to interpolate your points. From the documentations, np.interp is used for:

One-dimensional linear interpolation.

Which sounds just about like what you're after.

So you can create an array of 300 evenly spaced points from your minimum x value to your maximum x value using np.linspace:

new_x = np.linspace(min(arr[:,0]), max(arr[:,0]), num=300)

And then interpolate your new y values:

new_y = np.interp(new_x, arr[:,0], arr[:,1])

To illustrate graphically:

# Show the original 15 points:
plt.scatter(arr[:,0], arr[:,1], label='original', zorder=10)

# Show the interpolated 300 points:
plt.scatter(new_x, new_y, label='interpolated', s=0.5)

plt.legend()

plt.show()

enter image description here

Edit Based on your comment, to interpolate exactly 20 points between each datapoint in your array, you can create your new x axis by iterating through your array and applying linspace to get 20 points between each consecutive x value. However, this will yield 280 points, because you will be creating 20 points between 15 of your datapoints, leading to 20*(15-1) new datapoints:

new_x = np.concatenate([np.linspace(arr[i,0],arr[i+1,0], num=20)
                        for i in range(len(arr)-1)])

new_y = np.interp(new_x, arr[:,0], arr[:,1])


# Show the original 15 points:
plt.scatter(arr[:,0], arr[:,1], label='original', zorder=10)

# Show the interpolated 280 points:
plt.scatter(new_x, new_y, label='interpolated', s=0.5)

plt.legend()

plt.show()

enter image description here

sacuL
  • 49,704
  • 8
  • 81
  • 106
  • 1
    Thanks for the answer ! I think I explained it a little bit wrong. I saw this python function too. But I don't want to interpolate between min and max but between the single elements. Since I want an array of 300 elements, between each element I need about 20 interpolated values. It means I would like to interpolate between 114.28 and 128.57 (20 times) and between 14 and 16 (also 20 times) and so on.... – roflcopter122 Aug 06 '18 at 19:56
  • That is possible, but if you do that, it will lead to 280 points, because you will have 20 points between the first and second, 20 between the second and third, and so on until `n-1`, because you can only do that up to getting 20 points between the second to last and the last points. – sacuL Aug 06 '18 at 20:12
  • Sure, but then you'll have 308 points. To do that simply change `num=20` to `num=22` in the calls to `linspace` – sacuL Aug 06 '18 at 20:25
  • This community is just great, thank you it helped me a lot !!! I have one last problem regarding my plot (I need a colorbar). What do you think, should I ask a new question or should I ask it here? Best regards ! – roflcopter122 Aug 06 '18 at 20:37
  • You're welcome! Glad to help. Since you didn't ask about colorbars in your question, I would suggest asking a separate, specific question. Good luck! – sacuL Aug 06 '18 at 20:38