无边界的多角形结构

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

我正在使用Python中的plotly来创建一个由某个分类变量着色的美国县的choropleth。因为县的面积太小,所以它们之间的边界线占据了图像的大部分。我怎样才能去掉它们 (或将它们的宽度设为零)?

到目前为止的代码和输出(使用随机数据)。

Plot: https:/i.stack.imgur.comG3ltp.png。

from urllib.request import urlopen
import json
import numpy as np
import plotly.express as px
import pandas as pd

#Read county geography
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    buf = response.read()
    counties = json.loads(buf.decode('utf-8'))

#Create random data
df = pd.DataFrame(np.random.randn(99999, 1),columns=["val"])
df['category']=df['val']>1
df['fips'] = list(range(1,100000))

#Graph
fig = px.choropleth(df, geojson=counties, locations='fips', color='category',
scope="usa")
fig.show()

python plotly
1个回答
1
投票

只要指定。

fig['layout']['geo']['subunitcolor']='rgba(0,0,0,0)'

这将边界线的颜色设置为全透明 所以它们看起来不存在。

enter image description here

完整的代码

from urllib.request import urlopen
import json
import numpy as np
import plotly.express as px
import pandas as pd

#Read county geography
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    buf = response.read()
    counties = json.loads(buf.decode('utf-8'))

#Create random data
df = pd.DataFrame(np.random.randn(99999, 1),columns=["val"])
df['category']=df['val']>1
df['fips'] = list(range(1,100000))

#Graph
fig = px.choropleth(df, geojson=counties, locations='fips', color='category',
scope="usa")

fig['layout']['geo']['subunitcolor']='rgba(0,0,0,0)'
fig.show()

2
投票

我可以通过切换到 plotly.graph_objects.Choropleth (plotly_express.choropleth 的低级版本)来将线宽设置为 0。不幸的是,这仍然会在县之间产生线条或空格,但它们更细,这对我的目的来说已经足够好了。感谢 r-初学者 的指针。

输出。

绘图

代码(继续提问)。

import plotly.graph_objects as go

fig = go.Figure(data = go.Choropleth(
    locations = df['fips'],
    z = df['category'].astype(int),
    locationmode = 'geojson-id',
    geojson = counties,
    marker = dict(line=dict(width=0))
))

fig.update_layout(
    geo_scope='usa'
)
fig.show()

2
投票

下面的代码应该可以用。

fig.update_traces(marker_line_width=0)

https:/community.plotly.comtadding-statelines-to-county-level-geojson-based-choropleth36269。

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