在 seaborn 库中绘制 Lineplot 时遇到问题
Posted
技术标签:
【中文标题】在 seaborn 库中绘制 Lineplot 时遇到问题【英文标题】:Having problems plotting Lineplot in seaborn library 【发布时间】:2020-05-31 09:02:54 【问题描述】:我想在这里请求一些帮助。我在 Python 中使用 seaborn 来绘制多条线图,但不幸的是,我不断出错。然而,当我在 seaborn 中使用预加载的数据集(如 fMRI 数据集)时,它运行良好。
下面是我使用的代码:
import seaborn as sns; sns.set()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv("ucc.csv")
df
df = sns.load_dataset("ucc")
ax = sns.lineplot(x="hours", y="viability", data=ucc)
这是我在运行上述代码时遇到的错误:
HTTPError Traceback (most recent call last)
<ipython-input-3-07a022f8777e> in <module>
----> 1 df = sns.load_dataset("ucc")
2 ax = sns.lineplot(x="hours", y="viability", data=ucc)
~/miniconda3/lib/python3.7/site-packages/seaborn/utils.py in load_dataset(name, cache, data_home, **kws)
434 os.path.basename(full_path))
435 if not os.path.exists(cache_path):
--> 436 urlretrieve(full_path, cache_path)
437 full_path = cache_path
438
~/miniconda3/lib/python3.7/urllib/request.py in urlretrieve(url, filename, reporthook, data)
245 url_type, path = splittype(url)
246
--> 247 with contextlib.closing(urlopen(url, data)) as fp:
248 headers = fp.info()
249
~/miniconda3/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
220 else:
221 opener = _opener
--> 222 return opener.open(url, data, timeout)
223
224 def install_opener(opener):
~/miniconda3/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
529 for processor in self.process_response.get(protocol, []):
530 meth = getattr(processor, meth_name)
--> 531 response = meth(req, response)
532
533 return response
~/miniconda3/lib/python3.7/urllib/request.py in http_response(self, request, response)
639 if not (200 <= code < 300):
640 response = self.parent.error(
--> 641 'http', request, response, code, msg, hdrs)
642
643 return response
~/miniconda3/lib/python3.7/urllib/request.py in error(self, proto, *args)
567 if http_err:
568 args = (dict, 'default', 'http_error_default') + orig_args
--> 569 return self._call_chain(*args)
570
571 # XXX probably also want an abstract factory that knows when it makes
~/miniconda3/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
501 for handler in handlers:
502 func = getattr(handler, meth_name)
--> 503 result = func(*args)
504 if result is not None:
505 return result
~/miniconda3/lib/python3.7/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
647 class HTTPDefaultErrorHandler(BaseHandler):
648 def http_error_default(self, req, fp, code, msg, hdrs):
--> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp)
650
651 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 404: Not Found
【问题讨论】:
【参考方案1】:让我分解一下您所做的事情,以便您可以更好地了解正在发生的事情;查看与您的代码内联的 cmets。
import seaborn as sns; sns.set()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv("ucc.csv"). # Loads the csv file locally into a pandas dataframe
df # Prints the head of that dataframe
# This command specifically loads datasets from [github](https://github.com/mwaskom/seaborn-data) by name; It's for testing. You are getting the 404 error because there is no ucc dataset in the github repository.
df = sns.load_dataset("ucc")
# This is the command that actually creates the plot. You are referencing a variable 'ucc' that you have not declared and you would get an error if you had not already excepted from the prior 404.
ax = sns.lineplot(x="hours", y="viability", data=ucc)
我没有你的数据集,所以我不能给你一个确切的例子,但是,使用本地下载的 mpg 数据集,我至少可以给你一个我认为你正在尝试完成的工作示例。
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('mpg.csv')
ax = sns.lineplot(x=df['model_year'], y=df['mpg'])
【讨论】:
非常感谢教授/博士。实际上,我对使用 seaborn 进行数据科学非常陌生。我尝试在这里上传数据集,但找不到正确的方法。基本上,数据集包含四列“类别”、具有 0、24、48、72 和 96 小时的“小时”,每个时间的“生存能力”,以及包含八 (8) 个不同细胞系的“细胞”。我想使用 Seaborn 绘制带有误差条的 x=hours 和 y=viability 的线图。我会很高兴从你那里学到更多。我希望我能找到一种附加数据集的方法。再次感谢您,先生。 尊敬的教授/博士。 JerodG,我刚刚添加了上面数据集的快照,以观察数据集是如何排列的。我找不到在哪里附加我会这样做的数据集。感谢您期待您的积极回应。以上是关于在 seaborn 库中绘制 Lineplot 时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章