Plotly,streamlit:如何保持全屏视图
Plotly, streamlit: how to keep the fullscreen view
我正在绘制有关全球疫苗接种率的等值线图。为了使用连续颜色绘制,我使用 Plotly
绘制地图,并使用 streamlit
使用 Python 在网页上渲染地图。但是,地图太小了,我试过fig.update_layout()
调整大小,但是地图没有居中。工具栏上的“查看全屏”完全解决了我的问题,因为它居中并且始终适合屏幕。那么,有什么办法让它全屏显示吗?
这是我的代码:
import streamlit as st
import pandas as pd
import plotly.express as px
vaccination_data = pd.read_csv('vaccinations.csv')
df = pd.DataFrame(vaccination_data)
def vaccination_lastest_date_by_country(df):
return df.groupby('location').last().reset_index()
country_lastest_vaccination = vaccination_lastest_date_by_country(df)
df_vaccination_by_country = country_lastest_vaccination.sort_values(by='total_vaccinations_per_hundred', ascending=True)
fig = px.choropleth(df_vaccination_by_country, locations="iso_code",
color="total_vaccinations_per_hundred",
hover_name="location",
color_continuous_scale=px.colors.sequential.Plasma) # should be able to adjust the color
fig.update_layout(autosize=False, width= 1200, height=800) # map not centered
st.plotly_chart(fig)
数据为public:https://github.com/owid/covid-19-data/blob/master/public/data/vaccinations/README.md
我设法使用@Pluviophile 评论使其工作,use_container_width=True
和 st.set_page_config(layout="wide")
。我确实使用了 chloropleth examples 之一来显示数据。
fig.update_layout(title_text="title", margin={"r": 0, "t": 0, "l": 0, "b": 0}, height=800)
st.plotly_chart(fig, use_container_width=True)
完整代码如下:
import json
from urllib.request import urlopen
import pandas as pd
import streamlit as st
import plotly.express as px
st.set_page_config(
layout="wide",
)
with urlopen(
"https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json"
) as response:
counties = json.load(response)
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
dtype={"fips": str},
)
fig = px.choropleth(
df,
geojson=counties,
locations="fips",
color="unemp",
color_continuous_scale="Viridis",
range_color=(0, 12),
scope="usa",
labels={"unemp": "unemployment rate"},
)
fig.update_layout(title_text="title", margin={"r": 0, "t": 0, "l": 0, "b": 0}, height=800)
st.plotly_chart(fig, use_container_width=True)
我正在绘制有关全球疫苗接种率的等值线图。为了使用连续颜色绘制,我使用 Plotly
绘制地图,并使用 streamlit
使用 Python 在网页上渲染地图。但是,地图太小了,我试过fig.update_layout()
调整大小,但是地图没有居中。工具栏上的“查看全屏”完全解决了我的问题,因为它居中并且始终适合屏幕。那么,有什么办法让它全屏显示吗?
这是我的代码:
import streamlit as st
import pandas as pd
import plotly.express as px
vaccination_data = pd.read_csv('vaccinations.csv')
df = pd.DataFrame(vaccination_data)
def vaccination_lastest_date_by_country(df):
return df.groupby('location').last().reset_index()
country_lastest_vaccination = vaccination_lastest_date_by_country(df)
df_vaccination_by_country = country_lastest_vaccination.sort_values(by='total_vaccinations_per_hundred', ascending=True)
fig = px.choropleth(df_vaccination_by_country, locations="iso_code",
color="total_vaccinations_per_hundred",
hover_name="location",
color_continuous_scale=px.colors.sequential.Plasma) # should be able to adjust the color
fig.update_layout(autosize=False, width= 1200, height=800) # map not centered
st.plotly_chart(fig)
数据为public:https://github.com/owid/covid-19-data/blob/master/public/data/vaccinations/README.md
我设法使用@Pluviophile 评论使其工作,use_container_width=True
和 st.set_page_config(layout="wide")
。我确实使用了 chloropleth examples 之一来显示数据。
fig.update_layout(title_text="title", margin={"r": 0, "t": 0, "l": 0, "b": 0}, height=800)
st.plotly_chart(fig, use_container_width=True)
完整代码如下:
import json
from urllib.request import urlopen
import pandas as pd
import streamlit as st
import plotly.express as px
st.set_page_config(
layout="wide",
)
with urlopen(
"https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json"
) as response:
counties = json.load(response)
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
dtype={"fips": str},
)
fig = px.choropleth(
df,
geojson=counties,
locations="fips",
color="unemp",
color_continuous_scale="Viridis",
range_color=(0, 12),
scope="usa",
labels={"unemp": "unemployment rate"},
)
fig.update_layout(title_text="title", margin={"r": 0, "t": 0, "l": 0, "b": 0}, height=800)
st.plotly_chart(fig, use_container_width=True)