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)

L Xandor
  • 1,659
  • 4
  • 24
  • 48
  • It seems you are asking about running this code in a particular environment that actually allows to "split it up"? Which one is it? – ImportanceOfBeingErnest Apr 03 '19 at 15:56
  • In jupyter, but I could execute it in chunks anywhere. Just tried it in spyder and it works the same – L Xandor Apr 03 '19 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](https://stackoverflow.com/questions/45341765/matplotlib-inline-causes-error-in-following-code/45347268#45347268). – ImportanceOfBeingErnest Apr 03 '19 at 16:12

0 Answers0