【问题标题】:When I run '''sns.histplot(df['price'])''' in pycharm I get the code output but no graph, why is this?当我在 pycharm 中运行 '''sns.histplot(df['price'])''' 时,我得到了代码输出但没有图表,这是为什么呢?
【发布时间】:2021-04-08 08:09:21
【问题描述】:

我正在使用 pycharm 使用 Seaborn 运行一些代码。我对 python 很陌生,只是想学习一些技巧,所以我正在在线学习教程。我已经导入了必要的库并运行了以下代码

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# import the data saved as a csv
df = pd.read_csv('summer-products-with-rating-and-performance_2020-08.csv')

df["has_urgency_banner"] = df["has_urgency_banner"].fillna(0)

df["discount"] = (df["retail_price"] -
df["price"])/df["retail_price"]

df["rating_five_percent"] = df["rating_five_count"]/df["rating_count"]
df["rating_four_percent"] = df["rating_four_count"]/df["rating_count"]
df["rating_three_percent"] = df["rating_three_count"]/df["rating_count"]
df["rating_two_percent"] = df["rating_two_count"]/df["rating_count"]
df["rating_one_percent"] = df["rating_one_count"]/df["rating_count"]

ratings = [
    "rating_five_percent",
    "rating_four_percent",
    "rating_three_percent",
    "rating_two_percent",
    "rating_one_percent"
]

for rating in ratings:
    df[rating] = df[rating].apply(lambda x: x if x>= 0 and x<= 1 else 0)

# Distribution plot on price
sns.histplot(df['price'])

我的输出如下:

进程以退出代码 0 结束

所以我知道代码中没有错误,但我没有看到任何我应该看到的图表。 最后我找到了解决这个问题的方法

plt.show()

打开一个新选项卡并使用 matplotlib 向我显示一个类似的图表。

但是在我用来跟随的代码中,未导入或使用 matplotlib(我知道 seaborn 已内置 Matplotlib 功能),因为未使用 plt.show 语句,但仍可实现可视化图形.

我还使用了 print,它给了我以下信息

AxesSubplot(0.125,0.11;0.775x0.77)

最后要提的是,我跟随的代码使用了以下

import seaborn as sns
# Distribution plot on price
sns.distplot(df['price'])

但是 distplot 现在已经贬值了,我现在使用 histplot 因为我认为这是与使用 displot 相比最好的选择,如果不正确,请告诉我。

我觉得有一个简单的解决方案可以解决为什么我没有看到图表,但我不确定这是否与 pycharm 或代码中的某些内容有关。

【问题讨论】:

  • 您能否添加一些有关您的 PyCharm 设置的详细信息?特别是 PyCharm 社区版和专业版在处理数字方面有所不同
  • 作为对最后一段的一般回答,如果您不确定您的代码或编辑器是否是问题的根源,请尝试从命令行或其他编辑器(如 Spyder)中运行代码或 Jupyter
  • 嗨,Marco,我正在使用 PyCharm 社区设置。我也猜测它是默认的,因为我没有对其进行任何更改。

标签: python matplotlib graph seaborn


【解决方案1】:

matplotlib 是一个dependency of seaborn。因此,使用 import matplotlib.pyplot as plt 导入 matplotlib 并调用 plt.show() 不会给您的代码增加任何开销。

虽然此时没有sns.plt.show() 很烦人(请参阅类似的question 进行讨论),但我认为这是在使用 PyCharm 社区时强制显示图表的最简单解决方案。

只要你使用像plt这样的命名空间,以这种方式导入matplotlib不会影响你的练习的运行方式。

【讨论】:

  • 谢谢 Marco,这就是我决定做的。我想我真正想知道的是是否可以仅使用 sns.distplot(df['price']) 语句而不使用 plt.show() 来显示图表,就像我正在遵循的教程中那样,或者是那一步未提及,因为它可能看起来很明显。如果有可能不使用pycharm吗?这只是为了我自己的知识。如果不可能并且必须使用 plt.show() ,那么知道这将有助于我继续尝试学习或阅读其他人的代码。
  • 这绝对是可能的。 PyCharm 只是有点过于渴望抑制情节。可能是最简单的突出显示所有代码的方法,右键单击它并选择“在 Python 控制台中执行选择”,这将运行代码,就像您从终端执行它一样。它应该会产生一个带有绘图的弹出窗口。否则,您可以尝试从命令行运行该文件。确切的执行将取决于您的操作系统,尝试搜索“从命令行运行 Python”。你也可以试试 jupyter notebooks。重要的是,无论您以哪种方式运行,代码都应该保持不变
  • 在 Jupyter 界面中,您可以激活一个“matplotlib 模式”,只要您调用创建一个的 matplotlib 命令,它就会自动显示绘图。也许 PyCharm 有类似的东西?我从来没用过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2014-08-20
  • 1970-01-01
  • 2021-09-06
相关资源
最近更新 更多