Hello!I'm having a problem with plotting 3d axes grid. Can anyone please help me with the code, I'm doing this for my master thesis.
So, I need a 3d plot of divergence of two different distributions. The first one is fixed, but the second changes on the interval mean = mu = np.arange(-5, 5, 0.5)
and sigma = deviance = np.arange(1, 4, 0.5).
The problem is, that this is not a normal function, but is an estimation of histograms. and the whole loop is in function. I don't know how make a meshgrid of a and b matrix(as they represent x, y axes2D).
So, I need a 3d plot of divergence of two different distributions. The first one is fixed, but the second changes on the interval mean = mu = np.arange(-5, 5, 0.5)
and sigma = deviance = np.arange(1, 4, 0.5).
The problem is, that this is not a normal function, but is an estimation of histograms. and the whole loop is in function. I don't know how make a meshgrid of a and b matrix(as they represent x, y axes2D).
import numpy as np import matplotlib.pyplot as plt from scipy import stats from scipy.stats import norm from random import seed from numpy import random from scipy import integrate from math import log, e sample = 10 np.random.seed(seed=1) vzorec_seed1 = stats.norm.rvs(0, 1, size=int(sample)) mu = np.arange(-5, 5, 0.5) sigma = np.arange(1, 4, 0.5) def loop(): mu = np.arange(-5, 5, 0.5) sigma = np.arange(1, 4, 0.5) for a in mu: for b in sigma: vzorec_seed2 = stats.norm.rvs(a, b, size=int(sample)) [y1, x1] = np.histogram(vzorec_seed1, bins="scott", range=(-8, 8), density=True) [y2, x2] = np.histogram(vzorec_seed2, bins="scott", range=(-8, 8), density=True) plt.hist(vzorec_seed1, bins="scott", range=(-8, 8), density=True) plt.hist(vzorec_seed2, bins="scott", range=(-8, 8), density=True) def uredi_stolpce(x1, y1, x2, y2, zeros=1): try: x1 = x1.tolist() except: pass try: x2 = x2.tolist() except: pass try: y1 = y1.tolist() except: pass try: y2 = y2.tolist() except: pass if x1[0] < x2[0]: x2.insert(0, x1[0]) y2.insert(0, 0) elif x2[0] < x1[0]: x1.insert(0, x2[0]) y1.insert(0, 0) if max(x1) < max(x2): x1.append(max(x2)) y1.append(0) elif max(x2) < max(x1): x2.append(max(x1)) y2.append(0) x1_new = [] x2_new = [] y1_new = [] y2_new = [] for i in range(len(x1) - 1): x1_new.append(x1[i]) y1_new.append(y1[i]) for el2 in x2: if x1[i] < el2 < x1[i + 1]: x1_new.append(el2) y1_new.append(y1[i]) for j in range(len(x2) - 1): x2_new.append(x2[j]) y2_new.append(y2[j]) for el1 in x1: if x2[j] < el1 < x2[j + 1]: x2_new.append(el1) y2_new.append(y2[j]) x1_new.append(max(x1)) x2_new.append(max(x2)) x1, y1, x2, y2 = x1_new, y1_new, x2_new, y2_new if not zeros: for i in range(len(y2)): if y2[i] == 0: y2[i] = np.finfo(float).eps x = x1 return x, y1, y2 def divergence_hist(x1, y1, x2, y2): bins, y1_array, y2_array = uredi_stolpce(x1, y1, x2, y2, zeros=0) y = [] for i in range(len(y1_array)): if y1_array[i] >= np.finfo(float).eps and y2_array[i] >= np.finfo(float).eps: y.append(y1_array[i] * log(y1_array[i] / y2_array[i], e)) elif y1_array[i] < np.finfo(float).eps: y.append(0.) else: y.append(0.) area = 0 for i in range(len(y)): area += y[i] * (bins[i + 1] - bins[i]) return area divergence_hist(x1, y1, x2, y2) print("Divergenca histogramov:") print(divergence_hist(x1, y1, x2, y2)) Z = loop() Z = np.array(Z) X, Y = np.meshgrid(mu, sigma) fig = plt.figure(figsize=[8, 5]) ax = fig.add_subplot(111, projection='3d') #da prikaže 3d ax.plot_surface(X, Y, Z, cmap="plasma") #plasma; da je različne barve ax.set_title("Graf ocenjene divergence") ax.set_xlabel("Mean") ax.set_ylabel("Sigma") ax.set_zlabel("Divergenca") plt.show()And here is the result. The divergence does calculate. Loop works. But the plot doesn't.
C:\Users\Ester\PycharmProjects\programček1\venv\Scripts\python.exe "C:/Users/Ester/PycharmProjects/programček1/running code multiple time.py" Divergenca histogramov: 34.51801870498036 Divergenca histogramov: 34.51801870498036 Divergenca histogramov: 28.87555189533374 Divergenca histogramov: 5.190825985155286 Divergenca histogramov: 28.619839630743275 Divergenca histogramov: 1.0181634834793956 Divergenca histogramov: 32.698034622434086 Divergenca histogramov: 34.51801870498036 Divergenca histogramov: 27.884877874736453 Divergenca histogramov: 17.7913255931447 Divergenca histogramov: 5.34244481686284 Divergenca histogramov: 0.9003804478230121 Divergenca histogramov: 33.047989387092144 Divergenca histogramov: 32.33237244812081 Divergenca histogramov: 1.652419146211149 Divergenca histogramov: 5.036809656524479 Divergenca histogramov: 1.4700975894171944 Divergenca histogramov: 1.3058455559311763 Divergenca histogramov: 29.29206661726168 Divergenca histogramov: 5.190825985155286 Divergenca histogramov: 17.444752002864725 Divergenca histogramov: 1.0646324813090302 Divergenca histogramov: 1.2531652981022634 Divergenca histogramov: 1.3907950743288753 Divergenca histogramov: 17.5650886352687 Divergenca histogramov: 17.66747523523431 Divergenca histogramov: 4.355880645767522 Divergenca histogramov: 17.859676187006677 Divergenca histogramov: 6.970192149556786 Divergenca histogramov: 2.3455663267710944 Divergenca histogramov: 7.627103490159111 Divergenca histogramov: 7.349844617935133 Divergenca histogramov: 1.4700975894171944 Divergenca histogramov: 6.938937025534965 Divergenca histogramov: 1.06463248130903 Divergenca histogramov: 0.49491533971484764 Divergenca histogramov: 7.211215181823144 Divergenca histogramov: 9.337392929951259 Divergenca histogramov: 1.0119522234801168 Divergenca histogramov: 1.0119522234801168 Divergenca histogramov: 1.1291710024466013 Divergenca histogramov: 1.190289695449483 Divergenca histogramov: 10.169433703099486 Divergenca histogramov: 5.950814701611099 Divergenca histogramov: 6.0279435882276085 Divergenca histogramov: 0.5153263369749753 Divergenca histogramov: 1.2673650353631123 Divergenca histogramov: 1.0646324813090302 Divergenca histogramov: 8.745888539772306 Divergenca histogramov: 8.908470736030251 Divergenca histogramov: 0.2599135250919798 Divergenca histogramov: 0.31259378292089307 Divergenca histogramov: 0.6653786332001442 Divergenca histogramov: 1.2469540381029849 Divergenca histogramov: 3.6922985534910895 Divergenca histogramov: 4.287381942071785 Divergenca histogramov: 2.6755054568239864 Divergenca histogramov: 0.7180588910290574 Divergenca histogramov: 0.6369845192102332 Divergenca histogramov: 1.1235239991372217 Divergenca histogramov: 4.101103534177748 Divergenca histogramov: 0.23883958771735603 Divergenca histogramov: 0.08109302162163282 Divergenca histogramov: 0.6653786332001442 Divergenca histogramov: 0.9207914450831396 Divergenca histogramov: 0.8154309294253135 Divergenca histogramov: 3.1517573442349054 Divergenca histogramov: 0.40375456131787035 Divergenca histogramov: 0.1885365832141568 Divergenca histogramov: 0.6653786332001442 Divergenca histogramov: 0.8092196694260347 Divergenca histogramov: 0.9207914450831395 Divergenca histogramov: 0.3576648186704895 Divergenca histogramov: 4.127245060042239 Divergenca histogramov: 5.967354836594849 Divergenca histogramov: 0.9207914450831396 Divergenca histogramov: 0.9207914450831395 Divergenca histogramov: 0.9207914450831396 Divergenca histogramov: 8.922818768764957 Divergenca histogramov: 6.483440306881283 Divergenca histogramov: 0.7848245873413187 Divergenca histogramov: 0.8409734972346579 Divergenca histogramov: 7.6635678015179005 Divergenca histogramov: 1.2469540381029849 Divergenca histogramov: 24.39768756748555 Divergenca histogramov: 5.598623214798632 Divergenca histogramov: 1.2084735175349204 Divergenca histogramov: 0.8618999272549478 Divergenca histogramov: 0.8414889299948202 Divergenca histogramov: 4.4331459540790155 Divergenca histogramov: 24.44934440944882 Divergenca histogramov: 27.526525980890842 Divergenca histogramov: 1.06463248130903 Divergenca histogramov: 1.2673650353631123 Divergenca histogramov: 1.4112060715890027 Divergenca histogramov: 0.8288456681158816 Divergenca histogramov: 7.430937639556764 Divergenca histogramov: 24.567375979285654 Divergenca histogramov: 1.7577796618689754 Divergenca histogramov: 1.1235239991372217 Divergenca histogramov: 0.8414889299948202 Divergenca histogramov: 4.571316874150846 Divergenca histogramov: 32.62241856637301 Divergenca histogramov: 17.44475200286472 Divergenca histogramov: 5.129526910303374 Divergenca histogramov: 3.5197221611543523 Divergenca histogramov: 0.9956396098220786 Divergenca histogramov: 26.44052276661967 Divergenca histogramov: 34.51801870498037 Divergenca histogramov: 17.588593039090615 Divergenca histogramov: 5.656318767528932 Divergenca histogramov: 26.484307371956813 Divergenca histogramov: 5.541722926934771 Divergenca histogramov: 4.629512279235521 Divergenca histogramov: 34.51801870498036 Divergenca histogramov: 17.859676187006674 Divergenca histogramov: 17.39207174503581 Divergenca histogramov: 28.7567266643806 Divergenca histogramov: 4.93316058514687 Divergenca histogramov: 17.61298812117533 Traceback (most recent call last): File "C:\Users\Ester\PycharmProjects\programček1\running code multiple time.py", line 119, in <module> ax.plot_surface(X, Y, Z, cmap="plasma") #plasma; da je različne barve File "C:\Users\Ester\PycharmProjects\programček1\venv\lib\site-packages\matplotlib\_api\deprecation.py", line 431, in wrapper return func(*inner_args, **inner_kwargs) File "C:\Users\Ester\PycharmProjects\programček1\venv\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1658, in plot_surface raise ValueError("Argument Z must be 2-dimensional.") ValueError: Argument Z must be 2-dimensional. Process finished with exit code 1Thanks!
Larz60+ write Nov-26-2021, 06:12 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.