【问题标题】:How to follow seaborn's examples如何遵循 seaborn 的示例
【发布时间】:2015-12-05 21:54:28
【问题描述】:

每个官方 seaborn 演示/示例都以 sns.load_dataset() 开头。我想知道在哪里可以获得这些 seaborn 数据集以便我可以按照示例进行操作?

我尝试使用诸如 “在哪里可以找到官方 seaborn 数据集”等短语自己找到它们,但没有成功。

更新:

那么,我该如何使用它们呢?我正在关注http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.boxplot.htmlthis is the only thing that I get,也就是说,我没有得到任何图表。

不过,我的 seaborn 和 pandas 都运行良好。它们来自我的 Anaconda 安装,并且都是最新版本。我正在使用的 matplotlib 版本works fine with seaborn as well

@gabra,我在提问之前从网上找到了那些csv文件,因为我认为它们只是csv文件,不能直接在sns.load_dataset(xxx)中使用,对吧?

【问题讨论】:

  • 数据集与seaborn一起安装。您想知道它们在您计算机中的位置吗?
  • 看来您使用的matplotlib版本太新,与seaborn版本不兼容。我会考虑使用旧版本的 matplotlib。
  • 具体来说,尝试使用 matplotlib 1.3.1,因为它是在 2013 年 10 月发布的,而 seaborn 包是在 2 年前开发的。或者,在过去的 5 个月中对 seaborn 包进行了很多提交,这可能是对 2015 年 2 月发布的 matplotlib 1.4.3 的响应。我会尝试两者。
  • 您的matplotlibseaborn 是什么版本?
  • 嘿,请不要通过编辑更改问题。如果您需要答案中的更多细节,请评论具体答案。如果您有新问题,请使用 按钮提出新问题,并链接到该问题以供参考。

标签: python dataset seaborn


【解决方案1】:

数据集位于另一个存储库中,称为seaborn-data

在此 repo 中,每个数据集都存储为 .csv 文件。

更新

试试这个:

import seaborn as sns
%matplotlib inline # To show embedded plots in the notebook

tips = sns.load_dataset("tips")

fig, ax = plt.subplots()
ax = sns.boxplot(tips["total_bill"])

【讨论】:

  • @DanLowe,感谢您的反馈。我更新了它,我希望它会更好。
【解决方案2】:

seaborn 包应包含示例教程中引用的示例数据集或检索数据集的方法。

# Load the example planets dataset
planets = sns.load_dataset("planets")

当我在example folder 中查找“行星”数据集时,我看不到这些数据集。对函数“load_datasets”的一点探索表明,示例数据集来自seaborn-data file online,并且需要 pandas 包依赖项。

def load_dataset(name, cache=True, data_home=None, **kws):
    """Load a dataset from the online repository (requires internet).
    Parameters
    ----------
    name : str
        Name of the dataset (`name`.csv on
        https://github.com/mwaskom/seaborn-data).  You can obtain list of
        available datasets using :func:`get_dataset_names`
    cache : boolean, optional
        If True, then cache data locally and use the cache on subsequent calls
    data_home : string, optional
        The directory in which to cache data. By default, uses ~/seaborn_data/
    kws : dict, optional
        Passed to pandas.read_csv
    """
    path = "https://github.com/mwaskom/seaborn-data/raw/master/{0}.csv"
    full_path = path.format(name)

    if cache:
        cache_path = os.path.join(get_data_home(data_home),
                                  os.path.basename(full_path))
        if not os.path.exists(cache_path):
            urlretrieve(full_path, cache_path)
        full_path = cache_path

    df = pd.read_csv(full_path, **kws)
    if df.iloc[-1].isnull().all():
        df = df.iloc[:-1]

    if not pandas_has_categoricals:
        return df

    # Set some columns as a categorical type with ordered levels

    if name == "tips":
        df["day"] = pd.Categorical(df["day"], ["Thur", "Fri", "Sat", "Sun"])
        df["sex"] = pd.Categorical(df["sex"], ["Male", "Female"])
        df["time"] = pd.Categorical(df["time"], ["Lunch", "Dinner"])
        df["smoker"] = pd.Categorical(df["smoker"], ["Yes", "No"])

    if name == "flights":
        df["month"] = pd.Categorical(df["month"], df.month.unique())

    if name == "exercise":
        df["time"] = pd.Categorical(df["time"], ["1 min", "15 min", "30 min"])
        df["kind"] = pd.Categorical(df["kind"], ["rest", "walking", "running"])
        df["diet"] = pd.Categorical(df["diet"], ["no fat", "low fat"])

    if name == "titanic":
        df["class"] = pd.Categorical(df["class"], ["First", "Second", "Third"])
        df["deck"] = pd.Categorical(df["deck"], list("ABCDEFG"))

    return df

【讨论】:

  • 看来您使用的matplotlib版本太新,与seaborn版本不兼容。我会考虑使用旧版本的 matplotlib。
  • 嗯?我更新的问题应该表明我仍然很困惑和迷茫。
  • 您能否运行“print matplotlib.__version__”并告诉我们您的机器上的 matplotlib 版本是什么?
  • 另外,您可能应该选择一个答案,关闭此问题,然后打开一个新问题。您的问题已从这些数据在哪里变为与 seaborn 兼容的 matplotlib 版本。我们已经回答了最初的问题,但没有获得声誉积分,因为您已经扩展到另一个更耗时的问题。
  • 老兄,我说我的 matplotlib 版本与 seaborn 兼容,我说我不能使用那些 .csv 文件。跨度>
猜你喜欢
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多