簡體   English   中英

Seaborn 代碼 Anscombe 的四重奏不起作用

[英]Seaborn code Anscombe’s quartet does not work

Seaborn 代碼不起作用。 我使用 jupyterlite 來執行 seaborn python 代碼。 首先,我通過以下方式導入 seaborn --

import piplite
await piplite.install('seaborn')
import matplotlib.pyplot as plt
import seaborn as sn
%matplotlib inline

但是當我插入像下面這樣的 seaborn 代碼時,它會顯示許多我還不明白的錯誤——代碼鏈接

我面臨的問題

但是我將這段代碼插入到 google colab 中,它運行良好google 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 中pyodide.http open_url()的使用,有關更多示例,請參見此處


替代 pyfetch 並分配獲得的字符串

如果您見過 pyfetch,它也可以替代基於John Hanley 的帖子sns.load_dataset()行,它使用 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM