有什么方法可以绘制印度地图吗?

Is there any way to draw INDIA Map in plotly?(有什么方法可以绘制印度地图吗?)

本文介绍了有什么方法可以绘制印度地图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 plotly 绘制印度地图,但无法找到方法.以下是我为美国尝试的代码.

import pandas as pddf_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['州 FIPS 代码'] + df_sample['县 FIPS 代码']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['失业率 (%)'].tolist()无花果 = ff.create_choropleth(fips=fips,值=值,binning_endpoints=endpts,色阶=色阶,show_state_data=假,show_hover=True, centroid_marker={'不透明度': 0},asp=2.9, title='美国失业率%',legend_title='%失业者')fig.layout.template = 无图.show()

输出:

以类似的方式,我只想绘制带有悬停值的印度地图.只想要下面的输出......印度地图的输出:

解决方案

您使用的图形工厂 create_choropleth 方法是

为了更精细地控制绘图,我们可以直接使用图形对象:

将 pandas 导入为 pd导入 plotly.graph_objectsdf = 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',位置=df ['状态'],z=df['活跃病例'],自动色标=假,colorscale ='红色',marker_line_color='桃子',颜色条=字典(title={'text': "活动案例"},厚度=15,长度=0.35,bgcolor='rgba(255,255,255,0.6)',滴答0=0,dtick=20000,xanchor='左',x=0.01,yanchor='底部',y=0.05)))图.update_geos(可见=假,投影=字典(type='圆锥形保形',平行=[12.472944444, 35.172805555556],旋转={'lat':24,'lon':80}),lonaxis={'范围': [68, 98]},lataxis={'范围': [6, 38]})fig.update_layout(标题=字典(text=截至 2020 年 7 月 17 日,印度各州的活跃 COVID-19 病例",xanchor='中心',x=0.5,yref='纸',yanchor='底部',y=1,垫={'b':10}),边距={'r': 0, 't': 30, 'l': 0, 'b': 0},高度=550,宽度=550)图.show()

I am trying to plot India map using plotly, but unable to find a way to do that. Below is the code which I tried for USA.

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()

OUTPUT:

In a similar way I just want to draw India's map with hovering values. and just want output like below... the output of INDIAN MAP:

解决方案

The figure factory create_choropleth method that you're using is deprecated and deals with USA counties exclusively. For other maps, you need the GeoJSON for the features you're mapping. Plotly only comes with GeoJSON data for world countries and US states, so you'll have to provide the data for India's states yourself.

Like your example choropleth, let's plot the current number of active COVID-19 cases per state as of July 17 (this comes from indiacovid19.github.io, which is periodically archiving the data from India's Ministry of Health). As for the GeoJSON, a quick search yields a few GitHub repos but it seems the majority are too outdated for our cases data, as they don't include the merging of Dadra and Nagar Haveli and Daman and Diu. Luckily, datameet provides an up-to-date shapefile for India's states which I simplified a bit to reduce the size and converted to GeoJSON using mapshaper, then flipped the polygon winding using geojson-rewind.

Now, as detailed in the Plotly documentation, we can use plotly express to quickly make a choropleth map with our data:

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()

For more fine control over the plot, we can use the graph objects directly:

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()

这篇关于有什么方法可以绘制印度地图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:有什么方法可以绘制印度地图吗?

基础教程推荐