{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import plotly.figure_factory as ff\nimport numpy as np\n\nbg_text = [['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '@', '', 'd','', 'e', '', 's', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n           ['', '', '', '', '','', '', '', '', '','', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '','', '', ''],\n          ]\n          \n    \ntext_1 = text_2 = bg_text\n\nz = [[.0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0,.0, .5, .0, .5, .0, .5, .0, .5, .0, .0,.0, .0, .0, .0, .0, .5, .0, .0, .0, .0, .0, .5, .0, .0, .5, .0, .0, .0, .0, .0, .0, .0,],\n     [.0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0,.0, .0, .0, .0, .0, .5, .0, .0, .0, .0, .0, .5, .5, .0, .5, .0, .0, .0, .0, .0, .0, .0,],\n     [.0,.0, .5, .0, .3, .0, .5, .0, .3, .0, .0,.0, .5, .5, .5, .0, .5, .0, .5, .5, .5, .0, .5, .0, .0, .5, .0, .5, .0, .5, .0, .0, .0,],\n     [.0,.0, .0, .0, .3, .0, .0, .0, .3, .0, .0,.0, .5, .0, .5, .0, .5, .0, .5, .0, .5, .0, .5, .0, .0, .5, .0, .5, .0, .5, .0, .0, .0,],\n     [.0,.0, .3, .0, .3, .0, .3, .0, .3, .0, .0,.0, .5, .5, .5, .0, .5, .0, .5, .5, .5, .0, .5, .5, .0, .5, .0, .5, .5, .5, .0, .0, .0,],\n     [.0,.0, .3, .0, .3, .0, .3, .0, .3, .0, .0,.0, .5, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .5, .0, .0, .0,],\n     [.0,.0, .3, .0, .3, .0, .3, .0, .3, .0, .0,.0, .5, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .5, .5, .5, .0, .0, .0,],\n     [.0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0,.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],     \n     ]\n     \n    \n# Display something on hover\nhover=[]\nfor x in range(len(bg_text)):\n    hover.append([i + '<br>' + 'Plotly guide: Customize for better visualizations' + str(j)\n                      for i, j in zip(text_1[x], text_2[x])])\n\n# Invert Matrices\nbg_text = bg_text[::-1]\nhover =hover[::-1]\nz = z[::-1]\n\n# Set Colorscale\ncolorscale=[[.0, 'skyblue'], [.3, 'blue'], [.5, 'navy'], [1.0, 'cyan']\n           ]\n\n\n# Make Annotated Heatmap\nfig = ff.create_annotated_heatmap(z, annotation_text=bg_text, text=hover,\n                                 colorscale='blues', font_colors=['white'], hoverinfo='text')\nfig.update_layout(width=900,\n                  height=400,\n                 )                \n\nfig.show()","metadata":{"_kg_hide-input":true,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-08-12T08:58:55.938724Z","iopub.execute_input":"2022-08-12T08:58:55.939045Z","iopub.status.idle":"2022-08-12T08:58:58.872505Z","shell.execute_reply.started":"2022-08-12T08:58:55.938974Z","shell.execute_reply":"2022-08-12T08:58:58.871946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"toc\"></a>\n\n# Table of Contents\n\n* [0. Introduction](#0)   \n* [1. Templates](#1)\n* [2. Basic Plots](#2)\n    * [2.1 Histogram: basic](#2.1)\n    * [2.2 Histogram: ordered](#2.2)\n    * [2.3 Histogram: oredered and highlighted](#2.3)\n    * [2.4 Bar plots/horizontal histogram: (+ annotation)](#2.4)\n    * [2.5 Scatter/line plots](#2.5)\n    * [2.6 Pie Charts](#2.6)\n* [3. Distribution/kde plot](#3)\n* [4. Correlation Heatmaps](#4)\n    * [4.1 Correlation Heatmaps:full matrix ](#4.1)\n    * [4.2 Correlation Heatmaps:lower-triangular matrix ](#4.2)\n    * [4.3 Correlation Heatmaps:annotated matrix ](#4.3)\n* [5. Annotations](#5)\n* [6. Subplots](#6)\n    * [6.1 Mixed plots/subplots](#6.1)\n    * [6.2 Facet grids/pairplots](#6.2)\n    * [6.3 Daigonal Facet grids (custom made)](#6.3)\n* [7. Radar Charts](#7)\n* [8. 3D Plots](#8)\n* [9. Choropleth Map (Animation)](#9)\n* [10. Bonus: Getting creative with annotated heatmaps](#10)\n* [11. Reference](#11)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"0\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>0. Introduction</b></font>\n\nPlotly is an interactive, **open-source** plotting library that supports many unique chart types covering a wide range of areas  such as statistical, financial, geographic, scientific, and 3-dimensional use-cases.\n\nPlotly enables Python users to create beautiful **interactive** web-based visualizations that can be displayed in Jupyter notebooks, saved to standalone HTML files, or served as part of pure Python-built web applications using Dash.\n\nIn this notebook I aim to give some tips/examples on **customizing** plotly charts/plot for enhanced visualizations. Since this notebook is about customizing plots, I will be using several datasets which may or may not be related to each other as long as they are suitable for demonstrations.","metadata":{}},{"cell_type":"markdown","source":"<font color=\"lightseagreen\" size=+1.5><b> Import libraries and load datasets </b></font>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom scipy import stats\nimport seaborn as sns\nimport plotly.io as pio\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom sklearn.preprocessing import LabelEncoder\nfrom plotly.subplots import make_subplots\nfrom plotly.offline import init_notebook_mode, iplot\ninit_notebook_mode(connected=True)\npio.templates.default = \"none\"\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\ndf_study = pd.read_csv(r'/kaggle/input/students-performance-in-exams/StudentsPerformance.csv')\ndf_happy18 = pd.read_csv(r'/kaggle/input/world-happiness/2018.csv')\ndf_titanic = pd.read_csv(r'/kaggle/input/titanic/train.csv')\ndf_heart = pd.read_csv(r'/kaggle/input/heart-disease-uci/heart.csv')\ndf_netFlix = pd.read_csv(r'/kaggle/input/netflix-shows/netflix_titles.csv')\ndf_house = pd.read_csv(r'/kaggle/input/c/house-prices-advanced-regression-techniques/train.csv')\ndf_rain = pd.read_csv(r'/kaggle/input/weather-dataset-rattle-package/weatherAUS.csv')\ndf_iris = pd.read_csv(r'/kaggle/input/iris/Iris.csv')\ndf_sales = pd.read_csv(r'/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv')\ndf_vaccine = pd.read_csv(r'/kaggle/input/covid-world-vaccination-progress/country_vaccinations.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-12T08:58:58.873671Z","iopub.execute_input":"2022-08-12T08:58:58.873951Z","iopub.status.idle":"2022-08-12T08:59:01.754136Z","shell.execute_reply.started":"2022-08-12T08:58:58.873927Z","shell.execute_reply":"2022-08-12T08:59:01.753444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>1. Templates</b></font>\n\nAs good a plotting library as plotly is, its default template is not pleasing to the eye and we often need to customize in order to have an informative as well as better looking plots and charts. Plotly offers the following background/plot-area templates to choose from. \n\n >- `plotly` \n >- `ggplot2`     \n >- `seaborn`\n >- `plotly_dark`\n >- `simple_white`\n     \n**Note**: You can also customize your own backgroud/paper color if you wish to. Let's demonstrate using the titanic dataset.\n","metadata":{}},{"cell_type":"code","source":"df = df_titanic\nfig = px.histogram(df, x=\"Survived\", y=None, color=\"Sex\",\n                width=600,height=350,\n                histnorm='percent',\n                color_discrete_map={ \n                    \"male\": \"RebeccaPurple\", \"female\": \"lightsalmon\"\n                },\n                template=\"plotly\"\n                )\nfig.update_layout(title=\" template='plotly'\", \n                  font_family=\"San Serif\",\n                  bargap=0.2,\n                  barmode='group',\n                  titlefont={'size': 24},\n                  legend=dict(\n                  orientation=\"v\", \n                      y=1, \n                      yanchor=\"top\", \n                      x=1.250, \n                      xanchor=\"right\",)                 \n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:01.755123Z","iopub.execute_input":"2022-08-12T08:59:01.755322Z","iopub.status.idle":"2022-08-12T08:59:02.403524Z","shell.execute_reply.started":"2022-08-12T08:59:01.755298Z","shell.execute_reply":"2022-08-12T08:59:02.402572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# available templates\ntemplate = ['ggplot2','plotly_dark', 'seaborn', 'simple_white', 'plotly']\n\n\ndf = df_titanic\nfig = px.histogram(df, \n                   x=\"Survived\", \n                   y=None, \n                   color=\"Sex\",\n                   width=600,\n                   height=350,\n                   histnorm='percent',\n                   color_discrete_map={\n                       \"male\": \"RebeccaPurple\", \"female\": \"lightsalmon\"\n                       },\n                   template=\"plotly_dark\"\n                   )\n\nfig.update_layout(title=\" template='plotly_dark'\", \n                  font_family=\"San Serif\",\n                  bargap=0.2,\n                  barmode='group',\n                  titlefont={'size': 24},\n                  legend=dict(\n                  orientation=\"v\", y=1, yanchor=\"top\", x=1.25, xanchor=\"right\")                \n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.404623Z","iopub.execute_input":"2022-08-12T08:59:02.404888Z","iopub.status.idle":"2022-08-12T08:59:02.487126Z","shell.execute_reply.started":"2022-08-12T08:59:02.404860Z","shell.execute_reply":"2022-08-12T08:59:02.486381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_titanic\nfig = px.histogram(df, x=\"Survived\", y=None, color=\"Sex\",\n                width=600,height=350,\n                histnorm='percent',\n                color_discrete_map={ \n                    \"male\": \"RebeccaPurple\", \"female\": \"lightsalmon\"\n                },\n                template=\"ggplot2\"\n                )\n\nfig.update_layout(title=\" template='ggplot2'\", \n                  font_family=\"San Serif\",\n                  bargap=0.2,\n                  barmode='group',\n                  #coloraxis=False,\n                  titlefont={'size': 24},\n                  legend=dict(\n                  orientation=\"v\", y=1, yanchor=\"top\", x=1.25, xanchor=\"right\",)                 \n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.490013Z","iopub.execute_input":"2022-08-12T08:59:02.490509Z","iopub.status.idle":"2022-08-12T08:59:02.600508Z","shell.execute_reply.started":"2022-08-12T08:59:02.490475Z","shell.execute_reply":"2022-08-12T08:59:02.599072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_titanic\nfig = px.histogram(df, x=\"Survived\", y=None, color=\"Sex\",\n                width=600,height=350,\n                histnorm='density',\n                color_discrete_map={ \n                    \"male\": \"RebeccaPurple\", \"female\": \"lightsalmon\"\n                },\n                template=\"seaborn\"\n                )\n\nfig.update_layout(title=\" template='seaborn'\", \n                  font_family=\"San Serif\",\n                  bargap=0.2,\n                  barmode='group',\n                  titlefont={'size': 24},\n                  legend=dict(\n                  orientation=\"v\", y=1, yanchor=\"top\", x=1.25, xanchor=\"right\",)                 \n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.602390Z","iopub.execute_input":"2022-08-12T08:59:02.602610Z","iopub.status.idle":"2022-08-12T08:59:02.690353Z","shell.execute_reply.started":"2022-08-12T08:59:02.602586Z","shell.execute_reply":"2022-08-12T08:59:02.689814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_titanic\nfig = px.histogram(df, x=\"Survived\", y=None, color=\"Sex\",\n                width=600,height=350,\n                histnorm='percent',\n                color_discrete_map={ \n                    \"male\": \"RebeccaPurple\", \"female\": \"lightsalmon\"\n                },\n                template=\"simple_white\"\n                )\nfig.update_layout(title=\" template='simple_white'\", \n                  font_family=\"San Serif\",\n                  bargap=0.2,\n                  barmode='group',\n                  titlefont={'size': 24},\n                  legend=dict(\n                  orientation=\"v\", \n                      y=1, \n                      yanchor=\"top\", \n                      x=1.250, \n                      xanchor=\"right\",)                 \n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.691183Z","iopub.execute_input":"2022-08-12T08:59:02.691470Z","iopub.status.idle":"2022-08-12T08:59:02.773812Z","shell.execute_reply.started":"2022-08-12T08:59:02.691447Z","shell.execute_reply":"2022-08-12T08:59:02.772957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_titanic\nfig = px.histogram(df, x=\"Survived\", y=None, color=\"Sex\",\n                width=600,height=350,\n                histnorm='percent',\n                color_discrete_map={ \n                    \"male\": \"skyblue\", \"female\": \"darkblue\"\n                },\n                template=\"simple_white\"\n                )\nfig.update_layout(title=\"paper/plot bgcolor customized\", \n                  font_family=\"San Serif\",\n                  bargap=0.2,\n                  barmode='group',\n                  titlefont={'size': 24},\n                  paper_bgcolor='lightgray',#we can also choose the paper background color on top of a template\n                  plot_bgcolor='lightsalmon',#we can also choose the plot ackground color on top of a template\n                  legend=dict(\n                  orientation=\"v\", \n                      y=1, \n                      yanchor=\"top\", \n                      x=1.250, \n                      xanchor=\"right\",)                 \n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.774808Z","iopub.execute_input":"2022-08-12T08:59:02.775028Z","iopub.status.idle":"2022-08-12T08:59:02.841873Z","shell.execute_reply.started":"2022-08-12T08:59:02.775003Z","shell.execute_reply":"2022-08-12T08:59:02.841255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\">Back to top</a>\n\n<a id=\"2\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>2. Basic Plots</b></font>\n\n<a id=\"2.1\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>2.1 Histogram: basic</b></font>\n","metadata":{}},{"cell_type":"code","source":"fig = px.histogram(df_study, x=\"race/ethnicity\",\n                   width=600, \n                   height=500,\n                   histnorm='percent',\n                   template=\"simple_white\"\n                   )\nfig.update_layout(title=\"Race/Ethnic Group\", \n                  font_family=\"San Serif\",\n                  titlefont={'size': 20},\n                  showlegend=True,\n                  legend=dict(\n                      orientation=\"v\",\n                      y=1.0, \n                      yanchor=\"top\", \n                      x=1.0, \n                      xanchor=\"right\"\n                  )                 \n                 )\nfig.update_traces(marker_color=None, marker_line_color='white',\n                  marker_line_width=1.5, opacity=0.99)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.842718Z","iopub.execute_input":"2022-08-12T08:59:02.843013Z","iopub.status.idle":"2022-08-12T08:59:02.914352Z","shell.execute_reply.started":"2022-08-12T08:59:02.842988Z","shell.execute_reply":"2022-08-12T08:59:02.913381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2.2\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>2.2 Histogram: ordered</b></font>\n\n> Ordered bar/histogram according to y-value (percentage of students in each race/ethnicity group).","metadata":{}},{"cell_type":"code","source":"fig = px.histogram(df_study, x=\"race/ethnicity\",\n                   width=600, \n                   height=400,\n                   histnorm='percent',\n                   template=\"simple_white\"\n                   )\nfig.update_layout(title=\"Race/Ethnic Group\", \n                  font_family=\"San Serif\",\n                  titlefont={'size': 20},\n                  showlegend=True,\n                  legend=dict(\n                      orientation=\"v\", \n                      y=1, \n                      yanchor=\"top\", \n                      x=1.0, \n                      xanchor=\"right\"\n                  )                \n                 ).update_xaxes(categoryorder='total descending')#\nfig.update_traces(#marker_color='lightgray', marker_line_color='seagreen', \n                  marker_line_width=1.5, opacity=0.99)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.915387Z","iopub.execute_input":"2022-08-12T08:59:02.915616Z","iopub.status.idle":"2022-08-12T08:59:02.986769Z","shell.execute_reply.started":"2022-08-12T08:59:02.915592Z","shell.execute_reply":"2022-08-12T08:59:02.985932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2.3\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>2.3 Histogram: ordered and highlighted</b></font>\n\n> Highlight *important* message you want to communicate. We can do so by assigning different colors to specific categories (max and min in this example)","metadata":{}},{"cell_type":"code","source":"fig = px.histogram(df_study, x=\"race/ethnicity\", \n                   width=600, \n                   height=400,\n                   histnorm='percent',\n                   category_orders={\n                       \"race/ethnicity\": [\"group A\", \"group B\", \"group C\", \"group D\", \"group E\"],\n                       \"gender\": [\"male\", \"female\"]\n                   },\n                   color_discrete_map={\n                       \"male\": \"RebeccaPurple\", \"female\": \"lightsalmon\",\n                       },\n                   template=\"simple_white\"\n                   )\n\nfig.update_layout(title=\"Race/Ethnic Group\", \n                  font_family=\"San Serif\",\n                  titlefont={'size': 20},\n                  legend=dict(\n                  orientation=\"v\", y=1, yanchor=\"top\", x=1.0, xanchor=\"right\" )                 \n                 ).update_xaxes(categoryorder='total descending') # ordering the x-axis values\n# custom color\ncolors = ['gray',] * 5 # assign FIVE colors (5 ==length of x-axis) \ncolors[4] = 'crimson' # assign another color for the lowest count (group A)\ncolors[0] = 'lightseagreen' # assign another color for the highr count (group E)\n\n\nfig.update_traces(marker_color=colors, marker_line_color=None,\n                  marker_line_width=2.5, opacity=None)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:02.987829Z","iopub.execute_input":"2022-08-12T08:59:02.988064Z","iopub.status.idle":"2022-08-12T08:59:03.058847Z","shell.execute_reply.started":"2022-08-12T08:59:02.988039Z","shell.execute_reply":"2022-08-12T08:59:03.058281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2.4\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>2.4 Bar plots/Horizontal histogram</b></font>","metadata":{}},{"cell_type":"code","source":"colors = ['rgba(38, 24, 74, 0.8)', 'rgba(71, 58, 131, 0.8)',\n          'rgba(122, 120, 168, 0.8)', 'rgba(164, 163, 204, 0.85)',\n          'rgba(190, 192, 213, 1)']\n\nfig = px.histogram(df_netFlix, \n                   y=\"type\",\n                   orientation='h',\n                   width=600,\n                   height=350,\n                   histnorm='percent',\n                   template=\"plotly_dark\"\n                   )\nfig.update_layout(title=\"<b>Netflix Content: TV Show vs Movies<b>\", \n                  font_family=\"San Serif\",\n                  bargap=0.2,\n                  barmode='group',\n                  titlefont={'size': 28},\n                  paper_bgcolor='lightgray',\n                  plot_bgcolor='lightgray',\n                  legend=dict(\n                  orientation=\"v\", \n                      y=1, \n                      yanchor=\"top\", \n                      x=1.250, \n                      xanchor=\"right\",)                 \n                  )\nannotations = []\nannotations.append(dict(xref='paper', yref='paper',\n                        x=0.0, y=1.2,\n                        text='Netflix mostly produce Movies over TV shows.',\n                             font=dict(family='Arial', size=16, color=colors[2]),\n                        showarrow=False))\nannotations.append(dict(xref='paper', yref='paper',\n                        x=0.49, y=0.85,\n                        text='30.4%',\n                             font=dict(family='Arial', size=20, color=colors[2]),\n                        showarrow=False))\nannotations.append(dict(xref='paper', yref='paper',\n                        x=1.09, y=0.19,\n                        text='69.6%',\n                             font=dict(family='Arial', size=20, color=colors[2]),\n                        showarrow=False))\n\nfig.update_layout(\n    autosize=False,\n    width=600,\n    height=350,\n    margin=dict(\n        l=50,\n        r=50,\n        b=50,\n        t=120,\n    ),\n)\n\nfig.update_layout(annotations=annotations)\nfig.update_xaxes(showgrid=False)\nfig.update_yaxes(showgrid=False)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.059678Z","iopub.execute_input":"2022-08-12T08:59:03.059973Z","iopub.status.idle":"2022-08-12T08:59:03.301258Z","shell.execute_reply.started":"2022-08-12T08:59:03.059949Z","shell.execute_reply":"2022-08-12T08:59:03.300694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2.5\"></a>\n\n<font color=\"lightseagreen\" size=+1.5><b>2.5 Scatter/line plots</b></font>\n\n>- Highlight only the head story while keeping others in the background\n>- Add secondary y-axis\n","metadata":{}},{"cell_type":"code","source":"WINdIR = ['E','ENE', 'NE', 'NNE', 'N', 'NNW', 'NW', 'WNW', 'W', 'WSW', 'SW', 'SSW', 'S', 'SSE', 'SE', 'ESE']\n\n\nfig = make_subplots(specs=[[{\"secondary_y\": True}]])\nfig.add_trace(go.Scatter(y=df_rain.groupby('WindDir9am')['Rainfall'].mean(),\n                         x=WINdIR,\n                         name = 'Wind dir. 3pm',\n                         line=dict(color='royalblue', width=4, dash='solid')\n                         )\n             )\nfig.add_trace(go.Scatter(y=df_rain.groupby('WindDir3pm')['Rainfall'].mean(),\n                         x=WINdIR,\n                         name = 'Wind dir. 9am',\n                         line=dict(color='skyblue', width=2, dash='dot')\n                         )\n             )\nfig.add_trace(go.Scatter(y=df_rain.groupby('WindDir3pm')['Evaporation'].mean(),\n                         x=WINdIR,\n                         name = 'Wind dir. 9am',\n                         line=dict(color='darkgray', width=2, dash='dot')\n                         ),secondary_y=True,\n             )\nfig.add_trace(go.Scatter(y=df_rain.groupby('WindDir9am')['Evaporation'].mean(),\n                         x=WINdIR,\n                         name = 'Wind dir. 9am',\n                         line=dict(color='darkgray', width=2, dash='dot')\n                         ),secondary_y=True,\n             )\nfig.update_layout(title='<b>Rainfall & Evaporation vs Wind Direction<b>',\n                  xaxis_title='<b>Wind Direction<b>',\n                  yaxis_title='<b>Rainfall [mm]<b>',\n                  titlefont={'size': 28, 'family':'Serif'},\n                  template='simple_white',\n                  showlegend=True,\n                  paper_bgcolor='#edeeee',\n                  plot_bgcolor='#edeeee',\n                  width=950, height=500,\n                 )\nfig.update_yaxes(title_text=\"<b>Evaporation [mm]</b>\", secondary_y=True)\nfig.show()              \n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.302118Z","iopub.execute_input":"2022-08-12T08:59:03.302416Z","iopub.status.idle":"2022-08-12T08:59:03.413227Z","shell.execute_reply.started":"2022-08-12T08:59:03.302391Z","shell.execute_reply":"2022-08-12T08:59:03.412708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2.6\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>2.6 Pie Charts</b></font>\n\nGood practice while using Pie charts:\n>- Avoid using/displaying `too many categories` in a pie-chart. If you have to display more that say 10 categories, may be its time to consider other chart types.\n>- Hide a `long-list of legends`. When it is too long it's a distruction. \n>- Use `pulled-sector` only to highlight something interesting, not because it looks cool.\n>- Use `sunburst` charts to your advantage. They are quite useful in communicating hierarchical data.  \n>- If you use `donut` chart, use the center to display useful information. However, try to avoid very thin donut pies.","metadata":{}},{"cell_type":"code","source":"# data covid19 vaccination\n\nDF= pd.DataFrame(df_vaccine.groupby(\"country\")[\"people_fully_vaccinated\"].max().sort_values(ascending = False))\nDF.reset_index(level=0,inplace=True)\n\nfig = px.pie(DF,\n             values='people_fully_vaccinated',\n             names='country',\n             hover_data=['country'],\n             labels={'country':'country'}\n            )\nfig.update_traces(textposition='inside',\n                  textinfo='percent+label'\n                 )\nfig.update_layout(title='<b>COVID19 Fully Vaccinated Countries<b>',\n                  titlefont={'size': 24,\n                             'family': 'Serif',\n                            },\n                  showlegend=True, \n                  width=600, \n                  height=600)\nannotations = []\nannotations.append(dict(xref='paper', yref='paper',\n                        x=0.12, y=1.03,\n                        text=\"<i>(<b>Bad pie chart</b>: Large nr. of catergories with long list of legend)<i>\",\n                             font=dict(\n                                 family='Arial', \n                                 size=14, \n                                 color='black'\n                             ),\n                        showarrow=False)\n                  )\nfig.update_layout(annotations=annotations)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.414031Z","iopub.execute_input":"2022-08-12T08:59:03.414323Z","iopub.status.idle":"2022-08-12T08:59:03.468872Z","shell.execute_reply.started":"2022-08-12T08:59:03.414299Z","shell.execute_reply":"2022-08-12T08:59:03.468147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.02*DF['people_fully_vaccinated'].sum()\nm = DF['people_fully_vaccinated'] < threshold\nDF_short = DF[~m].append(DF.loc[m, ['people_fully_vaccinated']].sum().rename(8))\nDF_short['country'][DF_short['country'].isna()] = 'Other Countries'\nDF_short\n\nfig = px.pie(DF_short,\n             values='people_fully_vaccinated',\n             names='country',\n             hover_data=['country'],\n             labels={'country':'country'}\n            )\nfig.update_traces(textposition='inside',\n                  textinfo='percent+label'\n                 )\nfig.update_layout(title='<b>COVID19 Fully Vaccinated Countries<b>',\n                  titlefont={'size': 24,\n                             'family': 'Serif',\n                            },\n                  showlegend=False, \n                  width=600, \n                  height=600)\nannotations = []\nannotations.append(dict(xref='paper', yref='paper',\n                        x=0.12, y=1.03,\n                        text=\"<i>(<b>Better</b>: Small nr. of catergories w/out long list of legend)<i>\",\n                             font=dict(\n                                 family='Arial', \n                                 size=14, \n                                 color='black'\n                             ),\n                        showarrow=False)\n                  )\nfig.update_layout(annotations=annotations)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.469830Z","iopub.execute_input":"2022-08-12T08:59:03.470050Z","iopub.status.idle":"2022-08-12T08:59:03.512847Z","shell.execute_reply.started":"2022-08-12T08:59:03.470026Z","shell.execute_reply":"2022-08-12T08:59:03.512293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data students performance\nfig = go.Figure()\nfig.add_trace(\n    go.Pie(\n        labels=df_study['parental level of education'],\n        values=None,\n        pull=[0, 0, 0.15, 0, 0, 0],\n        ),   \n    )\nfig.update_traces(\n    hoverinfo='label+value',\n    textinfo='label+percent',\n    textfont_size=12,\n    marker=dict(line=dict(\n        color='#000000',\n        width=2)\n        )\n    )\nfig.layout.update(title=\"<b> Parental Level of Education <b>\",\n                  titlefont={'size':28, 'family': 'Serif',},\n                  showlegend=False, \n                  height=600, \n                  width=600, \n                  template=None, \n                 )\nannotations = []\nannotations.append(dict(xref='paper', yref='paper',\n                        x=0.12, y=1.03,\n                        text=\"<i>(* Parents with master's degree are the least represented)<i>\",\n                             font=dict(\n                                 family='Arial', \n                                 size=14, \n                                 color='black'\n                             ),\n                        showarrow=False)\n                  )\nfig.update_layout(annotations=annotations)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.513602Z","iopub.execute_input":"2022-08-12T08:59:03.513908Z","iopub.status.idle":"2022-08-12T08:59:03.550401Z","shell.execute_reply.started":"2022-08-12T08:59:03.513885Z","shell.execute_reply":"2022-08-12T08:59:03.549835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data students performance\nfig = px.sunburst(df_study,\n                  path=['race/ethnicity', 'gender', 'parental level of education'])\nfig.update_layout(title_text=\"<b>Parental Level of Education vs {Race, Gender}<b>\", \n                  titlefont={'size': 24, 'family':'Serif'},\n                  width=750, \n                  height=750,\n                 )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.551232Z","iopub.execute_input":"2022-08-12T08:59:03.551528Z","iopub.status.idle":"2022-08-12T08:59:03.664386Z","shell.execute_reply.started":"2022-08-12T08:59:03.551503Z","shell.execute_reply":"2022-08-12T08:59:03.663514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data titanic\nfig = make_subplots(rows=1, cols=2,\n                    specs=[[{'type':'domain'}, {'type':'domain'}],\n                          ])\nfig.add_trace(\n    go.Pie(\n        labels=df_titanic['Survived'],\n        values=None,\n        hole=.4,\n        title='Survived',\n        titlefont={'color':None, 'size': 24},       \n\n        ),\n    row=1,col=1\n    )\nfig.update_traces(\n    hoverinfo='label+value',\n    textinfo='label+percent',\n    textfont_size=12,\n    marker=dict(\n        colors=['lightgray', 'lightseagreen'],\n        line=dict(color='#000000',\n                  width=2)\n        )\n    )\n\nfig.add_trace(\n    go.Pie(\n        labels=df_titanic['Sex'],\n        values=None,\n        hole=.4,\n        title='Sex',\n        titlefont={'color':None, 'size': 24},\n        ),\n    row=1,col=2\n    )\nfig.update_traces(\n    hoverinfo='label+value',\n    textinfo='label+percent',\n    textfont_size=12,\n    marker=dict(\n        colors=['lightgray', 'lightseagreen'],\n        line=dict(color='#000000',\n                  width=2)\n        )\n    )\nfig.layout.update(title=\"<b> Some features of the Titanic dataset<b>\",\n                  titlefont={'color':None, 'size': 24, 'family': 'San-Serif'},\n                  showlegend=False, \n                  height=500, \n                  width=750,\n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.665770Z","iopub.execute_input":"2022-08-12T08:59:03.666090Z","iopub.status.idle":"2022-08-12T08:59:03.714113Z","shell.execute_reply.started":"2022-08-12T08:59:03.666056Z","shell.execute_reply":"2022-08-12T08:59:03.713202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data students performance\nfig = make_subplots(rows=1, cols=2,\n                    specs=[[{'type':'domain'}, {'type':'domain'}],\n                          ])\nfig.add_trace(\n    go.Pie(\n        labels=df_study['parental level of education'],\n        title=\"Parental Level of Education\",\n                  titlefont={'size':20, 'family': 'Serif',},\n        values=None,\n        hole=0.85,\n        ),  col=1, row=1, \n    )\nfig.update_traces(\n    hoverinfo='label+value',\n    textinfo='label+percent',\n    textfont_size=12,\n    )\n\nfig.add_trace(\n    go.Pie(\n        labels=df_study['parental level of education'],\n        title=\"Parental Level of Education\",\n                  titlefont={'size':20, 'family': 'Serif',},\n        values=None,\n        hole=0.5,\n        ),  col=2, row=1, \n    )\nfig.update_traces(\n    hoverinfo='label+value',\n    textinfo='label+percent',\n    textfont_size=12,\n    )\nfig.layout.update(title=\"<b> Avoid very thin donut pies [right plot is better than the left] <b>\",\n                  titlefont={'size':20, 'family': 'Serif',},\n                  showlegend=False, \n                  height=500, \n                  width=1100, \n                  template=None, \n                 )\n\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.715130Z","iopub.execute_input":"2022-08-12T08:59:03.715354Z","iopub.status.idle":"2022-08-12T08:59:03.752602Z","shell.execute_reply.started":"2022-08-12T08:59:03.715331Z","shell.execute_reply":"2022-08-12T08:59:03.751902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\">Back to top</a>\n\n<a id=\"3\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>3. Distribution (kde) plot</b></font>\n\n`Half-violin` in a horizontal orientation gives a very good alternative to the kde plot seaborn library offers. Having said that the `.create_distplot()` from `plotly.figure_factory` works fine with little work.","metadata":{}},{"cell_type":"code","source":"# data for plotting (happiness wold countries)\nhappy = df_happy18[df_happy18['Overall rank'] < 79]['GDP per capita']# assume half the coutries are happy \nless_happy = df_happy18[df_happy18['Overall rank'] >= 79]['GDP per capita']# the other half are less happy\n\nfig = go.Figure()\nfig.add_trace(go.Violin(x=happy, line_color='lightseagreen', name='Happy', y0=0))\nfig.add_trace(go.Violin(x=less_happy, line_color='red', name= 'Less Happy', y0=0))\n\nfig.update_traces(orientation='h', side='positive', meanline_visible=False)\nfig.update_layout(xaxis_showgrid=False, xaxis_zeroline=False)\n\nfig.update_layout(title='<b>GDP per capita (happy vs less happy countries)<b>',\n                  xaxis_title='GDP per capita',\n                  titlefont={'size': 24},\n                  width=600,\n                  height=400,\n                  template=\"plotly_dark\",\n                  showlegend=True,\n                  paper_bgcolor=\"lightgray\",\n                  plot_bgcolor='lightgray', \n                  font=dict(\n                      color ='black',\n                      )\n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.753689Z","iopub.execute_input":"2022-08-12T08:59:03.754068Z","iopub.status.idle":"2022-08-12T08:59:03.809513Z","shell.execute_reply.started":"2022-08-12T08:59:03.754037Z","shell.execute_reply":"2022-08-12T08:59:03.808963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group_labels = ['Happy', 'Less happy']\nfig = ff.create_distplot([happy, less_happy],\n                         group_labels, \n                         show_hist=False, \n                         show_rug=False,\n                         )\nfig.update_layout(title='<b>Happiness of countries vs GDP<b>',\n                  xaxis_title='GDP per capita', \n                  yaxis_title='density',\n                  titlefont={'size': 28},\n                  font_family = 'San Serif',\n                  width=600,height=400,\n                  template=\"plotly_dark\",\n                  showlegend=True,\n                  paper_bgcolor=\"black\",\n                  font=dict(\n                      color ='white',\n                      ),\n                  legend=dict(\n                      orientation=\"v\",\n                      y=1, \n                      yanchor=\"top\", \n                      x=1.0, \n                      xanchor=\"right\",\n                  )\n                 )                  \nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.810431Z","iopub.execute_input":"2022-08-12T08:59:03.810805Z","iopub.status.idle":"2022-08-12T08:59:03.875970Z","shell.execute_reply.started":"2022-08-12T08:59:03.810776Z","shell.execute_reply":"2022-08-12T08:59:03.875389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>4. Correlation Heatmaps</b></font>\n\nCorrelation heatmap is a very good way of summerizing how features of a dataset are related to one another or with the target variable. It gives a helicopter-view of all the features in a compact and beautifull correlation matrix. Often times seaborn offers an excellent plotting functon and it is a go-to option even when people try to make a plolty-only EDA. The main reason is that plotly's default heatmap is not pleasing to the eye. However, when cutomized properly plotly can also be beautifully pleasing with the added interactivness as well.\n\nBellow are few examples. \n\n<a id=\"4.1\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>4.1 Correlation Heatmap: full matrix</b></font>","metadata":{}},{"cell_type":"code","source":"df_Heart = df_heart[['age', 'trestbps', 'chol', 'thalach', 'oldpeak']]\n\ncorr = df_Heart.corr()\nfig = go.Figure(data= go.Heatmap(z=corr,\n                                 x=corr.index.values,\n                                 y=corr.columns.values,\n                                 colorscale='earth',\n                                 )\n                )\nfig.update_layout(title_text='<b>Correlation Matrix (cont. features)<b>',\n                  title_x=0.5,\n                  titlefont={'size': 24},\n                  width=550, height=550,\n                  xaxis_showgrid=False,\n                  yaxis_showgrid=False,\n                  yaxis_autorange='reversed', \n                  paper_bgcolor=None,\n                  )\nfig.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.876851Z","iopub.execute_input":"2022-08-12T08:59:03.877174Z","iopub.status.idle":"2022-08-12T08:59:03.895198Z","shell.execute_reply.started":"2022-08-12T08:59:03.877142Z","shell.execute_reply":"2022-08-12T08:59:03.894380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4.2\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>4.2 Correlation Heatmap: annotated full matrix</b></font>","metadata":{}},{"cell_type":"code","source":"df_Heart = df_heart[['age', 'trestbps', 'chol', 'thalach', 'oldpeak']]\ncorr = df_Heart.corr()\nfig = ff.create_annotated_heatmap(\n    z=corr.to_numpy().round(2),\n    x=list(corr.index.values),\n    y=list(corr.columns.values),       \n    xgap=3, ygap=3,\n    zmin=-1, zmax=1,\n    colorscale='earth',\n    colorbar_thickness=30,\n    colorbar_ticklen=3,\n)\nfig.update_layout(title_text='<b>Correlation Matrix (cont. features)<b>',\n                  title_x=0.5,\n                  titlefont={'size': 24},\n                  width=550, height=550,\n                  xaxis_showgrid=False,\n                  xaxis={'side': 'bottom'},\n                  yaxis_showgrid=False,\n                  yaxis_autorange='reversed',                   \n                  paper_bgcolor=None,\n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.896173Z","iopub.execute_input":"2022-08-12T08:59:03.896390Z","iopub.status.idle":"2022-08-12T08:59:03.932895Z","shell.execute_reply.started":"2022-08-12T08:59:03.896367Z","shell.execute_reply":"2022-08-12T08:59:03.932130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4.3\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>4.3 Correlation Heatmap: lower-triangular & annotated matrix</b></font>","metadata":{}},{"cell_type":"code","source":"df_Heart = df_heart[['age', 'trestbps', 'chol', 'thalach', 'oldpeak']]\ncorr = df_Heart.corr()\n\nmask = np.triu(np.ones_like(corr, dtype=np.bool))\n\ncorr = corr.mask(mask)\nfig = ff.create_annotated_heatmap(\n    z=corr.to_numpy().round(2),\n    x=list(corr.index.values),\n    y=list(corr.columns.values),       \n    xgap=3, ygap=3,\n    zmin=-1, zmax=1,\n    colorscale='earth',\n    colorbar_thickness=30,\n    colorbar_ticklen=3,\n)\nfig.update_layout(title_text='<b>Correlation Matrix (cont. features)<b>',\n                  title_x=0.5,\n                  titlefont={'size': 24},\n                  width=550, height=550,\n                  xaxis_showgrid=False,\n                  xaxis={'side': 'bottom'},\n                  yaxis_showgrid=False,\n                  yaxis_autorange='reversed',                   \n                  paper_bgcolor='lightgray',\n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.933849Z","iopub.execute_input":"2022-08-12T08:59:03.934066Z","iopub.status.idle":"2022-08-12T08:59:03.966739Z","shell.execute_reply.started":"2022-08-12T08:59:03.934042Z","shell.execute_reply":"2022-08-12T08:59:03.965937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\">Back to top</a>\n\n<a id=\"5\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>5. Annotations</b></font>\n\nWith annotation you can draw the attention of your reader to the a specific point(s) you would like to focus. Area annotation and text annotation are two examples as demonstated using the examples below. We have seen this already at [section 2.4](#2.4) (text annotation). \n","metadata":{}},{"cell_type":"code","source":"#...........data: housing \nimportant_features = ['LotArea', 'OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF', \n                      '1stFlrSF', 'FullBath', 'TotRmsAbvGrd', 'YearBuilt','YearRemodAdd', 'SalePrice']\nDF = df_house[important_features]\n\n\n#.......main plot (scatter)\nfig = px.scatter(DF,\n                 x='GrLivArea',\n                 y='SalePrice',\n                 color='OverallQual',\n                 size='LotArea',\n                 width=750,\n                 height=400)\n\n#......annotation (text)\nfig.add_annotation(text=\"Possible outliers\",\n                   xref=\"x\", yref=\"y\",\n                   x=6200,y=160000,\n                   showarrow = True,                   \n                   yshift=30,\n                   xshift=-60,\n                   font=dict(\n                       family=\"sans serif\",\n                       size=12,\n                       color=\"LightSeaGreen\"\n                       )\n                  )\n#......annotation (box)\nfig.add_shape(type=\"rect\",\n              xref=\"x\", yref=\"y\",\n              x0=4500, x1=5800, y0=100000, y1=250000,\n              fillcolor=\"lightgray\",    \n              line_color=\"yellow\",\n              opacity=0.25\n              )\n#......update the plot as you wish\nfig.update_layout(title='<b>House Price vs GrLivArea<b>',\n                  titlefont={'size': 24},\n                  template='plotly_dark'\n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:03.967887Z","iopub.execute_input":"2022-08-12T08:59:03.968157Z","iopub.status.idle":"2022-08-12T08:59:04.076751Z","shell.execute_reply.started":"2022-08-12T08:59:03.968134Z","shell.execute_reply":"2022-08-12T08:59:04.075914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_titanic['Age'] = df_titanic['Age'].fillna(df_titanic['Age'].mode().iloc[0])\n\n# data to plot:  age distribution survived vs not-sutvived\nsurv = df_titanic[df_titanic['Survived'] == 1]['Age']\nvict = df_titanic[df_titanic['Survived'] == 0]['Age']\n\n\ngroup_labels = ['Survived', 'Victims']\nfig = ff.create_distplot([surv, vict],\n                         group_labels, \n                         show_hist=False, \n                         show_rug=False,\n                         )\nfig.add_vrect(\n    x0=2.01, x1=17.99,\n    annotation_text=\"Young children\", annotation_position=\"top\",\n    fillcolor=\"skyblue\", opacity=0.5,\n    layer=\"below\", line_width=0,\n),\nfig.add_vrect(\n    x0=18, x1=44.99,\n    annotation_text=\"Young adults\", annotation_position=\"top\",\n    fillcolor=\"Salmon\", opacity=0.5,\n    layer=\"below\", line_width=0,\n),\nfig.add_vrect(\n    x0=45, x1=64.99,\n    annotation_text=\"Mid age\", annotation_position=\"top\",\n    fillcolor=\"lightgray\", opacity=0.5,\n    layer=\"above\", line_width=0,\n),\nfig.add_vrect(\n    x0=65, x1=80,\n    annotation_text=\"Senior\", annotation_position=\"top\",\n    fillcolor=\"gray\", opacity=0.5,\n    layer=\"below\", line_width=0,\n),\n\n# annotations = []\n# annotations.append(dict(xref='paper', yref='paper',\n#                         x=0.05, y=0.8,\n#                         text='Young children',\n#                         font=dict(family='Arial', size=16, color='#161616'),\n#                         showarrow=False))\n# annotations.append(dict(xref='paper', yref='paper',\n#                         x=.3, y=0.8,\n#                         text='Young adults',\n#                              font=dict(family='Arial', size=16, color='#161616'),\n#                         showarrow=False))\n# annotations.append(dict(xref='paper', yref='paper',\n#                         x=0.65, y=0.8,\n#                         text='Middle age',\n#                              font=dict(family='Arial', size=16, color='#161616'),\n#                         showarrow=False))\n# annotations.append(dict(xref='paper', yref='paper',\n#                         x=.93, y=0.8,\n#                         text='Senior',\n#                              font=dict(family='Arial', size=16, color='#161616'),\n#                         showarrow=False))\n \nfig.update_layout(title='<b>Passengers Age distn. by survival rate<b>',\n                  xaxis_title='Age [years]', \n                  yaxis_title='Probability Density [-]',\n                  titlefont={'size': 24},\n                  font_family = 'San Serif',\n                  width=950,height=500,\n                  template=\"simple_white\",\n                  showlegend=True,\n                  paper_bgcolor=\"white\",\n                  font=dict(\n                      color ='black',\n                      ),\n                  legend=dict(\n                      orientation=\"v\",\n                      y=1, \n                      yanchor=\"bottom\", \n                      x=1.0, \n                      xanchor=\"right\",)   \n )\n#fig.update_layout(annotations=annotations)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:04.077797Z","iopub.execute_input":"2022-08-12T08:59:04.078015Z","iopub.status.idle":"2022-08-12T08:59:04.182062Z","shell.execute_reply.started":"2022-08-12T08:59:04.077992Z","shell.execute_reply":"2022-08-12T08:59:04.181013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data to plot\nts=df_sales.groupby([\"date_block_num\"])[\"item_cnt_day\"].sum()\n\nfig = go.Figure()\nfig.add_trace(go.Scatter(x=ts.index, y=ts,\n                         name = 'Sales',\n                         line=dict(color='seagreen', width=5, dash='5px 10px 2px 2px')\n                         )\n             )\nfig.update_layout(title='<b>Total Sale of The Company<b>',\n                  xaxis_title='<b>Time<b>',\n                  yaxis_title='<b>Sales<b>',\n                  titlefont={'size': 28, 'family':'Serif'},\n                  template='simple_white',\n                  width=750, height=500,\n                )\nfig.add_vrect(\n    x0=10, x1=12,\n    fillcolor=\"orange\", opacity=0.5,\n    layer=\"below\", line_width=0,\n),\nfig.add_vrect(\n    x0=22, x1=24,\n    fillcolor=\"orange\", opacity=0.5,\n    layer=\"below\", line_width=0,\n),\n\nannotations = []\nannotations.append(dict(xref='paper', yref='paper',\n                        x=-0.075, y=1.1,\n                        text='<b> {Highlighting the peak sale period} <b>',\n                             font=dict(family='Arial', size=16, color='skyblue'),\n                        showarrow=False))\nfig.update_layout(annotations=annotations)\nfig.show()              ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:04.183281Z","iopub.execute_input":"2022-08-12T08:59:04.183536Z","iopub.status.idle":"2022-08-12T08:59:04.280739Z","shell.execute_reply.started":"2022-08-12T08:59:04.183510Z","shell.execute_reply":"2022-08-12T08:59:04.279996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\">Back to top</a>\n\n<a id=\"6\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>6. Subplots</b></font> \n\n<a id=\"6.1\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>6.1 Mixed plots/subplots</b></font>\n\nUsing the housing dataset I tried to demonstrate how you can customize a sub-plot. Plot area, chart type, annotation and more can be adjusted according to the need. Two examples below.","metadata":{}},{"cell_type":"code","source":"# data we'd like to plot\nimportant_features = ['LotArea', 'OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF', \n                      '1stFlrSF', 'FullBath', 'TotRmsAbvGrd', 'YearBuilt','YearRemodAdd', 'SalePrice']\nDF = df_house[important_features]\n\n\nfig = make_subplots(\n    rows=5, cols=2,\n    specs=[[{}, {\"rowspan\": 2}],\n           [{}, None],\n           [{\"rowspan\": 2, \"colspan\": 2}, None],\n           [None, None],\n           [{}, {}]],\n    #If you wish to give indivdual subtitle, you could do so by uncommenting  and updating the titles \n    #subplot_titles=(\"Subplot Title 1\", \"Subplot Title 2\", \"Subplot Title 3\", \"Subplot Title 4\", \"Subplot Title 5\", \"Subplot Title 6\"),\n    \n    #the format of the subplots can be visualized by printing the grid\n    y_title='Sale Price',\n    print_grid=True, \n)\n\n\n\nfig.add_trace(go.Bar(x=DF.groupby('TotRmsAbvGrd')['SalePrice'].mean().index,\n                     y=DF.groupby('TotRmsAbvGrd')['SalePrice'].mean(),\n                     name=\"Total rooms above gr.\",\n                     marker_color='#a1a1a1'\n                    ),\n              row=1, col=1\n             )\nfig.add_trace(go.Scatter(x=DF['LotArea'], y=DF['SalePrice'], mode='markers', name=\"Lot area\", marker_color='lightblue'), row=1, col=2)\nfig.add_trace(go.Bar(x=DF.groupby('OverallQual')['SalePrice'].mean().index, \n                     y=DF.groupby('OverallQual')['SalePrice'].mean(), name=\"Overall quality\", marker_color='lightsalmon'), row=2, col=1)\nfig.add_trace(go.Scatter(x=DF['GrLivArea'], y=DF['SalePrice'], mode='markers',name=\"Gr living area\", marker_color='lightseagreen'), row=3, col=1)\nfig.add_trace(go.Box(x=DF['GarageCars'],  y=DF['SalePrice'],marker_color='darkblue', name=\"Garage cars\", boxmean=True), row=5, col=1) \nfig.add_trace(go.Violin(x=DF['FullBath'],y=DF['SalePrice'], name=\"Full bath\",box_visible=False, line_color='black',meanline_visible=True, fillcolor='purple', opacity=0.6),row=5, col=2)\n    \n# Update xaxis properties\nfig.update_xaxes(title_text=\"TotRmsAbvGrd\",\n                 showgrid=False,\n                 row=1, col=1\n                )\nfig.update_xaxes(title_text=\"Lot area\", showgrid=False, row=1, col=2)\nfig.update_xaxes(title_text=\"Overall quality\", showgrid=False, row=2, col=1)\nfig.update_xaxes(title_text=\"Grd. living area\", showgrid=False, row=3, col=1)\nfig.update_xaxes(title_text=\"Garage cars\", showgrid=False, row=5, col=1)\nfig.update_xaxes(title_text=\"FullBath\", showgrid=False, row=5, col=2)\n\n# # Update yaxis properties\n# fig.update_yaxes(title_text=\"SalePrice\",\n#                  showgrid=False,\n#                  row=1, col=1\n#                 )\n# fig.update_yaxes(title_text=\"SalePrice\", showgrid=False, row=1, col=2)\n# fig.update_yaxes(title_text=\"SalePrice\", showgrid=False, row=2, col=1)\n# fig.update_yaxes(title_text=\"SalePrice\", showgrid=False, row=3, col=1)\n# fig.update_yaxes(title_text=\"SalePrice\", showgrid=False, row=5, col=1)\n# fig.update_yaxes(title_text=\"SalePrice\", showgrid=False, row=5, col=2)\n\n\n\nfig.update_layout(height=1050, width=750,\n                  showlegend=False,\n                  title_text=\"<b>House Prices: Features highly correlated with SalePrice<b>\",\n                  titlefont={'size': 28, 'family':'Serif'},\n                  paper_bgcolor=\"#ededed\",\n                  plot_bgcolor='#ededed',\n                 )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:04.282223Z","iopub.execute_input":"2022-08-12T08:59:04.282470Z","iopub.status.idle":"2022-08-12T08:59:04.431424Z","shell.execute_reply.started":"2022-08-12T08:59:04.282445Z","shell.execute_reply":"2022-08-12T08:59:04.430525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data we'd like to plot\nimportant_features = ['LotArea', 'OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF', \n                      '1stFlrSF', 'FullBath', 'TotRmsAbvGrd', 'YearBuilt','YearRemodAdd', 'SalePrice']\nDF = df_house[important_features]\n\nfrom sklearn.linear_model import LinearRegression\n\nfig = make_subplots(\n    rows=3, cols=3,\n    specs=[[{\"rowspan\": 2, \"colspan\": 3},None, None],\n           [None, None,None],\n           [{}, {}, {}]],\n           \n    #If you wish to give indivdual subtitle, you could do so by uncommenting  and updating the titles \n    #subplot_titles=(\"Subplot Title 1\", \"Subplot Title 2\", \"Subplot Title 3\", \"Subplot Title 4\", \"Subplot Title 5\", \"Subplot Title 6\"),\n    \n    #the format of the subplots can be visualized by printing the grid\n    y_title='Sale Price',\n    print_grid=False)\n\nfig.add_trace(go.Scatter(x=DF['GrLivArea'], y=DF['SalePrice'], mode='markers',name=\"Gr living area\", marker_color='lightseagreen'), row=1, col=1)\n\n## the following line is to add a regression (linear) fit line to othe above trace (GrLivArea, SalePrice)\nmodel = LinearRegression().fit(np.array(DF['GrLivArea']).reshape(-1,1), (np.array(DF['SalePrice'])))\ny_hat = model.predict(np.array(DF['GrLivArea']).reshape(-1,1))\nfig.add_trace(go.Scatter(x=DF['GrLivArea'], y=y_hat, mode='lines',name=\"Linear_reg_fit\", marker_color='salmon'), row=1, col=1)\n##\n\nfig.add_trace(go.Scatter(x=DF['YearBuilt'], y=DF['SalePrice'], mode='markers', name=\"Lot area\", marker_color='lightblue'), row=3, col=1)\nmodel = LinearRegression().fit(np.array(DF['YearBuilt']).reshape(-1,1), (np.array(DF['SalePrice'])))\ny_hat = model.predict(np.array(DF['YearBuilt']).reshape(-1,1))\nfig.add_trace(go.Scatter(x=DF['YearBuilt'], y=y_hat, mode='lines', name=\"Linear_reg_fit\", marker_color='salmon'), row=3, col=1)\n\nfig.add_trace(go.Bar(x=DF.groupby('OverallQual')['SalePrice'].mean().index, \n                     y=DF.groupby('OverallQual')['SalePrice'].mean(), name=\"Overall quality\", marker_color='lightsalmon'), row=3, col=2)\n\nfig.add_trace(go.Bar(x=DF.groupby('TotRmsAbvGrd')['SalePrice'].mean().index, y=DF.groupby('TotRmsAbvGrd')['SalePrice'].mean(),\n                     name=\"Total rooms above gr.\", marker_color='#a1a1a1'),row=3, col=3)\n# Update xaxis properties\nfig.update_xaxes(title_text=\"GrLivArea\", showgrid=False,row=1, col=1)\nfig.update_xaxes(title_text=\"YearBuilt\", showgrid=False, row=3, col=1)\nfig.update_xaxes(title_text=\"Overall quality\", showgrid=False, row=3, col=2)\nfig.update_xaxes(title_text=\"TotRmsAbvGrd\", showgrid=False, row=3, col=3)\n\n# Update yaxis properties\nfig.update_yaxes(showgrid=False,row=1, col=1)\nfig.update_yaxes(showgrid=False, row=1, col=2)\nfig.update_yaxes(showgrid=False, row=1, col=3)\nfig.update_yaxes(showgrid=False, row=2, col=1)\nfig.update_yaxes(showgrid=False, row=2, col=3)\nfig.update_yaxes(showgrid=False, row=3, col=3)\n\n\n\nfig.update_layout(height=750, width=750,\n                  showlegend=False,\n                  title_text=\"House Prices: Features highly correlated with SalePrice\",\n                  titlefont={'size': 28, 'family':'Serif'},\n                  template='plotly_dark',\n                  plot_bgcolor='#303030',\n                 )\nfig.update_layout()\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:04.432381Z","iopub.execute_input":"2022-08-12T08:59:04.432614Z","iopub.status.idle":"2022-08-12T08:59:04.691627Z","shell.execute_reply.started":"2022-08-12T08:59:04.432583Z","shell.execute_reply":"2022-08-12T08:59:04.690490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"6.2\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>6.2 Facet grids/pairplots</b></font>\n\nYou can plot pairplot using the standard plolty function `figure_factory.create_scatterplotmatrix()`. The diagonal plot type can be chosen from *histogram*, *scatter* or *box* types. Background and paper colors can also be customized. ","metadata":{}},{"cell_type":"code","source":"scores= ['math score', 'writing score', 'reading score']\ndf_scores =df_study[scores]\n\nfig = ff.create_scatterplotmatrix(df_scores, diag='box', colormap='Viridis',\n                                  colormap_type='cat',\n                                  height=700, width=700,\n                                 )\nfig.update_layout(title=\"<b>Pair Plots: Student's Test Score<b>\",\n                  titlefont={'size': 28, 'family':'Courier New'},\n                  template='simple_white',\n                  paper_bgcolor='lightgray',\n                  plot_bgcolor='lightgray')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:04.692724Z","iopub.execute_input":"2022-08-12T08:59:04.692949Z","iopub.status.idle":"2022-08-12T08:59:04.951329Z","shell.execute_reply.started":"2022-08-12T08:59:04.692924Z","shell.execute_reply":"2022-08-12T08:59:04.950637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Another option is to use the `go.Splom` from `plotly.graph_objects`. See the example below which uses the Iris dataset.","metadata":{}},{"cell_type":"code","source":"index_vals = df_iris['Species'].astype('category').cat.codes\n\nfig = go.Figure(data=go.Splom(\n                dimensions=[dict(label='sepal length',\n                                 values=df_iris['SepalLengthCm']),\n                            dict(label='sepal width',\n                                 values=df_iris['SepalWidthCm']),\n                            dict(label='petal length',\n                                 values=df_iris['PetalLengthCm']),\n                            dict(label='petal width',\n                                 values=df_iris['PetalWidthCm'])],\n                showupperhalf=False,\n                diagonal=dict(visible=False),\n                text=df_iris['Species'],\n                marker=dict(color=index_vals,\n                            showscale=True, \n                            line_color='white', \n                            line_width=0.5\n                           )\n))\n\nfig.update_layout(title=\"<b>Pair plot: Iris Data set<b>\",\n                  titlefont={'size': 28, 'family':'Courier New'},\n                  template='simple_white',\n                  paper_bgcolor='lightgray',\n                  plot_bgcolor='lightgray',\n                  width=700,\n                  height=700,)\n\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:04.952319Z","iopub.execute_input":"2022-08-12T08:59:04.952712Z","iopub.status.idle":"2022-08-12T08:59:05.019385Z","shell.execute_reply.started":"2022-08-12T08:59:04.952680Z","shell.execute_reply":"2022-08-12T08:59:05.018403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"6.3\"></a>\n<font color=\"lightseagreen\" size=+1.5><b>6.3 Daigonal Facet grids (custom made)</b></font>\n\nWe can also make a new pairplot with lower-triangular with a kde diagonal plot. This plot was inspiried by [subinium's](https://www.kaggle.com/subinium)  [notebook](https://www.kaggle.com/subinium/dark-mode-visualization-apple-version) which he did using matplotlib/seaborn library.","metadata":{}},{"cell_type":"code","source":"fig = make_subplots(\n    rows=3, cols=3,\n    specs=[[{},None, None],\n           [{}, {},None],\n           [{}, {}, {}]],\n    print_grid=False)\n\nfig.add_trace(go.Violin(x=df_study['math score'], line_color='lightseagreen', name='math_math', y0=0,\n                       orientation='h', side='positive', meanline_visible=False), row=1, col=1)\n\n\nfig.add_trace(go.Scatter(x=df_study['math score'], y=df_study['reading score'], mode='markers', name=\"math_reading\", marker_color='#b0fbb0'), row=2, col=1)\nfig.add_trace(go.Violin(x=df_study['reading score'], line_color='lightsalmon', name='reading_reading', y0=0,\n                       orientation='h', side='positive', meanline_visible=False), row=2, col=2)\nfig.add_trace(go.Scatter(x=df_study['math score'], y=df_study['writing score'], mode='markers', name=\"math_writing\", marker_color='#f86767'), row=3, col=1)\nfig.add_trace(go.Scatter(x=df_study['reading score'], y=df_study['writing score'], mode='markers', name=\"reading_writing\", marker_color='#fac998'), row=3, col=2)\nfig.add_trace(go.Violin(x=df_study['reading score'], line_color='lightgray', name='reading_reading', y0=0,\n                       orientation='h', side='positive', meanline_visible=False), row=3, col=3)\n\n# Update xaxis properties\n\nfig.update_xaxes(title_text=\"math\", showgrid=False, row=3, col=1)\nfig.update_xaxes(title_text=\"reading\", showgrid=False, row=3, col=2)\nfig.update_xaxes(title_text=\"writing\", showgrid=False, row=3, col=3)\n\n# Update yaxis properties\nfig.update_yaxes(title_text=\"math\",showgrid=False,row=1, col=1)\nfig.update_yaxes(title_text=\"reading\",showgrid=False, row=2, col=1)\nfig.update_yaxes(title_text=\"writing\",showgrid=False, row=3, col=1)\n\nfig.update_layout(height=750, width=750,\n                  showlegend=False,\n                  title_text=\"<b>Student's Test Score<b>\",\n                  titlefont={'size': 28, 'family':'Courier New'},\n                  template='plotly_dark',\n                  #paper_bgcolor=\"#303330\",\n                  plot_bgcolor='#303330',\n                 )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.023546Z","iopub.execute_input":"2022-08-12T08:59:05.023815Z","iopub.status.idle":"2022-08-12T08:59:05.151657Z","shell.execute_reply.started":"2022-08-12T08:59:05.023789Z","shell.execute_reply":"2022-08-12T08:59:05.150854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"#toc\">Back to top</a>\n\n<a id=\"7\"></a>\n<font color=\"lightseagreen\" size=+3.5><b>7. Radar Charts</b></font>\n\nUse `radar chart` when appropriate. When plotting some feature which varies with direction, it can best be represented (plotted) using a radar chart. For example, the effect of wind direction on rainfall (Rainfall in Austrailia dataset) can be a good example. Below I have depicted how this can be demonstrated with line graph and radar chart. In my opinion the radar chart looks/feels right.","metadata":{}},{"cell_type":"code","source":"WINdIR = ['E','ENE', 'NE', 'NNE', 'N', 'NNW', 'NW', 'WNW', 'W', 'WSW', 'SW', 'SSW', 'S', 'SSE', 'SE', 'ESE']","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.152776Z","iopub.execute_input":"2022-08-12T08:59:05.153007Z","iopub.status.idle":"2022-08-12T08:59:05.156539Z","shell.execute_reply.started":"2022-08-12T08:59:05.152982Z","shell.execute_reply":"2022-08-12T08:59:05.155773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure()\n\nfig.add_trace(go.Scatter(y=df_rain.groupby('WindDir9am')['Rainfall'].mean(),\n                         x=WINdIR,\n                         name = 'Wind dir. 3pm',\n                         line_width=5.0,\n                         )\n             )\nfig.add_trace(go.Scatter(y=df_rain.groupby('WindDir3pm')['Rainfall'].mean(),\n                         x=WINdIR,\n                         name = 'Wind dir. 9am',\n                         line_width=5.0,\n                        )\n             )\nfig.update_layout(title='Rainfall vs Wind Direction',\n                  xaxis_title='Wind Direction',\n                  yaxis_title='Rainfall [mm]',\n                  titlefont={'size': 28, 'family':'Serif'},\n                  showlegend=True,\n                  paper_bgcolor='lightgray',\n                  plot_bgcolor='#999999',\n                  width=750, height=500,\n                 )\nfig.show()              ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.157461Z","iopub.execute_input":"2022-08-12T08:59:05.157734Z","iopub.status.idle":"2022-08-12T08:59:05.195914Z","shell.execute_reply.started":"2022-08-12T08:59:05.157710Z","shell.execute_reply":"2022-08-12T08:59:05.195371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure()\n\nfig.add_trace(go.Scatterpolar(r=df_rain.groupby('WindDir9am')['Rainfall'].mean(),\n                          theta=WINdIR,\n                          name='Wind Dir. 3pm',\n                          fill='toself',\n                          )\n             )\nfig.add_trace(go.Scatterpolar(r=df_rain.groupby('WindDir3pm')['Rainfall'].mean(),\n                          theta=WINdIR,\n                          name='Wind Dir. 9am',\n                          fill='toself',\n                          )\n             )\nfig.update_layout(polar=dict(radialaxis=dict(\n    visible=False,\n        )\n),\n                  showlegend=True,\n                  title='Rainfall vs Wind Direction',\n                  titlefont={'size': 28, 'family':'Serif'},\n                  template='plotly_dark',\n                  paper_bgcolor='lightgray',\n                  width=600, height=500,\n                 )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.196818Z","iopub.execute_input":"2022-08-12T08:59:05.197144Z","iopub.status.idle":"2022-08-12T08:59:05.254301Z","shell.execute_reply.started":"2022-08-12T08:59:05.197119Z","shell.execute_reply":"2022-08-12T08:59:05.253586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure()\n\nfig.add_trace(go.Barpolar(r=df_rain.groupby('WindDir9am')['Rainfall'].mean(),\n                          theta=WINdIR,\n                          name='Wind Dir. 3pm'\n                          )\n             )\nfig.add_trace(go.Barpolar(r=df_rain.groupby('WindDir3pm')['Rainfall'].mean(),\n                          theta=WINdIR,\n                          name='Wind Dir. 9am'\n                          )\n             )\nfig.update_layout(polar=dict(radialaxis=dict(visible=False,\n                                             )\n                             ),\n                  showlegend=True,\n                  title='Rainfall vs Wind Direction',\n                  titlefont={'size': 28, 'family':'Serif'},\n                  template='plotly_dark',\n                  paper_bgcolor='gray',\n                  width=600, height=500,\n                 )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.255247Z","iopub.execute_input":"2022-08-12T08:59:05.255469Z","iopub.status.idle":"2022-08-12T08:59:05.313954Z","shell.execute_reply.started":"2022-08-12T08:59:05.255445Z","shell.execute_reply":"2022-08-12T08:59:05.312971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"8\"></a>\n\n<font color=\"lightseagreen\" size=+3.5><b>8. 3D Plots</b></font>\n\nIn 3D plots it helps to use different background colors for the three planes to make the visiblity and visualization a bit more clear. With the help of the legendary iris dataset let's do that. ","metadata":{}},{"cell_type":"code","source":"fig = px.scatter_3d(df_iris, x='SepalLengthCm', y='SepalWidthCm', z='PetalWidthCm',\n                    color='Species')\nfig.update_layout(title='<b>3D: The Legendary Iris Dataset <b>',\n                  titlefont=dict({'size':28, 'family': 'Courier New'}),\n                  template='plotly',\n                  paper_bgcolor='lightgray',\n                  width=750, height=550,\n                 )\nfig.update_layout(scene = dict(\n                    xaxis = dict(\n                         backgroundcolor=\"rgb(200, 200, 230)\",\n                         gridcolor=\"white\",\n                         showbackground=True,\n                         zerolinecolor=\"black\",),\n                    yaxis = dict(\n                        backgroundcolor=\"rgb(230, 200,230)\",\n                        gridcolor=\"white\",\n                        showbackground=True,\n                        zerolinecolor=\"black\"),\n                    zaxis = dict(\n                        backgroundcolor=\"rgb(230, 230,200)\",\n                        gridcolor=\"white\",\n                        showbackground=True,\n                        zerolinecolor=\"black\"),\n                              ),\n                  )\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.315062Z","iopub.execute_input":"2022-08-12T08:59:05.315269Z","iopub.status.idle":"2022-08-12T08:59:05.420678Z","shell.execute_reply.started":"2022-08-12T08:59:05.315247Z","shell.execute_reply":"2022-08-12T08:59:05.419689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"9\"></a>\n\n<font color=\"lightseagreen\" size=+3.5><b>9. Choropleth Map (Animation)</b></font>\n\n* Use the `scope` argument if you want to focus on one continent only. \n* Africa is selected as the scope argument in the second example. ","metadata":{}},{"cell_type":"code","source":"df = px.data.gapminder()\n\nfig = px.choropleth(df, locations=\"iso_alpha\",\n                    animation_frame=\"year\",\n                    color=\"lifeExp\", \n                    hover_name=\"country\", \n                    color_continuous_scale=px.colors.sequential.Plasma)\n\n                 \nfig.update_layout(title='<b>Global Life Expectancy (1952-2007) <b>',\n                  font_family=\"San Serif\",\n                  width=800,\n                  height=600,\n                  titlefont={'size': 28},\n                  paper_bgcolor='#E6E6E6',\n                  plot_bgcolor='#E6E6E6',                  \n                  )\n\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.422096Z","iopub.execute_input":"2022-08-12T08:59:05.422439Z","iopub.status.idle":"2022-08-12T08:59:05.587117Z","shell.execute_reply.started":"2022-08-12T08:59:05.422404Z","shell.execute_reply":"2022-08-12T08:59:05.586392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = px.data.gapminder()\n\nfig = px.choropleth(df, locations=\"iso_alpha\",\n                    animation_frame=\"year\",\n                    scope='africa',\n                    color=\"lifeExp\", \n                    hover_name=\"country\", \n                    color_continuous_scale=px.colors.sequential.Plasma)\n\n                 \nfig.update_layout(title='<b>Life Expectancy in Africa (1952-2007) <b>',\n                  font_family=\"San Serif\",\n                  width=800,\n                  height=600,\n                  titlefont={'size': 28},\n                  paper_bgcolor='#E6E6E6',\n                  plot_bgcolor='#E6E6E6',                  \n                  )\n\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.588134Z","iopub.execute_input":"2022-08-12T08:59:05.588372Z","iopub.status.idle":"2022-08-12T08:59:05.692679Z","shell.execute_reply.started":"2022-08-12T08:59:05.588347Z","shell.execute_reply":"2022-08-12T08:59:05.691617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"10\"></a>\n\n<font color=\"lightseagreen\" size=+3.5><b>10. Bonus: Getting creative with annotated heatmaps</b></font>\n\nIf you haven't seen the periodic table plotted using plotly, you can find it [here](https://plotly.com/python/annotated-heatmap/). Go ahead and check it. It gives you an idea on how to get creative with annotated heatmaps. [@janiobachmann](https://www.kaggle.com/janiobachmann) also used the periodic table structure in his [housing market project](https://www.kaggle.com/janiobachmann/melbourne-comprehensive-housing-market-analysis). Below is a simplified example.\n\nNote: You might have already noticed that the header of this notebook is also an adaptation of the periodic table plot.","metadata":{}},{"cell_type":"code","source":"# Add text data\ntext0 = [['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'C', 'O', 'V', 'I', 'D', '- 1', '9', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'J', '&', 'J', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'V', 'A', 'C', 'C', 'I', 'N', 'E', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',],\n          ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',]]\n          \ntext1 = text2 = text0\n\nz = [[.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .2, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .1, .1, .0, .0, .2, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .1, .1, .0, .0, .2, .2, .3, .3, .3, .3, .3, .3, .3, .3, .3, .3, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .1, .1, .1, .1, .2, .2, .3, .3, .3, .8, .8, .8, .8, .8, .8, .3, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .1, .1, .1, .1, .2, .2, .3, .3, .3, .8, .8, .8, .8, .8, .8, .3, .9, .9, .9, .9, .9, .9, .9, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .1, .1, .1, .1, .2, .2, .3, .3, .3, .8, .8, .8, .8, .8, .8, .3, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .1, .1, .0, .0, .2, .2, .3, .3, .3, .3, .3, .3, .3, .3, .3, .3, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .1, .1, .0, .0, .2, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .2, .2, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .25, .25, .25, .25, .25, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .66, .66, .66, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .5, .5, .5, .5, .5, .5, .5, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .55, .55, .55, .55, .55, .55, .55, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .5, .5, .5, .5, .5, .5, .5, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .5, .5, .5, .5, .5, .5, .5, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .55, .55, .55, .55, .55, .55, .55, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .5, .5, .5, .5, .5, .5, .5, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .5, .5, .5, .5, .5, .5, .5, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     [.0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0, .0,],\n     ]  \n        \n# Display text1 and text2 on hover\nhover=[]\nfor x in range(len(text0)):\n    hover.append([i + '<br>' + '' + str(j)\n                      for i, j in zip(text1[x], text2[x])])\n\n# Invert Matrices\ntext0 = text0[::-1]\nhover = hover[::-1]\nz = z[::-1]\n\n# Set Colorscale\ncolorscale=[[0.0, 'white'], [.2, 'salmon'],\n            [.5, '#005876'], [.6, 'lightseagreen'],\n            [.8, 'seagreen'],[1.0, 'yellow']]\n\n\n# Make Annotated Heatmap\nfig = ff.create_annotated_heatmap(z, \n                                  annotation_text=text0, \n                                  text=hover,\n                                  colorscale=colorscale, font_colors=['black'], hoverinfo='text')\nfig.update_layout(title='<b>Vaccine leads to herd immunity and eventually to normalcy!<b>',\n                  titlefont={'size': 24},\n                  width=750,\n                  height=500,)\nfig.show()\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-12T08:59:05.694009Z","iopub.execute_input":"2022-08-12T08:59:05.694292Z","iopub.status.idle":"2022-08-12T08:59:06.177942Z","shell.execute_reply.started":"2022-08-12T08:59:05.694262Z","shell.execute_reply":"2022-08-12T08:59:06.176978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"11\"></a>\n\n<font color=\"lightseagreen\" size=+3.5><b>11. Reference</b></font>\n\n1. [plotly.com](https://plotly.com/) \n2. [Plotly Tutorial for Everyone](https://www.kaggle.com/saurav9786/plotly-tutorial-for-everyone/) by [@saurav9786](https://www.kaggle.com/saurav9786) \n3. [Plotly Tutorial for Beginners](https://www.kaggle.com/kanncaa1/plotly-tutorial-for-beginners) by [@kanncaa1](https://www.kaggle.com/kanncaa1)\n\n**Note**: Ref 2&3 are very good tutorials for beginners on plotly. I recommend them for anyone who's just starting exploring the plotly plotting library.\n\nOther notebooks where I used plotly:\n\n1. [Students Performance: Practice EDA with plotly](https://www.kaggle.com/desalegngeb/students-performance-practice-eda-with-plotly)\n2. [April TPS: Synthanic EDA + Visualizations](https://www.kaggle.com/desalegngeb/april-tps-synthanic-eda-visualizations)\n3. [English PL Players' stat: Data-viz with Plotly](https://www.kaggle.com/desalegngeb/english-pl-players-stat-data-viz-with-plotly)\n","metadata":{}},{"cell_type":"markdown","source":"---\n---\n\n### End of notebook!\n\n\n#### Thank you for reading! \n---\n---\n\n<a href=\"#toc\">Back to top</a>","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}