Cara menggunakan plotting interpolation python

So, I found a solution. For future reference this code should give those with the same problem what they need. I added an 'interpolation' argument to the plot.imshow() function like:

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp2d

# fmt: off
X_COORDINATE = [1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 0.0, 0.5, 1.0, 1.1, 1.5, 2.0, 3.0, 4.0, 5.0, 0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0]
Z_COORDINATE = [0, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5]
C_I = [1.02414267447743, 0.210700871073941, 0.156586042435711, 0.109151033138569, 0.2279728779957, 0.204768257586954, 1.09037445301743, 0.287155868433615, 0.211257395413685, 0.132554129593619, 0.0900680495011601, 0.194608837248807, 1.34397119257655, 0.1201882143371, 0.17555070608144, 0.127220190160657, 0.204384526301353, 0.197414938747342, 0.195583977408476, 0.148150828086297, 0.183751866814816, 0.134858902076203, 0.183027629350907, 0.180267135381046, 0.0876356087026242, 0.183285092770786, 0.165502978081942, 0.0487725567447014, 0.172053559692846, 0.142204671797215, 0.166163224221791, 0.249334486033046, 0.150888488422605, 0.259452257883415]
# fmt: on

x_list = np.array(X_COORDINATE)
z_list = np.array(Z_COORDINATE)
C_I_list = np.array(C_I)

#   f will be a function with two arguments (x and z coordinates),
# but those can be array_like structures too, in which case the
# result will be a matrix representing the values in the grid
# specified by those arguments
f = interp2d(x_list, z_list, C_I_list, kind="linear")

x_coords = np.arange(min(x_list), max(x_list) + 1)
z_coords = np.arange(min(z_list), max(z_list) + 1)
c_i = f(x_coords, z_coords)

fig = plt.imshow(
    c_i,
    extent=[min(x_list), max(x_list), min(z_list), max(z_list)],
    origin="lower",
    interpolation="bicubic",
)

# Show the positions of the sample points, just to have some reference
fig.axes.set_autoscale_on(False)
plt.scatter(x_list, z_list, 400, facecolors="none")
plt.colorbar()
plt.show()

Does anybody know how I can change the resolution of the colours on the map so that I can better see the variation between values of 0 and 1 throughout the map?

The map now looks like the following:

Cara menggunakan plotting interpolation python

Linear Interpolation is the technique of determining the values of the functions of any intermediate points when the values of two adjacent points are known. Linear interpolation is basically the estimation of an unknown value that falls within two known values. Linear Interpolation is used in various disciplines like statistical, economics, price determination, etc. It is used to fill the gaps in the statistical data for the sake of continuity of information. 

 

By using the following formula we can Linearly interpolate the given data point 

Cara menggunakan plotting interpolation python

Here (x1, y1) are the coordinates of the first data point. And (x2,y2) are coordinates of the second data point, where x is the point on which we perform interpolation and y is the interpolated value.

Example Problem:

Let’s take an example for better understanding. We have the following data values where x denotes the number and y is the function of the square root of x. Our task is to find the square root of 5.5 (x).

x

1

2

3

4

5

6

y ( f(x) = √x ) 

1

1.4142

1.7320

2

2.2360

2.4494

We can use the Linear Interpolation method here.

1. Find the two adjacent  (x1, y1) ,(x2,y2) from the x. i.e. (5,2.2360) and (6,2.4494).

Where x1 = 5, x2= 6, y1 = 2.2360, y2 = 2.4494, and we interpolate at point x = 5.5.

2. Using the formula y(x)  =  y1  +  (x – x1)  \frac{(y2 – y1) }{ (x2 – x1)}

Cara menggunakan plotting interpolation python

3. After putting the values in the above equation. 

Cara menggunakan plotting interpolation python
y = 2.3427

At x = 5.5 the value of Y will be 2.3427. So by using linear interpolation we can easily determine the value of a function between two intervals.

Approach 1:

Using the formula 

Cara menggunakan plotting interpolation python

Example: Suppose we have a dataset of the population of a city and the year.

X(Year)

2016

2017

2018

2019

2021

Y(Population)

10001

12345

74851

12124

5700

Here, X is the year and Y is the population in any city. Our task to find the population of the city in the year 2020.

We choose our (x1, y1) ,(x2,y2) as x1=2019 , y1=12124, x2=2021,  y2=5700, x = 2020, y = ?

