我正在尝试构建一个简化的应用程序,以方便探索美国人口普查数据。我正在使用censusdis包来获取和呈现数据,下面是重现该问题的程序片段:
import streamlit as st
import censusdis.data as ced
import censusdis.maps as cem
from censusdis.datasets import ACS5
df = ced.download(dataset=ACS5,
vintage=2022,
download_variables=['NAME', 'B19013_001E'],
state='01',
county='*',
with_geometry=True)
st.dataframe(df[['NAME', 'B19013_001E']], hide_index=True)
cem.plot_map(df, 'B19013_001E', legend=True, with_background=True)
st.pyplot()
该代码“有效”,但生成此警告:
PyplotGlobalUseWarning: You are calling st.pyplot() without any arguments. After December 1st, 2020, we will remove the ability to do this as it requires the use of Matplotlib's global figure object, which is not thread-safe.
To future-proof this code, you should pass in a figure as shown below:
>>> fig, ax = plt.subplots()
>>> ax.scatter([1, 2, 3], [1, 2, 3])
>>> ... other plotting actions ...
>>> st.pyplot(fig)
我不知道如何解决这个问题。我尝试这样做:
st.pyplot(cem.plot_map(df, 'B19013_001E', legend=True, with_background=True))
但这产生了这个错误(不是警告):
AttributeError: 'Axes' object has no attribute 'savefig'
只需将调用保存
plot_map
为Fig,然后fig.figure
在中引用即可st.pyplot
。