gpt4 book ai didi

python - 有什么方法可以绘制印度 map 吗?

转载 作者:行者123 更新时间:2023-12-03 16:06:37 34 4
gpt4 key购买 nike

我正在尝试使用plotly绘制印度 map ,但无法找到一种方法。以下是我为美国尝试过的代码。

import pandas as pd

df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv')
df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(lambda x: str(x).zfill(2))
df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(lambda x: str(x).zfill(3))
df_sample['FIPS'] = df_sample['State FIPS Code'] + df_sample['County FIPS Code']

colorscale = ["#f7fbff","#ebf3fb","#deebf7","#d2e3f3","#c6dbef","#b3d2e9","#9ecae1",
"#85bcdb","#6baed6","#57a0ce","#4292c6","#3082be","#2171b5","#1361a9",
"#08519c","#0b4083","#08306b"]
endpts = list(np.linspace(1, 12, len(colorscale) - 1))
fips = df_sample['FIPS'].tolist()
values = df_sample['Unemployment Rate (%)'].tolist()

fig = ff.create_choropleth(
fips=fips, values=values,
binning_endpoints=endpts,
colorscale=colorscale,
show_state_data=False,
show_hover=True, centroid_marker={'opacity': 0},
asp=2.9, title='USA by Unemployment %',
legend_title='% unemployed'
)

fig.layout.template = None
fig.show()
输出:
enter image description here
以类似的方式,我只想绘制具有悬停值的印度 map 。
只是想要像下面的输出...
INDIAN MAP的输出:
enter image description here

最佳答案

您使用的图形工厂create_choropleth方法是deprecated,专门处理美国县。对于其他 map ,您需要使用GeoJSON来映射您的 map 项。 Plotly仅随附世界各国和美国各州的GeoJSON数据,因此您必须自己提供印度各州的数据。
像您的示例choropleth一样,让我们​​绘制截至July 17的每个州的当前活跃COVID-19病例数(这来自indiacovid19.github.io,它正在定期归档来自印度卫生部的数据)。至于GeoJSON,快速搜索会产生一些GitHub存储库,但对于我们的案例数据而言,似乎大多数都已经过时了,因为它们不包括Dadra和Nagar Haveli以及Daman和Diu的合并。幸运的是,datameet为印度各邦提供了最新的shapefile,我对其进行了一些简化以减小尺寸,并使用GeoJSON转换为mapshaper,然后使用geojson-rewind翻转了多边形缠绕。
现在,如Plotly documentation中所述,我们可以使用plotly express快速使用我们的数据制作一个Choropleth映射:

import pandas as pd
import plotly.express as px

df = pd.read_csv("https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/active_cases_2020-07-17_0800.csv")

fig = px.choropleth(
df,
geojson="https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/india_states.geojson",
featureidkey='properties.ST_NM',
locations='state',
color='active cases',
color_continuous_scale='Reds'
)

fig.update_geos(fitbounds="locations", visible=False)

fig.show()
Active cases simple plotly express
为了更好地控制图,我们可以直接使用图对象:
import pandas as pd
import plotly.graph_objects as go

df = pd.read_csv("https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/active_cases_2020-07-17_0800.csv")

fig = go.Figure(data=go.Choropleth(
geojson="https://gist.githubusercontent.com/jbrobst/56c13bbbf9d97d187fea01ca62ea5112/raw/e388c4cae20aa53cb5090210a42ebb9b765c0a36/india_states.geojson",
featureidkey='properties.ST_NM',
locationmode='geojson-id',
locations=df['state'],
z=df['active cases'],

autocolorscale=False,
colorscale='Reds',
marker_line_color='peachpuff',

colorbar=dict(
title={'text': "Active Cases"},

thickness=15,
len=0.35,
bgcolor='rgba(255,255,255,0.6)',

tick0=0,
dtick=20000,

xanchor='left',
x=0.01,
yanchor='bottom',
y=0.05
)
))

fig.update_geos(
visible=False,
projection=dict(
type='conic conformal',
parallels=[12.472944444, 35.172805555556],
rotation={'lat': 24, 'lon': 80}
),
lonaxis={'range': [68, 98]},
lataxis={'range': [6, 38]}
)

fig.update_layout(
title=dict(
text="Active COVID-19 Cases in India by State as of July 17, 2020",
xanchor='center',
x=0.5,
yref='paper',
yanchor='bottom',
y=1,
pad={'b': 10}
),
margin={'r': 0, 't': 30, 'l': 0, 'b': 0},
height=550,
width=550
)

fig.show()
Active cases full plotly graph objects

关于python - 有什么方法可以绘制印度 map 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60910962/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com