Python 更改生动的合唱团字体和图例大小
我有以下代码来生成2016年至2020年间县级选票变化图Python 更改生动的合唱团字体和图例大小,python,plotly,choropleth,Python,Plotly,Choropleth,我有以下代码来生成2016年至2020年间县级选票变化图 from urllib.request import urlopen import json with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response: counties = json.load(response) import plotly.express as
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
import plotly.express as px
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='vote_change',
color_continuous_scale="magma",
range_color=(-25, 35),
mapbox_style="carto-positron",
zoom=2, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.75,
title='Figure 2: Change in Turnout from 2016 to 2020',
labels={'total_votes_2016':'TEST'}
)
fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})
)
fig.show()
fig.write_image("../figures/vote_change_map.png", width = 450, height = 250)
该代码将呈现此结果png
我想使标题文本大小为8,并可能使图例更窄,这样就不会占用太多空间。有人知道怎么做吗?首先,让我们从更改
title font size=8
开始。然后我们将解决与图例大小
相关的问题。有关更改font size=8
的信息,请参阅以下更新的代码:-
# Import all the Libraries
from urllib.request import urlopen
import plotly.express as px
import json
# Open JSON File Using 'urlopen' Module of 'json' library and used 'json.load()' JSON Loader to load JSON Data
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
# Used Mapbox choropleth map, each row of 'data_frame (df)' is represented by a colored region on a Mapbox map.
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='vote_change',
color_continuous_scale="magma",
range_color=(-25, 35),
mapbox_style="carto-positron",
zoom=2, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.75,
labels={'total_votes_2016':'TEST'}
)
# Updated Layout for 'Title with Font Size 8' and 'Narrower Legend'
fig.update_layout(
title='Figure 2: Change in Turnout from 2016 to 2020',
margin=dict(l=0, r=0, t=40, b=0),
font=dict(size=8),
)
# Show Plotted Figure
fig.show()
# Store Image of Generated Plot
fig.write_image("../figures/vote_change_map.png", width = 450, height = 250)
我使用了您提供的相同代码。现在,我们可以使用图例大小
因此,根据我的说法,您不能更改图例大小。由于定义了图像大小,图例大小变大了。当前图像大小正在挤压所有布局
有两种解决方案可以帮助您:-
注意:-所有与位置
和大小
相关的参数都是预期参数。你可以根据需要加满
(1.)以水平格式打印图例:-
如果不想更改图像大小。然后您可以尝试以水平
格式绘制图例
。执行此任务的参考代码如下所示:-
fig.update_layout(
# customize legend orientation & position
legend=dict(
title=None, orientation = 'h', y=1, yanchor="bottom", x=0.5, xanchor="center"
)
)
# Added 'width=600' and 'height=400' in Current Code for Ploting Chroleopath Mapbox
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='vote_change',
width=600, height=400,
color_continuous_scale="magma",
range_color=(-25, 35),
mapbox_style="carto-positron",
zoom=2, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.75,
labels={'total_votes_2016':'TEST'}
)
注意:-如果您想了解有关方向
、x
、xanchor
或与绘图图例
相关的更多操作的详细信息,您可以参考:
(2.)更改图像大小:-
如果要更改图像大小
,可以参考以下代码:-
fig.update_layout(
# customize legend orientation & position
legend=dict(
title=None, orientation = 'h', y=1, yanchor="bottom", x=0.5, xanchor="center"
)
)
# Added 'width=600' and 'height=400' in Current Code for Ploting Chroleopath Mapbox
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='vote_change',
width=600, height=400,
color_continuous_scale="magma",
range_color=(-25, 35),
mapbox_style="carto-positron",
zoom=2, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.75,
labels={'total_votes_2016':'TEST'}
)
如果它看起来很完美,那么您可以使用相同的大小存储它,使用:-
# Store Image of Generated PLot
fig.write_image("../figures/vote_change_map.png", width = 600, height = 400)
希望此解决方案对您有所帮助