每个官方的seaborn演示/示例都以sns.load_dataset()
开头。我想知道在哪里可以获得这些seaborn数据集,以便我可以按照这些示例进行操作?
我试图用“在哪里找到官方的seaborn数据集”等词语来找到它们,但没有点击。
更新:
那么,我该如何使用它们呢?我正在关注http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.boxplot.html和this is the only thing that I get,即我没有得到任何图表。
我的seaborn和pandas都工作得很好。它们来自我的Anaconda安装,并且都是最新版本。我正在使用works fine with seaborn as well的matplotlib版本。
@gabra,我在问这个问题之前就已经从互联网上找到了那些csv文件,因为我认为它们只是csv文件,不能直接在sns.load_dataset(xxx)
中使用,对吗?
答案 0 :(得分:5)
数据集位于另一个名为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"])
答案 1 :(得分:0)
seaborn包应包含示例教程中引用的样本数据集或检索数据集的方法。
# Load the example planets dataset
planets = sns.load_dataset("planets")
当我在example folder中查找“行星”数据集时,我看不到数据集。对函数“load_datasets”的详细探索表明,示例数据集来自seaborn-data file在线并且需要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