Here (x1, y1) and (x2, y2) are two adjacent points and x is the year for which we want to predict the value of the y population.

Python3




# Python3 code

# Implementing Linear interpolation

# Creating Function to calculate the

# linear interpolation

 

def interpolation(d, x):

    

Population on year 2020 is 8912.0
0
Population on year 2020 is 8912.0
1
Population on year 2020 is 8912.0
2
Population on year 2020 is 8912.0
3
Population on year 2020 is 8912.0
4
Population on year 2020 is 8912.0
5
Population on year 2020 is 8912.0
6
Population on year 2020 is 8912.0
7
Population on year 2020 is 8912.0
8
Population on year 2020 is 8912.0
9
Population on year 2020 is 8912.0
2
Population on year 2020 is 8912.0
3
Population on year 2020 is 8912.0
4
Population on year 2020 is 8912.0
3
Value of y at x = 2.5 is 2.85
4
Value of y at x = 2.5 is 2.85
5
Value of y at x = 2.5 is 2.85
6
Population on year 2020 is 8912.0
5
Population on year 2020 is 8912.0
4
Population on year 2020 is 8912.0
5
Population on year 2020 is 8912.0
6
Population on year 2020 is 8912.0
9
Population on year 2020 is 8912.0
2
Population on year 2020 is 8912.0
3
Population on year 2020 is 8912.0
4
Population on year 2020 is 8912.0
5
Value of y at x = 2.5 is 2.85
4# Python3 code7# Python3 code8
Population on year 2020 is 8912.0
5
Population on year 2020 is 8912.0
4
Population on year 2020 is 8912.0
3
Population on year 2020 is 8912.0
6
Population on year 2020 is 8912.0
9
Population on year 2020 is 8912.0
2
Population on year 2020 is 8912.0
3
Population on year 2020 is 8912.0
4
Population on year 2020 is 8912.0
3# Implementing Linear interpolation8

 

    # Creating Function to calculate the0

Population on year 2020 is 8912.0
0

 

# Creating Function to calculate the2

# Creating Function to calculate the3

Population on year 2020 is 8912.0
1# Creating Function to calculate the5# Creating Function to calculate the6# Creating Function to calculate the7# Creating Function to calculate the8# Creating Function to calculate the9# linear interpolation0# Creating Function to calculate the7# linear interpolation2# linear interpolation3

 

# linear interpolation4

Population on year 2020 is 8912.0
1# linear interpolation6

 

# linear interpolation7

# linear interpolation8# linear interpolation9def0def1def2def3

def4def5

Output

Population on year 2020 is 8912.0

Approach 2:

Using scipy.interpolate.interp1d

Similarly, we can achieve linear interpolation using a scipy library function called interpolate.interp1d.

Syntax : scipy.interpolate.interp1d(x, y, kind=’linear’, axis=- 1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)

Sr. no.            

Parameters                        

Description

1.

x

A 1-D array of real values.

2.

y

A N-D array of real values.

3.

kind

i.e. kind of interpolation do you want it can be ‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’, by defaults it is linear.

4.

axis

Specifies the axis of y along  which we interpolate.

5.

copy

It holds boolean values if True, the class makes internal copies of x and y .

6.

bounds_error

It holds boolean values If True, a ValueError is raised when interpolation is attempted on a value outside the range of x.

Example:

Let’s have a random dataset :

X = [1,2,3,4,5], Y = [11,2.2,3.5,-88,1], and we want to find the value of Y at point 2.5.

Python3




def6

def7

def8 def9interpolation(d, x):0 interpolation(d, x):1

 

interpolation(d, x):2

Population on year 2020 is 8912.0
1 interpolation(d, x):4
Population on year 2020 is 8912.0
5# Creating Function to calculate the7interpolation(d, x):7# Creating Function to calculate the7interpolation(d, x):9# Creating Function to calculate the7    1# Creating Function to calculate the7    3
Population on year 2020 is 8912.0
6    5

    6

Population on year 2020 is 8912.0
1 interpolation(d, x):4    9# Creating Function to calculate the7
Population on year 2020 is 8912.0
01# Creating Function to calculate the7
Population on year 2020 is 8912.0
03# Creating Function to calculate the7
Population on year 2020 is 8912.0
9
Population on year 2020 is 8912.0
06# Creating Function to calculate the7
Population on year 2020 is 8912.0
5
Population on year 2020 is 8912.0
6
Population on year 2020 is 8912.0
10