Sea mistake? Inconsistency in building a heat map

This code:

%matplotlib inline #import numpy as np; np.random.seed(0) import matplotlib.pyplot as plt import seaborn as sns #; sns.set() flights = sns.load_dataset("flights") flights = flights.pivot("month", "year", "passengers") sns.heatmap(flights, annot=True, linewidths=.2, fmt="d") #plt.show() 

The result will look like an official result ( See / check it here ):

enter image description here

However, if I turn off inline build and enable plt.show() , the result will look like this:

Annotated heatmaps?

Ie, there is no annotation, except that one cell and y-label orientation are incorrect if the built-in plotting is disabled . Since this is the only change I made, I think it is a mistake with seaborn that it cannot give consistent results.

Can anyone confirm this? And are there any possible corrections, please?

Update, thanks to Sergey for his feedback, here are my versions of everything that matters:

 Python: 3.5.0 |Anaconda 2.4.0 (64-bit)| (default, Dec 1 2015, 11:46:22) [MSC v.1900 64 bit (AMD64)] IPython: 4.0.0 Matplotlib: 1.5.0 Seaborn: 0.6.0 

So I think this is a Python3 or Matplotlib: 1.5 issue. Just in case, I'll add a Python3 tag.

thanks

+7
python ipython-notebook heatmap seaborn
source share
2 answers

This bug was actually posted on the Seaborn GitHub page here . From the comments there, the problem arises when matplotlib uses the MacOSX , TkAgg or QtAgg (also when using %matplotlib notebook in an IPython / Jupyter laptop).

In principle, changing the backend to another should make the chart work as expected (as shown in the first figure). From the matplotlib documentation you can check which backend you are using with

 matplotlib.get_backend() 

and change it to another using

 matplotlib.use() 

Unfortunately, it seems that this problem appears with all available interactive backends. If this is what you need, you probably have to wait until the error is resolved (you can track any achievements on the GitHub page).

If you are happy to create a PNG / PDF file instead of an interactive window for your chart, then the Agg backend should work correctly with a slight code change:

 import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import seaborn as sns #; sns.set() flights = sns.load_dataset("flights") flights = flights.pivot("month", "year", "passengers") sns.heatmap(flights, annot=True, linewidths=.2, fmt="d") plt.savefig("heatmap.png") 
+3
source share

For me, the code produces the same result no matter if I use %matplotlib inline and make it plt.show() , no errors are observed.

Check your versions of everything relevant:

 import sys print 'Python: ' + sys.version import IPython print 'IPython: ' + IPython.__version__ import matplotlib print 'Matplotlib: ' + matplotlib.__version__ import seaborn print 'Seaborn: ' + seaborn.__version__ 

My versions for reference:

 Python: 2.7.10 |Anaconda 2.4.0 (64-bit)| (default, Oct 21 2015, 19:35:23) [MSC v.1500 64 bit (AMD64)] IPython: 4.0.0 Matplotlib: 1.4.3 Seaborn: 0.6.0 
0
source share

All Articles