{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyPCnDQgvE0eG/4j3EbbcH4R"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Plotly Express\n","\n","The `plotly.express` module contains functions that can create entire figures at once. `plotly.express` functions are designed to be a user-friendly point of entry to the `plotly` package. A graph object is created as part of any `plotly.express` function, but the point of the `plotly.express` function is to significantly reduce the amount of code needed to create, customize, and render the graph object."],"metadata":{"id":"yB7l4gpBZNkz"}},{"cell_type":"markdown","source":["## Scatterplot Workflows\n","\n","For example, to create a scatterplot using `plotly.express`, we use the `px.scatter()` function, which takes values for the `X` and `Y` axis.\n","\n","Without any additional arguments, the default formatting and style options are applied."],"metadata":{"id":"Y7iv9NGvZJGq"}},{"cell_type":"code","source":["import plotly.express as px # import statement\n","fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16]) # create figure\n","fig # show figure"],"metadata":{"id":"j2khLMawZh5l"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["Everything from tick marks to axis labels to grid lines to hover labels has been generated by the `plotly.express` defaults. To create a scatterplot from data stored in a `DataFrame`, the same general syntax specifying `X` and `Y` values still applies.\n","\n","Let's look at a `DataFrame` example."],"metadata":{"id":"SXTEkZSpZoDP"}},{"cell_type":"code","source":["df = px.data.iris() # load data\n","df # inspect data"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":444},"id":"3UEsyFFkZnn-","executionInfo":{"status":"ok","timestamp":1706148997882,"user_tz":300,"elapsed":384,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"fcc707cc-d66a-479a-d054-308d7bab0ac2"},"execution_count":4,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" sepal_length sepal_width petal_length petal_width species \\\n","0 5.1 3.5 1.4 0.2 setosa \n","1 4.9 3.0 1.4 0.2 setosa \n","2 4.7 3.2 1.3 0.2 setosa \n","3 4.6 3.1 1.5 0.2 setosa \n","4 5.0 3.6 1.4 0.2 setosa \n",".. ... ... ... ... ... \n","145 6.7 3.0 5.2 2.3 virginica \n","146 6.3 2.5 5.0 1.9 virginica \n","147 6.5 3.0 5.2 2.0 virginica \n","148 6.2 3.4 5.4 2.3 virginica \n","149 5.9 3.0 5.1 1.8 virginica \n","\n"," species_id \n","0 1 \n","1 1 \n","2 1 \n","3 1 \n","4 1 \n",".. ... \n","145 3 \n","146 3 \n","147 3 \n","148 3 \n","149 3 \n","\n","[150 rows x 6 columns]"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
sepal_lengthsepal_widthpetal_lengthpetal_widthspeciesspecies_id
05.13.51.40.2setosa1
14.93.01.40.2setosa1
24.73.21.30.2setosa1
34.63.11.50.2setosa1
45.03.61.40.2setosa1
.....................
1456.73.05.22.3virginica3
1466.32.55.01.9virginica3
1476.53.05.22.0virginica3
1486.23.45.42.3virginica3
1495.93.05.11.8virginica3
\n","

150 rows × 6 columns

\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","
\n","
\n"]},"metadata":{},"execution_count":4}]},{"cell_type":"code","source":["fig = px.scatter(df, x=\"sepal_width\", y=\"sepal_length\") # create figure\n","fig # show output"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"XBnYF3jZZ5Nv","executionInfo":{"status":"ok","timestamp":1706149000411,"user_tz":300,"elapsed":269,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"352554aa-5a49-4cd9-f36e-83d1cc617a3c"},"execution_count":5,"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}]},{"cell_type":"markdown","source":["In the `dataframe` example, we passed the entire data frame to `px.scatter()`, and specify columns to use for the `X` and `Y` axis. In the resulting figure, we can see how `plotly` assigns the column names as axis labels."],"metadata":{"id":"fW_p9h2HZ9Ql"}},{"cell_type":"markdown","source":["## Customizing\n","\n","We can modify point color and size to reflect underlying values in the data frame (using `color` and `size`). We can also modify the information displayed in the hover label (using `hover_data`)."],"metadata":{"id":"mbE7wIoSac2u"}},{"cell_type":"code","source":["fig = px.scatter(df, x=\"sepal_width\", y=\"sepal_length\", color=\"species\", size=\"petal_length\", hover_data=[\"petal_width\"]) # create figure\n","fig # show figure"],"metadata":{"id":"AdilC8Mrae8W"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["In this modified example, `color` specifies what field to use to color points, `size` specifies what field to use to size points, and `hover_data` adds fields not already incorporated in the figure to the hover label, which now includes 5 different fields."],"metadata":{"id":"_NAku-znajU0"}},{"cell_type":"markdown","source":["### Additional Scatterplot Resources\n","\n","- [`plotly`, Scatter Plots in Python](https://plotly.com/python/line-and-scatter/)\n","- [`plotly.express.scatter`](https://plotly.com/python-api-reference/generated/plotly.express.scatter)\n","- [`plotly`, Python Figure Reference: `scatter` Traces](https://plotly.com/python/reference/scatter/)\n","- [`plotly`, Python Figure Reference: `scattergl` Traces](https://plotly.com/python/reference/scattergl/)"],"metadata":{"id":"qdrHW2nfakyX"}}]}