{"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."},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"import matplotlib.cm as cm\ndef tri_indices(simplices):\n    return ([triplet[c] for triplet in simplices] for c in range(3))\n\ndef plotly_trisurf(x, y, z, simplices, colormap=cm.RdBu, plot_edges=None):\n\n    points3D=np.vstack((x,y,z)).T\n    tri_vertices=map(lambda index: points3D[index], simplices)\n    zmean=[np.mean(tri[:,2]) for tri in tri_vertices ]\n    min_zmean=np.min(zmean)\n    max_zmean=np.max(zmean)\n    facecolor=[map_z2color(zz,  colormap, min_zmean, max_zmean) for zz in zmean]\n    I,J,K=tri_indices(simplices)\n\n    triangles=go.Mesh3d(x=x, y=y, z=z,\n                     facecolor=facecolor,\n                     i=I, j=J, k=K,\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."},{"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/pku-autonomous-driving/car_models_json/benchi-ML500.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    graph_data = plotly_trisurf(x,y,z, triangles, 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=dict(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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"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":1}