2

I'm using pycharm to run some code using Seaborn. I'm very new to python and am just trying to learn the ropes so I'm following a tutorial online. I've imported the necessary libraries and have run the below code

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'])

My output is as follows:

Process finished with exit code 0

so I know there are no errors in the code but I don't see any graphs anywhere as I'm supposed to. Ive found a way around this by using this at the end

plt.show()

which opens a new tab and uses matplotlib to show me a similar graph.

However in the code I'm using to follow along, matplotlib is not imported or used (I understand that seaborn has built in Matplotlib functionality) as in the plt.show statement is not used but the a visual graph is still achieved.

I've also used print which gives me the following

AxesSubplot(0.125,0.11;0.775x0.77)

Last point to mention is that the code im following along with uses the following

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

but distplot has now depreciated and I've now used histplot because I think that's the best alternative vs using displot, If that's incorrect please let me know.

I feel there is a simple solution as to why I'm not seeing a graph but I'm not sure if it's to do with pycharm or due to something within the code.

8ull53y3
  • 37
  • 6
  • Could you add some details on your PyCharm setup? In particular PyCharm Community and Professional editions differ in their treatment of figures – Callum Rollo Jan 01 '21 at 14:29
  • As a general answer to your last paragraph, if you are ever unsure if you code or the editor is the source of the issue, try to run the code from the command line or in another editor like Spyder or Jupyter – Callum Rollo Jan 01 '21 at 14:30
  • Hi Marco, I'm using PyCharm Community setup. I'm also guessing it's default as I haven't made any changes to it. – 8ull53y3 Jan 01 '21 at 14:34

2 Answers2

2

matplotlib is a dependency of seaborn. As such, importing matplotlib with import matplotlib.pyplot as plt and calling plt.show() does not add any overhead to your code.

While it is annoying that there is no sns.plt.show() at this time (see this similar question for discussion), I think this is the simplest solution to force plots to show when using PyCharm Community.

Importing matplotlib in this way will not affect how your exercises run as long as you use a namespace like plt.

Callum Rollo
  • 515
  • 3
  • 12
  • Thanks Marco, that's what i've decided to do. I guess what I really wanted to know is if it's possible to show a graph with only using the sns.distplot(df['price']) statement without plt.show() as in the tutorial I'm following or is that step not mentioned as it may seem obvious. And if it's possible is it without using pycharm? It's just for my own knowledge really. If it's not possible and plt.show() has to be used then knowing that will help me out going forward in trying to learn or read other peoples code. – 8ull53y3 Jan 01 '21 at 15:11
  • 1
    This should absolutely be possible. PyCharm is just a bit over-eager in suppressing plots. Probably the simplest it to highlight all the code, right click it and select "Execute selection in Python console" this will run the code as if you had executed it from your terminal. It should produce a pop up window with the plot in. Otherwise you can try running the file from the command line. The exact execution will depend on your OS, try searching for "run Python from command line". You could also try jupyter notebooks. Importantly, whichever way you run it the code should stay the same – Callum Rollo Jan 01 '21 at 15:31
  • In Jupyter interfaces, there is a "matplotlib mode" you can activate that will automatically show the plot as soon as you invoke a matplotlib command that creates one. Maybe PyCharm has something similar? I've never used it. – mwaskom Jan 01 '21 at 16:29
0

Be aware the 'data' must be pandas DataFrame object, not: <class 'pandas.core.series.Series'> I using this, work finely:

# Distribution plot on price

sns.histplot(df[['price']])

plt.show()
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Shahram
  • 27
  • 4