{"cells":[{"metadata":{},"cell_type":"markdown","source":"## 3D Interactive Car with Plotly\n\n\n\nImpressed by these kernels, I used these kernels and the plotly official documentation to create the following visualizations:\n\n- Eric Bouteillon(@ebouteillon) : [Load a 3D car model](https://www.kaggle.com/ebouteillon/load-a-3d-car-model)\n- Phung Hieu(@phunghieu) [A quick & simple EDA](https://www.kaggle.com/phunghieu/a-quick-simple-eda)\n\n- [Plotly : Surface Triangulation in Python/v3](https://plot.ly/python/v3/surface-triangulation/)\n\nI hope you all get good results.","execution_count":null},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"import matplotlib.cm as cm\ndef tri_indices(simplices):\n    #print('len(simplices)',len(simplices))\n    #print('after tri',len(([triplet[c] for triplet in simplices] for c in range(3))))\n    return ([triplet[c] for triplet in simplices] for c in range(3))\n\ndef plotly_trisurf(x, y, z, simplices,left_front_max_x,left_front_min_x,left_front_max_y,left_front_min_y,left_front_max_z,left_front_min_z, colormap=cm.RdBu, plot_edges=None):\n    points3D=np.vstack((x,y,z)).T\n    tri_vertices=[ points3D[index] for index in  simplices]\n\n    zmean=[np.mean(tri[:,2]) for tri in tri_vertices ]\n    ymean=[np.mean(tri[:,1]) for tri in tri_vertices ]\n    xmean=[np.mean(tri[:,0]) for tri in tri_vertices ]\n    \n    zmean_new=[np.mean(tri[:,2]) for tri in tri_vertices ] \n    min_zmean=np.min(zmean)#按z轴将点排序\n    max_zmean=np.max(zmean)\n    \n    min_ymean=np.min(ymean)#按z轴将点排序\n    max_ymean=np.max(ymean)\n\n    min_xmean=np.min(xmean)#按z轴将点排序\n    max_xmean=np.max(xmean)\n    \n    facecolor=[map_z2color(zz,  colormap, min_zmean, max_zmean) for zz in zmean]\n    \n    I,J,K=tri_indices(simplices)\n    I_pt= points3D[I]\n    J_pt= points3D[J]\n    K_pt= points3D[K]\n    \n    I_new =[]\n    J_new =[]\n    K_new =[]\n    \n    left_front_I = []\n    left_front_J = []\n    left_front_K = []\n\n    for i, tri_i in enumerate(I_pt):\n        if xmean[i]>left_front_min_x and xmean[i]<left_front_max_x:\n            if ymean[i]>left_front_min_y and ymean[i]<left_front_max_y:\n                if zmean[i]>left_front_min_z and zmean[i]<left_front_max_z:\n                    I_new.append(I[i])\n                    J_new.append(J[i])\n                    K_new.append(K[i])\n                    print('i',i,'I[i]',I[i],'J[i]',J[i],'K[i]',K[i])\n\n    triangles=go.Mesh3d(x=x, y=y, z=z,\n                     facecolor=facecolor,\n                     i=I_new, j=J_new, k=K_new,\n                     name='')\n\n    if plot_edges is None: return [triangles]\n    else:\n        lists_coord=[[[T[k%3][c] for k in range(4)]+[ None]   for T in tri_vertices]  for c in range(3)]\n        Xe, Ye, Ze=[reduce(lambda x,y: x+y, lists_coord[k]) for k in range(3)]\n\n        lines=go.Scatter3d(x=Xe, y=Ye, z=Ze,\n                        mode='lines',\n                        line=dict(color= 'rgb(50,50,50)', width=1.5))\n        return [triangles, lines]\n    \ndef map_z2color(zval, colormap, vmin, vmax):\n    if vmin>vmax: raise ValueError('incorrect relation between vmin and vmax')\n    t=(zval-vmin)/float((vmax-vmin))#normalize val\n    R, G, B, alpha=colormap(t)\n    return 'rgb('+'{:d}'.format(int(R*255+0.5))+','+'{:d}'.format(int(G*255+0.5))+\\\n           ','+'{:d}'.format(int(B*255+0.5))+')'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> Thanks to **Ollie Perrée** for giving me advice on the ratio.","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import json\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport plotly.figure_factory as FF\nimport plotly.graph_objs as go\n\nimport numpy as np\n\nwith open('../input/car-models-wd/car_models_json_wd/aodi-Q7-SUV.json') as json_file:\n    data = json.load(json_file)\n    vertices, triangles = np.array(data['vertices']), np.array(data['faces']) - 1\n    \n    x, y, z = vertices[:,0], vertices[:,2], -vertices[:,1]\n    car_type = data['car_type']\n    \n    left_front_max_x = -0.86\n    left_front_min_x = -1   \n    \n    left_front_max_y = 0.99\n    left_front_min_y = -0.19   \n    \n    left_front_max_z = 0.16\n    left_front_min_z = -0.54  \n    \n    graph_data = plotly_trisurf(x,y,z, triangles,left_front_max_x,left_front_min_x,left_front_max_y,left_front_min_y,left_front_max_z,left_front_min_z, colormap=cm.RdBu, plot_edges=None)\n\n    # with no axis\n    noaxis=dict(showbackground=False,\n            showline=False,\n            zeroline=False,\n            showgrid=False,\n            showticklabels=False,\n            title='')\n    \n    # with axis\n    axis = dict(\n        showbackground=True,\n        backgroundcolor=\"rgb(230, 230,230)\",\n        gridcolor=\"rgb(255, 255, 255)\",\n        zerolinecolor=\"rgb(255, 255, 255)\",\n    )\n    \n    layout = go.Layout(\n         title=car_type + ' with noaxis',\n         width=800, height=600,\n         scene=dict(\n             xaxis= (noaxis), yaxis=dict(noaxis), zaxis=dict(noaxis),\n#              aspectratio=dict( x=1, y=2, z=0.5)\n         )\n    )\n\n    fig = go.Figure(data= graph_data, layout=layout)\n    \n    \n    fig.show()\n    \n    layout = go.Layout(\n         title=car_type + ' with axis', \n         width=800, height=600,\n         scene=dict(\n             xaxis=dict(axis), yaxis=dict(axis), zaxis=dict(axis),\n#              aspectratio=dict( x=1, y=2, z=0.5)\n         )\n    )\n\n    fig = go.Figure(data= graph_data, layout=layout)\n    fig.update_layout(scene_aspectmode=\"data\")\n    \n    fig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}