Python Forum
3D graphic - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: 3D graphic (/thread-10861.html)



3D graphic - lollie - Jun-10-2018

Hello, I have to realise a 3D graphic. Z is defined with my functions f and somme which works properly when the graphic code isn't in the script and returns a float.
Output:
>>> (f(12,56,3)) -223.60707402538546 >>> type(f(12,56,3)) <class 'float'> >>> type(somme(20,5)) <class 'float'>
However, at the moment I put this piece of code, I have an error which is :
Error:
Traceback (most recent call last): File "<tmp 5>", line 60, in <module> Z[i][j]+= (X6[k]-f(i,j,k))**2 File "mtrand.pyx", line 2086, in mtrand.RandomState.f (numpy\random\mtrand\mtrand.c:23441) ValueError: dfnum <= 0
and my functions seem to change :

Output:
>>> type(somme(20,5)) Traceback (most recent call last): File "<console>", line 1, in <module> File "<tmp 5>", line 37, in somme S+= (X6[k]-f(xr,teta,k))**2 ValueError: operands could not be broadcast together with shapes (0,) (2,) (0,) >>> type(f(12,56,3)) <class 'numpy.ndarray'> >>> (f(12,56,3)) array([ 1.1772358 , 0.60745691, 0.97005387])
Please can you help me ? Thanks a lot !!

My script is the following :


X6 = [6.044642857142857, 86.72222222222223, 124.57142857142857, 175.375, 278.4392523364486, 321.6201923076923, 343.53496614473124, 396.7894736842105, 421.44897959183675, 475.187134502924, 510.888198757764, 524.5, 614.3641975308642, 621.640625, 718, 757.7796610169491, 782, 809.375, 961.3730158730159, 1025.5, 1286.5925925925926, 1398.2167630057804, 1560.962962962963, 1564.1199484488632, 1739.9130434782608, 1795.8029850746268, 1826.078125, 1996.842105263158, 2027.075984990619, 2045.1772151898733, 2123.913043478261, 2290.9411764705883, 2310.3076923076924, 2492.190476190476, 2649.8333333333335, 2796.1521739130435, 2876.479905437352, 2942.5, 2961.4117647058824, 3120, 3174.4451219512193, 3292.2688172043013, 3524.808510638298, 3537.5882352941176, 3556.235294117647, 3650.9, 3659.2105263157896, 3718.8311125916052, 3762.285714285714, 3916.5704225352115, 4073, 4084, 4105.526315789473, 4178.452380952381, 4232.531914893617, 4258.585714285715, 4349.878048780488, 4351.98087431694, 4424.675516224189, 4475.285714285715]

X5 = [58.357142857142854, 140, 174, 228.85714285714286, 328.2909090909091, 375.38, 392.53819444444446, 440.84615384615387, 462.8181818181818, 527.7, 555.5, 574.1428571428571, 662.7580174927114, 672.1451612903226, 770, 802.1363636363636, 829.3520833333333, 852.3333333333334, 1009.5272727272727, 1075.857142857143, 1341.7272727272727, 1450.4545454545455, 1613.275, 1615.5365110726927, 1791, 1843.012658227848, 1875.483870967742, 2041.090909090909, 2080.9009584664536, 2089.9285714285716, 2167.7, 2337.848484848485, 2357, 2538.0454545454545, 2699.5, 2840.7368421052633, 2919.196296296296, 2990, 3013, 3165.3333333333335, 3224.0263157894738, 3337.0208333333335, 3575.2571428571428, 3590.5, 3608.3333333333335, 3697.137931034483, 3711.375, 3770.460317460317, 3815, 3966.2266666666665, 4125, 4131, 4155.166666666667, 4229.772727272727, 4281, 4306.375, 4395.8125, 4402.906976744186, 4469.17094017094, 4528.333333333333]

#

