Seaborn代码不工作。我使用jupyterlite来执行python代码。首先,我以以下方式导入seaborn——
import piplite
await piplite.install('seaborn')
import matplotlib.pyplot as plt
import seaborn as sn
%matplotlib inline
但是当我插入像下面这样的海上代码时,它显示了许多我还不理解的错误——代码链接
我面临的问题
但我插入这段代码在谷歌协作它工作得很好谷歌colab
问题是获得示例数据集,正如我在评论中指出的。
问题步骤与:
# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")
您需要将df = sns.load_dataset("anscombe")
行替换为以下内容:
url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/anscombe.csv' # based on [Data repository for seaborn examples](https://github.com/mwaskom/seaborn-data)
from pyodide.http import open_url
import pandas
df = pandas.read_csv(open_url(url))
这是基于pyodide.http
的open_url()
的使用,参见这里的更多示例。
可选的pyfetch和分配获得的字符串
如果你已经看到了pyfetch,这也可以作为sns.load_dataset()
行基于John Hanley的帖子的替代品,使用pyfetch获取CSV数据。代码被进一步注释:
# GET text at URL via pyfetch based on John Hanley's https://www.jhanley.com/blog/pyscript-loading-python-code-in-the-browser/
url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/anscombe.csv' # based on [Data repository for seaborn examples](https://github.com/mwaskom/seaborn-data)
from pyodide.http import pyfetch
response = await pyfetch(url)
content = (await response.bytes()).decode('utf-8')
# READ in string to dataframe based on [farmOS + JupyterLite: Import a CSV of Animals](https://gist.github.com/symbioquine/7641a2ab258726347ec937e8ea02a167)
import io
import pandas
df = pandas.read_csv(io.StringIO(content))