pythonarraysnumpyplotlinspace

How do I create a better resolution on my plot with linspace?


i need to create a higher resolution of my plot with the linspace function but i can't figure out how to implement it into my code. Maybe someone has a better understanding of this and can help me.

import numpy as np
import matplotlib.pyplot as plt
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16]) 
ct = c/1000                                            
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = np.zeros(n)                                         
k = np.zeros(n)                                        
for j in range(0,n):
    A[j] = (ct[j] - ct0)/(cmax - ct0)
for j in range(0, n):
    if j < 3:
        k[j] = -(np.log(1 - A[j])) / N[j]
    else:
        k[j] = 1
MWk = np.mean(k)
Amod = np.zeros(n)
for j in range(0,n):
    Amod[j] = 1 - np.exp((-N[j]) * MWk)    
    
     
print(ct)
print(A)
print(k)
plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.plot(N, A, "g", label = "Aufschlussgrad")
plt.plot(N, Amod, "k", label = "Modelfunktion")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()

Solution

  • There is no need to interpolate Amod, since it is a function you defined. On the other hand, it is necessary to perform an interpolation (either on A or on the original data c) in order to add more points to the graph. With only 4 or 5 points, the interpolation will not be very meaningful. In this case I choose to interpolate A.

    The code was not taking profit of numpy's arrays, so I pythonized it a little (it looked like C)

    import numpy as np
    import matplotlib.pyplot as plt
    
    def Amod(x, MWk):
        return 1 - np.exp((-x) * MWk) 
    
    def k(x, A):
        rv = -np.log(1 - A) / x
        rv[np.nonzero(A==1)] = 1
        return rv
    
    # No real changes here: only a little 'pythonization'
    N = np.array([1, 2, 3, 4])
    c = np.array([1359,2136.6,2617.74,2630.16]) 
    ct = c/1000                                            
    ct0 = 103.8348/1000
    cmax = 2630.16/1000
    n = N.size
    A = (ct - ct0) / (cmax-ct0)
    MWk = np.mean(k(N, A))
    
    print(ct)
    print(A)
    print(k)
    
    # we now interpolate A
    # numpy's interpolation is linear... it is not useful for this case
    from scipy.interpolate import interp1d
    # interp1d returns a function that interpolates the data we provide
    # in this case, i choose quadratic interpolation
    A_interp_fun = interp1d(N, A, 'quadratic')
    
    # Let's increase the number of points
    new_x = np.linspace(N[0], N[-1])
    
    A_xtra = A_interp_fun(new_x)
    
    plt.xlabel("N")
    plt.ylabel("Aufschlussgrad ")
    plt.scatter(N, A, label = "Aufschlussgrad - data")
    plt.plot(new_x, A_xtra, "g", label="Aufschlussgrad - interpolation")
    plt.scatter(N, Amod(N, MWk), label="Modelfunktion - data")
    plt.plot(new_x, Amod(new_x, MWk), "k", label="Modelfunktion - function")
    plt.title("Hochdruckhomogenisator")
    plt.legend()
    plt.show()