{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"toc_visible":true,"authorship_tag":"ABX9TyNTp0CWTdkPMYEwTbeAv1At"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# `px.scatter_mapbox()`\n","\n","

\n","\n","Now we can use the `px.scatter_mapbox()` function to add point data to our figure. This example takes the `open-street-map` base map from the previous section and adds point data for 1,000 US cities with the highest population count."],"metadata":{"id":"r_DLzDtbfEe7"}},{"cell_type":"code","source":["import pandas as pd, plotly.express as px # import statement\n","us_cities = pd.read_csv(\"https://raw.githubusercontent.com/kwaldenphd/elements-of-computing/book/data/ch12/us-cities-top-1k.csv\") # load data\n","\n","# create figure\n","fig = px.scatter_mapbox(us_cities, lat=\"lat\", lon=\"lon\", hover_name=\"City\", hover_data=[\"State\", \"Population\"],\n"," color_discrete_sequence=[\"maroon\"], zoom=3, height=300)\n","\n","fig.update_layout(mapbox_style=\"open-street-map\") # update basemap\n","fig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0}) # update layout\n","fig # show figure"],"metadata":{"id":"lIK2uh15fqxS"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["We specify which fields in the `us_cities` dataframe include latitude and longitude information (`lat` and `lon`). We set a color sequence with discrete colors. But since no field is assigned for a color attribute, all points are the same color."],"metadata":{"id":"nlpj8Qvog2rj"}},{"cell_type":"markdown","source":["## Customizing\n","\n","If we wanted to color the points based on population size, we would want to switch to a continuous color scale."],"metadata":{"id":"ONm4qBk2g5wJ"}},{"cell_type":"code","source":["# color points based on population value\n","fig = px.scatter_mapbox(us_cities, lat=\"lat\", lon=\"lon\", hover_name=\"City\", hover_data=[\"State\", \"Population\"],\n"," color_continuous_scale=px.colors.sequential.Viridis, mapbox_style=\"open-street-map\",\n"," color=\"Population\", zoom=3, height=300)\n","\n","fig # show figure"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":317},"id":"ec9qx72dg8CE","executionInfo":{"status":"ok","timestamp":1706167705688,"user_tz":300,"elapsed":227,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"e83fce5d-ca6f-4534-b9c5-e27934841bc3"},"execution_count":null,"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}]},{"cell_type":"markdown","source":["If we wanted our point size to be based on the population value, we would modify the `size` parameter."],"metadata":{"id":"lPjpbYvnhG7i"}},{"cell_type":"code","source":["# size points based on population value\n","fig = px.scatter_mapbox(us_cities, lat=\"lat\", lon=\"lon\", hover_name=\"City\", hover_data=[\"State\", \"Population\"],\n"," size='Population', mapbox_style=\"open-street-map\",\n"," color=\"Population\", zoom=3, height=300)\n","\n","fig # show figure"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":317},"id":"jQ9lCN2KhJRE","executionInfo":{"status":"ok","timestamp":1706167736076,"user_tz":300,"elapsed":241,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"09a6fe39-f472-4c6a-f96c-4b086744ce83"},"execution_count":null,"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}]},{"cell_type":"markdown","source":["Another example that uses rideshare data for Montreal. In this example, point size is based on the number of car hours (`car_hours`) and point color is based on time of day (`peak_hour`)."],"metadata":{"id":"qP-WN_27hcEl"}},{"cell_type":"code","source":["# load data\n","df = px.data.carshare()\n","\n","# create figure\n","fig = px.scatter_mapbox(df, lat=\"centroid_lat\", lon=\"centroid_lon\", mapbox_style=\"carto-positron\",\n"," color=\"peak_hour\", size=\"car_hours\", color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10)\n","\n","# show figure\n","fig.show()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"of-COUZ0hdta","executionInfo":{"status":"ok","timestamp":1706167794757,"user_tz":300,"elapsed":219,"user":{"displayName":"Katherine Walden","userId":"17094108395123900917"}},"outputId":"428920ad-01a3-42f8-c782-adc486f7ab87"},"execution_count":null,"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}]},{"cell_type":"markdown","source":["We pass the entire dataframe to `px.scatter_mapbox` and specify which fields include geospatial information. We set `color` and `size` and assign a maximum size for the points."],"metadata":{"id":"rgUfm3xyhpvZ"}},{"cell_type":"markdown","source":["### Color Maps\n","\n","`plotly` includes a number of built-in discrete and continuous colormaps. To learn more about the built-in colormap options:\n","- [`plotly`, Continuous Color Scales and Color Bars in Python](https://plotly.com/python/colorscales/#color-scales-in-plotly-express)\n","- [`plotly`, Built-In Continuous Color Scales in Python](https://plotly.com/python/builtin-colorscales/#using-builtin-continuous-color-scales)\n","- [`plotly`, Discrete Colors in Python](https://plotly.com/python/discrete-color/)"],"metadata":{"id":"LFDWTgzehp9J"}}]}