Y6 = [1600.0267857142858, 1817.8333333333333, 1075.7142857142858, 1873.5625, 1233.3084112149534, 1741.1778846153845, 419.1517139229793, 12.078947368421053, 24.510204081632654, 1549.46783625731, 457.6832298136646, 1197, 136.62962962962962, 1277.4140625, 1629.5, 742.1694915254237, 1417, 99.25, 955.436507936508, 1131.5, 2038.7407407407406, 1476.393063583815, 1547.9037037037037, 1560.8350915953235, 1396.2173913043478, 726.5194029850746, 1096.140625, 268.8421052631579, 1365.092987804878, 399.46835443037975, 282.2173913043478, 724.0294117647059, 771.9230769230769, 514.9285714285714, 1219, 345.0869565217391, 65.81323877068557, 833.5, 1519.1176470588234, 464, 1214.939024390244, 343.86021505376345, 1392.099290780142, 1736.8823529411766, 1537.1764705882354, 588.38, 1538, 818.2527204086165, 1705.5, 1207.161971830986, 1561, 959.5, 1147.6315789473683, 1461.595238095238, 998.5744680851063, 887.2428571428572, 558.8780487804878, 1398.8196721311476, 477.97050147492627, 1674]

Y5 = [1596.482142857143, 1814.125, 1071.5, 1868.2857142857142, 1228.3272727272727, 1736.51, 414.2013888888889, 7.384615384615385, 15.681818181818182, 1548.2, 451.41087916499396, 1190.857142857143, 129.71428571428572, 1270.4193548387098, 1622, 734.1545454545454, 1408.9333333333334, 91.5, 946.2363636363636, 1122.2857142857142, 2027.4545454545455, 1464.1322314049587, 1534.8, 1546.5678768420187, 1382.111111111111, 711.9071729957806, 1081.774193548387, 253.0909090909091, 1349.888178913738, 383.7142857142857, 266.1, 706.3333333333334, 754, 495.8636363636364, 1199.25, 324.05263157894734, 44.94814814814815, 812.3333333333334, 1497.4285714285713, 441.375, 1191.6052631578948, 319.7083333333333, 1366.6714285714286, 1711.5, 1512, 562.5862068965517, 1511.5, 791.9206349206349, 1679, 1179.4133333333334, 1532, 931, 1119, 1431.909090909091, 968.9615384615385, 857.75, 528.5625, 1370.9302325581396, 447.36752136752136, 1642.3333333333333]


##

from math import cos
from math import sin

def f(xr,teta,i):
    x6=X6[i]
    x5=X5[i]
    y6=Y6[i]
    y5=Y5[i]
    a = xr + (x5-xr)*cos(teta) - (sin(teta)/(1-cos(teta)))*(y5-y6+(x5-xr)*sin(teta))
    return a

    
from math import pi

def somme(xr,teta):
    S = 0
    n=len(X6)
    if teta//(pi/2)==0:
        S=0
    else :
        for k in range(n):
            S+= (X6[k]-f(xr,teta,k))**2
    return S
    
## Graphic code
import numpy as np
import matplotlib as plt
from pylab import *
from mpl_toolkits.mplot3d import Axes3D

fig = figure()
ax = Axes3D(fig)
X = np.linspace(-150, 150, 301)
Y = np.linspace(-150, 150, 301)
X, Y = np.meshgrid(X, Y)

Z = zeros((301,301))
for i in range(301):
    for j in range(301):
        n=len(X6)
        if j//(pi/2)==0:
            Z[i][j]=0
        else :
            for k in range(n):
                Z[i][j]+= (X6[k]-f(i,j,k))**2
    
ax.plot_surface(X,Y,Z) 



RE: 3D graphic - killerrex - Jun-10-2018

You are mixing pure python operations (float and lists) with vector-oriented ones using numpy. Although there is nothing wrong with this normally it is better to stick to one of them... in this cae I think the proper one is numpy, but is up to you.

Numerically your code has many small details that I think you shall take into account:
- Your f function has singularities around theta=-2*pi, 0, 2*pi... it will be good idea to either filter them or prpare your code to deal with inf and -inf.
- In your somme function (and later) you use if teta//(pi/2)==0 I imagine to exclude this singularities... but notice that that is true for teta in the range [0.0 to 1.57..] that I think is not really what you whant to exclude
- For the plot you are creating a mesh in the range of [-150, 150] and your i, j variable vary from 0 to 300. Then you call f(i, j, k) so at least for j you are using a range of values between 0 and 300 radians (~47 complete revolutions) so I think there there is a mixture of indexes, degrees and radians.


RE: 3D graphic - lollie - Jun-11-2018

Thank you for your response ! Indeed you have corrected some important points in my code ! Thanks you for that
Eventually the main problem was the name of the function f (f must already be used by pylbab)
I renamed it and everything worked properly