{"id":6620,"date":"2021-01-30T15:42:57","date_gmt":"2021-01-30T15:42:57","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=6620"},"modified":"2021-02-02T16:34:34","modified_gmt":"2021-02-02T16:34:34","slug":"plotly-for-interactive-3d-plotting","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2021\/01\/plotly-for-interactive-3d-plotting\/","title":{"rendered":"Plotly for interactive 3D plotting"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>An recently wrote a post on how to use <a href=\"https:\/\/www.blopig.com\/blog\/2021\/01\/seaborn-101\/\">the seaborn library<\/a>. I really like seaborn and use it a lot for 2D plots. However, recently I have been dealing with 3D data and have found <a href=\"https:\/\/plotly.com\/python\/3d-charts\/\">plotly<\/a> to be best. When used in a jupyter notebook, it allows you to easily generate 3D interactive plots. This is extremely useful to visualize structural data.<\/p>\n<\/div>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><p>Plotly is best used in combination with the <a href=\"https:\/\/pandas.pydata.org\/docs\/user_guide\/10min.html#min\">pandas library<\/a>, most of the tutorials in its documentations use this library. In the following example they plot data from the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Iris_flower_data_set\">iris flower data set<\/a>.<\/p>\n<\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import plotly.express as px\ndf = px.data.iris()\nfig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',\n              color='species')\nfig.show()<\/pre>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><p>And this will return the following plot (I tried to make it interactive but I am not sure if that worked. If it didn&#8217;t, an interactive version can be found by clicking on the image)<\/p>\n<\/div>\n\n\n\n<div>\n    <a href=\"https:\/\/plotly.com\/~bryik\/13\/\" target=\"_blank\" title=\"Iris\" style=\"text-align: center\" rel=\"noopener\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/plotly.com\/~bryik\/13.png?w=625&#038;ssl=1\" alt=\"Iris\" style=\"max-width: 100%;width: 1735px\" ><\/a>\n    \n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Personally, I don&#8217;t always use pandas data frames for everything, so here is how to plot the position of residues in the H3 loop stored as a numpy array. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import plotly.graph_objects as go\ntrueH3 = go.Scatter3d(x=truth[0], y=truth[1], z=truth[2], name = \"Truth\", mode = \"lines\")\nprediction = go.Scatter3d(x=pred[0], y=pred[1], z=pred[2], name = \"Prediction\", mode = \"lines\")\nfig  = go.Figure(data=[trueH3, prediction])\nfig.update_layout(title = \"CDRH3 Truth Vs Prediction\")\nfig.show()<\/pre>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><p>And that would return the following plot (interactive version <a href=\"https:\/\/brennanaba.github.io\/plotly\/\">here<\/a>):<\/p>\n<\/div>\n\n\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2021\/01\/newplot.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"625\" height=\"334\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2021\/01\/newplot.png?resize=625%2C334&#038;ssl=1\" alt=\"\" class=\"wp-image-6624\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2021\/01\/newplot.png?w=983&amp;ssl=1 983w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2021\/01\/newplot.png?resize=300%2C160&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2021\/01\/newplot.png?resize=768%2C410&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2021\/01\/newplot.png?resize=624%2C333&amp;ssl=1 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Plotly can be used for a large number of plotting tasks, so I hope I have convinced you that it is at least worth looking at. If you have read this far I hope this was not a complete waste of your time.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":71,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,361,227],"tags":[256],"ppma_author":[552],"class_list":["post-6620","post","type-post","status-publish","format-standard","hentry","category-code","category-data-science","category-python-code","tag-plotting"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":552,"user_id":71,"is_guest":0,"slug":"brennan","display_name":"Brennan Abanades Kenyon","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/5c85dcbb5b1499e82ecfc264ec387c8302ac238c786e68cc5c92e9c21904d260?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/71"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=6620"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6620\/revisions"}],"predecessor-version":[{"id":6638,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6620\/revisions\/6638"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=6620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=6620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=6620"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=6620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}