按纬度和经度添加散点图到等值线

问题描述 投票:0回答:1

我正在尝试将散点图放在 Python 中明尼苏达州的等值线图上。纬度和经度值已经给出。我正在尝试更新的情节:

import plotly.figure_factory as ff
import plotly.express as px
import numpy as np
import pandas as pd

df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')
df_sample_r = df_sample[df_sample['STNAME'] == 'Minnesota']

values = df_sample_r['TOT_POP'].tolist()
fips = df_sample_r['FIPS'].tolist()

endpts = list(np.mgrid[min(values):max(values):8j])
colorscale = ["#030512","#1d1d3b","#323268","#3d4b94","#3e6ab0",
              "#4989bc","#60a7c7","#85c5d3","#b7e0e4","#eafcfd"]

fig = ff.create_choropleth(
    fips=fips, values=values, scope=['Minnesota'], show_state_data=True,
    colorscale=colorscale, binning_endpoints=endpts, round_legend_values=True,
    plot_bgcolor='rgb(229,229,229)',
    simplify_county = 0.0,
    paper_bgcolor='rgb(229,229,229)',
    legend_title='Population by County',
    county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},
    exponent_format=True,
)

fig.show()
python plot plotly visualization geo
1个回答
0
投票

我有点不清楚你的问题:你还能输出等值线图,还是无法绘制?我试图运行你的代码,但即使在安装了必要的模块之后,我也无法绘制地图并出现错误。我建议先显示地图的解决方法。我已经在参考你的代码中改编了examples

import geopandas as gpd

df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv')
df_sample_r = df_sample[df_sample['STNAME'] == 'Minnesota']
df_sample_r['FIPS'] = df_sample_r['FIPS'].astype(str)

geo_df = gpd.read_file('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json')
geo_df.rename(columns={'id':'FIPS'}, inplace=True)
geo_df = geo_df.merge(df_sample_r, on='FIPS').set_index('FIPS')

fig = px.choropleth(geo_df,
                    geojson=geo_df.geometry,
                    locations=geo_df.index,
                    color="TOT_POP",
                    hover_data=['CTYNAME'],
                    projection="mercator",
                    color_continuous_scale='blues',
               )

fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(autosize=False, width=450, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.