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 !
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With