Home > Back-end >  Try to get a multiple plot with different X-axes
Try to get a multiple plot with different X-axes

Time:09-18

I need to get something like this:

enter image description here

but I understand that I know I have to give a separate time for each time of the plot but I don't know how. There is also something wrong with my plot I think it plot more than one tiem and they are over each other.

enter image description here

Here is the code:

import mpmath as mp
import numpy as np
import sympy
beta = 0.25;
L=[0.04980905361573844, 0.0208207352451072, 0.012368753716465475, 0.009117292529338674, 0.007461219338976698, 0.006510364609693688, 0.005899506135250773, 0.005485130537183343, 0.0051898472561455, 0.004961157595209418, 0.004778617403698715, 0.00463084459959999, 0.004510113095117956, 0.004410195593051274, 0.004330450690278247]
Lc=[1.7509008762765992, 0.14986486457338544, 0.03453912303580302, 0.014622269851256788, 0.00831141421008418, 0.005660123321843252, 0.004287823173522503, 0.0034922189865254395, 0.0029879534061896186, 0.0026315863522143363, 0.002367747989524076, 0.0021671535838986545, 0.0020116727106455415, 0.0018885896058416002, 0.0017939246597491803]
for k0 in [0.052,0.12,0.252,0.464,0.792,1.264,1.928,2.824,4,5.600,7.795,10.806,14.928,20.599,28.000]:
    t=np.linspace(k0,30,50)
    for i in range(len(L)):
        j0 = L[i];
        j1 = Lc[i];
        G = []
        def f(s):
            return s**(beta - 1)/(j0*s**beta   j1*sympy.gamma(beta   1))

        for j in range(len(t)):
            G.append(mp.invertlaplace(f, t[j], method = 'dehoog', dps = 10, degree = 50))

        plt.plot(t,G)

The answer should be like that but I get this erorr

import numpy as np
import sympy
beta = 0.25;
L=[0.04980905361573844, 0.0208207352451072, 0.012368753716465475, 0.009117292529338674, 0.007461219338976698, 0.006510364609693688, 0.005899506135250773, 0.005485130537183343, 0.0051898472561455, 0.004961157595209418, 0.004778617403698715, 0.00463084459959999, 0.004510113095117956, 0.004410195593051274, 0.004330450690278247]
Lc=[1.7509008762765992, 0.14986486457338544, 0.03453912303580302, 0.014622269851256788, 0.00831141421008418, 0.005660123321843252, 0.004287823173522503, 0.0034922189865254395, 0.0029879534061896186, 0.0026315863522143363, 0.002367747989524076, 0.0021671535838986545, 0.0020116727106455415, 0.0018885896058416002, 0.0017939246597491803]
for k0 in [0.052,0.12,0.252,0.464,0.792,1.264,1.928,2.824,4,5.600,7.795,10.806,14.928,20.599,28.000]:
    t[k0]=np.linspace(k0,30,50)
    for i in range(len(L)):
        j0 = L[i];
        j1 = Lc[i];
        G = []
        def f(s):
            return s**(beta - 1)/(j0*s**beta   j1*sympy.gamma(beta   1))

        for j in range(len(t[k0])):
            G.append(mp.invertlaplace(f, t[k0][j], method = 'dehoog', dps = 10, degree = 50))

        plt.plot(t[k0],G)

enter image description here]

For each step should be done something like this. enter image description here

CodePudding user response:

You are looping one time too many, this is the reason why you are plotting more than one line overlapped.
You should re-structured your code in this way:

import mpmath as mp
import numpy as np
import sympy
import matplotlib.pyplot as plt


beta = 0.25
L = [0.04980905361573844, 0.0208207352451072, 0.012368753716465475, 0.009117292529338674, 0.007461219338976698, 0.006510364609693688, 0.005899506135250773, 0.005485130537183343, 0.0051898472561455, 0.004961157595209418, 0.004778617403698715, 0.00463084459959999, 0.004510113095117956, 0.004410195593051274, 0.004330450690278247]
Lc = [1.7509008762765992, 0.14986486457338544, 0.03453912303580302, 0.014622269851256788, 0.00831141421008418, 0.005660123321843252, 0.004287823173522503, 0.0034922189865254395, 0.0029879534061896186, 0.0026315863522143363, 0.002367747989524076, 0.0021671535838986545, 0.0020116727106455415, 0.0018885896058416002, 0.0017939246597491803]
K = [0.052, 0.12, 0.252, 0.464, 0.792, 1.264, 1.928, 2.824, 4, 5.600, 7.795, 10.806, 14.928, 20.599, 28.000]


def f(s):
    return s**(beta - 1)/(j0*s**beta   j1*sympy.gamma(beta   1))


for k0, j0, j1 in zip(K, L, Lc):
    t = np.linspace(k0, 30, 50)
    G = []
    for j in range(len(t)):
        G.append(mp.invertlaplace(f, t[j], method = 'dehoog', dps = 10, degree = 50))

    plt.plot(t, G)

plt.show()

Thanks to zip you can iterate over K, L and Lc lists, picking one element from each lists at the same time; no need of i and j counters.

enter image description here

  • Related