Exploring Geographical Data with scattergeo() in Python's Plotly Library¶

Introduction:¶

In the realm of data visualization, one of the most captivating ways to unearth insights is through the art of mapping. Maps provide an intuitive means to grasp spatial relationships, trends, and patterns within data. Python, a versatile programming language, offers a powerful tool called scattergeo() within the Plotly library, which enables the creation of interactive scatter plots on geographical maps. In this blog, we will dive into the world of scattergeo() and explore how it can transform raw data into compelling visual narratives.

Understanding scattergeo():¶

The scattergeo() function is a part of Plotly, a popular Python graphing library that excels in creating interactive and aesthetically pleasing visualizations. This function specializes in generating scatter plots on geographical maps, an invaluable technique for analyzing datasets that involve location-based attributes. By plotting data points with latitude and longitude coordinates, scattergeo() makes it possible to visualize spatial relationships and gain new perspectives on your data.

Getting Started:¶

To begin using scattergeo(), you need to have the Plotly library installed. If you haven't done so yet, you can easily install it using the following command:

pip install plotly

Once you have Plotly installed, you can import the necessary modules and start creating your geographical scatter plots.

Creating Geographical Scatter Plots:¶

Creating a basic geographical scatter plot using scattergeo() is remarkably straightforward. Here's a simple example:

Importing Libraries¶

In [5]:
import plotly.express as px
import pandas as pd
import IPython.display as display

Importing Data¶

In [2]:
data = pd.read_csv('C:/Users/SANKHYA/Downloads/data.csv')
data.head()
Out[2]:
time latitude longitude depth mag magType nst gap dmin rms ... updated place type horizontalError depthError magError magNst status locationSource magSource
0 2023-08-16T04:59:13.247Z 36.699100 -116.326700 7.800 -0.10 ml 9.0 174.97 0.028000 0.0608 ... 2023-08-16T05:01:14.555Z 45 km ESE of Beatty, Nevada earthquake NaN 1.100 0.450 6.0 automatic nn nn
1 2023-08-16T04:44:58.140Z 38.822834 -122.809166 0.710 0.76 md 9.0 78.00 0.000904 0.0200 ... 2023-08-16T05:02:11.983Z 7 km NW of The Geysers, CA earthquake 0.35 0.760 0.310 9.0 automatic nc nc
2 2023-08-16T04:44:55.925Z 37.344000 -117.630500 3.500 0.50 ml 10.0 185.81 0.273000 0.2119 ... 2023-08-16T04:47:24.357Z 45 km S of Silver Peak, Nevada earthquake NaN 6.400 1.060 6.0 automatic nn nn
3 2023-08-16T04:28:28.346Z 59.135000 -152.743600 88.400 1.60 ml NaN NaN NaN 0.4300 ... 2023-08-16T04:30:18.729Z 53 km WSW of Nanwalek, Alaska earthquake NaN 0.500 NaN NaN automatic ak ak
4 2023-08-16T04:09:05.462Z -12.592100 167.044900 224.357 5.20 mb 68.0 97.00 7.640000 0.5000 ... 2023-08-16T04:28:24.040Z Santa Cruz Islands earthquake 10.21 7.026 0.027 461.0 reviewed us us

5 rows × 22 columns

In [ ]:
# Creating scatter map
fig = px.scatter_geo(data, lat='latitude', lon='longitude', color='mag',
                     hover_name='place', #size='mag',
                     title='Earthquakes Around the World')
fig.show()
In [8]:
images = [
    "C:/Users/SANKHYA/Downloads/Capture.png",
    "C:/Users/SANKHYA/Downloads/Capture1.png",
    ]

def display_images(images):
  """Displays a group of images side-by-side."""
  for i in range(0, len(images), 2):
    row = images[i:i+2]
    display.display(display.Image(row[0], width=800, height=600))
    display.display(display.Image(row[1], width=800, height=600))

display_images(images)

Note: Although we've used static images, hovering remains fully functional. When hovering, you can retrieve details like centroid, state name, longitude, latitude, and magnitude.

In this example, we import the plotly.express module as px, define data points with longitude and latitude coordinates, and specify the text associated with each point. We then create a layout, define the geographical scope, and generate the plot using fig.show().

Features of Scattergeo:¶

Geographical Mapping, Customization Options, Map Projections, Hover Interaction, Enhancing Interactivity and Export, Application Areas.

Conclusion:¶

The scattergeo() function in Python's Plotly library opens a gateway to exploring data in a spatial context. By transforming raw datasets into interactive geographical scatter plots, it empowers data analysts and researchers to uncover hidden trends, patterns, and insights. From business analytics to scientific research, scattergeo() demonstrates the power of visualization in understanding complex geographical data. So why not embark on your journey of visual discovery with scattergeo() and bring your data to life on the global map?

Remember, every data point tells a story, and with scattergeo(), you have the canvas to paint those stories across the world.