Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn Heatmap Subplots - keep axis ratio consistent

If I have the following code:

import seaborn 
import matplotlib.pyplot as plt
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
f,(ax1,ax2,ax3) = plt.subplots(1,3,sharey=True)
g1 = sns.heatmap(flights,cmap="YlGnBu",cbar=False,ax=ax1)
g1.set_ylabel('')
g1.set_xlabel('')
g2 = sns.heatmap(flights,cmap="YlGnBu",cbar=False,ax=ax2)
g2.set_ylabel('')
g2.set_xlabel('')
g3 = sns.heatmap(flights,cmap="YlGnBu",ax=ax3)
g3.set_ylabel('')
g3.set_xlabel('')

Which outputs the following - enter image description here

How can I adjust the subplots so that the g3 axis is the same width as the g1,g2 axis. Since I have not added the color bar to the first two axis', seaborn shrinks the third axis down to make the entire figure consistent. This is understandable.

I want this:

enter image description here

Perhaps I need to make a 4 panel subplot with the fourth panel only containing the colorbar?

like image 762
jwillis0720 Avatar asked Mar 10 '17 07:03

jwillis0720


People also ask

Can you use Seaborn with subplots?

In this article, we will explore how to create a subplot or multi-dimensional plot in seaborn, It is a useful approach to draw subplot instances of the same plot on different subsets of your dataset. It allows a viewer to quickly extract a large amount of data about complex information.

What is FMT in heatmap Seaborn?

The annot only help to add numeric value on python heatmap cell but fmt parameter allows to add string (text) values on the cell. Here, we created a 2D numpy array which contains string values and passes to annot along with that pass a string value “s” to fmt.

What is CMAP in Seaborn?

cmapmatplotlib colormap name or object, or list of colors, optional. The mapping from data values to color space.


1 Answers

A way to go is indeed to create 4 axes, where the fourth axes will contain the colorbar. You can use the cbar_ax argument to tell the heatmap in which axes to plot the colorbar. In order to create the axes with some good proportions, you can use the gridspec_kw argument to subplots. The problem is then that the axes would share the y scaling with the colorbar, so we need to turn sharey off and manually share the first three axes by using ax1.get_shared_y_axes().join(ax2,ax3). This in turn will create unwanted axis labels, which need to be turned off.

import seaborn  as sns
import matplotlib.pyplot as plt
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
f,(ax1,ax2,ax3, axcb) = plt.subplots(1,4, 
            gridspec_kw={'width_ratios':[1,1,1,0.08]})
ax1.get_shared_y_axes().join(ax2,ax3)
g1 = sns.heatmap(flights,cmap="YlGnBu",cbar=False,ax=ax1)
g1.set_ylabel('')
g1.set_xlabel('')
g2 = sns.heatmap(flights,cmap="YlGnBu",cbar=False,ax=ax2)
g2.set_ylabel('')
g2.set_xlabel('')
g2.set_yticks([])
g3 = sns.heatmap(flights,cmap="YlGnBu",ax=ax3, cbar_ax=axcb)
g3.set_ylabel('')
g3.set_xlabel('')
g3.set_yticks([])

# may be needed to rotate the ticklabels correctly:
for ax in [g1,g2,g3]:
    tl = ax.get_xticklabels()
    ax.set_xticklabels(tl, rotation=90)
    tly = ax.get_yticklabels()
    ax.set_yticklabels(tly, rotation=0)

plt.show()

enter image description here

like image 69
ImportanceOfBeingErnest Avatar answered Sep 22 '22 09:09

ImportanceOfBeingErnest