Edit Map in px.scatter_geo Plot (add GA county lines)

46 views Asked by At

This code I made does exactly what I wanted; I would just like to add in county lines. I didn't know if I needed to edit the map or replace it altogether.

Below is the code; I have other detailed geojson and shapefiles with county lines, but I'm not sure how to add them in and I know the shapefile works differently.

SN: I also don't need the latitude and longitude displayed in the hover annotations, but I couldn't remove them and have the code work.

import pandas as pd
import numpy as np
!pip install chart_studio
!pip install plotly.express
!pip install pandas
import plotly.express as px
!pip install get_path
import geopandas
import matplotlib.pyplot as plt
import chart_studio
import chart_studio.plotly as py
import plotly.graph_objects as go

# Import credentials with chart_studio
chart_studio.tools.set_credentials_file(username='*********', 
api_key='*************')
chart_studio.tools.set_config_file(world_readable=True, 
sharing='public')


# Read the first dataset
Counties = pd.read_excel('/content/uscounties.xlsx')

# Extract Georgia's data from the dataset
data1 = Counties[Counties['state_name'] == 'Georgia']

# Read the second dataset
Permits = pd.read_excel('/content/housing_23.xlsx')

# Drop unnecessary columns from Permits
permits1 = Permits.drop(columns=['2019 New Privately-Owned 
Residential Construction, Structures',
                   '2020 New Privately-Owned Residential 
                    Construction, Structures',
                   '2021 New Privately-Owned Residential 
                    Construction, Structures',
                   '2022 County Completely Covered by Building Permit 
                    Systems',
                   '2022 Number of Places Issuing Building Permits',
                   '2022 New Privately-Owned Residential 
                    Construction, Structures'])


# Rename columns in Permits
permits = permits1.rename(columns={'2019 Valuation of Construction, 
                                Dollars in Thousands':'2019 Total 
                                Valuation',
                               '2020 Valuation of Construction, 
                                Dollars in Thousands':'2020 Total 
                                Valuation',
                               '2021 Valuation of Construction, 
                                Dollars in Thousands':'2021 Total 
                                Valuation',
                               '2022 Valuation of Construction, 
                                Dollars in Thousands':'2022 Total 
                                Valuation',
                               '2019 New Privately-Owned Residential 
                                Construction, Units':'2019 New 
                                Residential Units',
                               '2020 New Privately-Owned Residential 
                                Construction, Units':'2020 New 
                                Residential Units',
                               '2021 New Privately-Owned Residential 
                                Construction, Units':'2021 New 
                                Residential Units',
                               '2022 New Privately-Owned Residential 
                                Construction, Units':'2022 New 
                                Residential Units',
                               'County':'county'})

# Convert value to currency
permits['2019 Total Valuation'] = ['${:,.2f}M'.format(x) for x in 
permits['2019 Total Valuation']/1000000]
permits['2020 Total Valuation'] = ['${:,.2f}M'.format(x) for x in 
permits['2020 Total Valuation']/1000000]
permits['2021 Total Valuation'] = ['${:,.2f}M'.format(x) for x in 
permits['2021 Total Valuation']/1000000]
permits['2022 Total Valuation'] = ['${:,.2f}M'.format(x) for x in 
permits['2022 Total Valuation']/1000000]

# Rename the 'County' column in the Permits to 'county' to match Data
permits.rename(columns={'County': 'county'}, inplace=True)

# Drop unneccesary columns from Data
data = data1.drop(['county_ascii'], axis=1)

# Change the case of the 'county' column in Data to match Permits
data['county'] = data['county'].str.upper()

# Merge the DataFrames
df = permits.merge(data, on='county', how='inner')

fig = px.scatter_geo(df, lat='lat', lon='lng', color='county',
                     hover_name='county', scope='usa',
                     hover_data={
                     '2019 New Residential Units': True,
                     '2019 Total Valuation': True,
                     '2020 New Residential Units': True,
                     '2020 Total Valuation': True,
                     '2021 New Residential Units': True,
                     '2021 Total Valuation': True,
                     '2022 New Residential Units': True,
                     '2022 Total Valuation': True,
                     'population': True,
                     },
                     center=dict(lat=32.1574, lon=-82.9071))

fig.update_geos(fitbounds="locations")
fig.update_layout(height=600, margin={"r":0,"t":0,"l":0,"b":0})
fig.write_html('permitvaluationplot.html')
fig.show()
0

There are 0 answers