0

Trying to build some intuition of how plotting works in seaborn (and overall).

Data

df = pd.DataFrame(dict(categorical_1=['apple', 'banana', 'grapes',
                                      'apple', 'banana', 'grapes',
                                      'apple', 'banana', 'grapes'], 
                  categorical_2=['A','A','A','B','B','B','C','C','C'], 
                  value=[10,2,5,7,3,15,1,6,8]))
pivot_table = df.pivot("categorical_1", "categorical_2", "value")

Running the following code it works fine (i.e. I get a heatmap)

fig, ax = plt.subplots(figsize=(5,5))
sns.heatmap(data=pivot_table, 
            cmap=sns.color_palette("Blues"),
            ax=ax)

plt.show()

But when I split it up and first run

fig, ax = plt.subplots(figsize=(5,5))

and then executing

sns.heatmap(data=pivot_table, 
            cmap=sns.color_palette("Blues"),
            ax=ax)

plt.show()

This doesn't return anything for me? Just trying to understand why that is, the ax object still exists and is being passed into the heatmap function, why would this make a difference? If I call "fig" instead it shows it fine. There are a few of these behaviors when plotting in python that surprises me and slows everything down. I've tried this in a few different environment, and executing the code all together vs doing one part of the time has the same result (plot showing vs no plot showing)

3
  • It seems you are asking about running this code in a particular environment that actually allows to "split it up"? Which one is it? Commented Apr 3, 2019 at 15:56
  • In jupyter, but I could execute it in chunks anywhere. Just tried it in spyder and it works the same Commented Apr 3, 2019 at 15:59
  • 1
    Both Juypter notebook and spyder use IPython, so by "anywhere" you mean IPython?! And by default probably the %matplotlib inline backend?! Then, pyplot figures are closed at the end of cells. See this answer. Commented Apr 3, 2019 at 16:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.