Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have a bar next to python seaborn heatmap which shows the summation of row values?

I am able to generate a heatmap with quantity overlaid on the graphic as a visual of a pivot table. I would like to have a column next to the heatmap which shows the summation of rows and I would like to have a row under the heatmap that shows the summation of columns.

Is there a way to incorporate this into the heatmap figure? pv is my pivot table which I use to generate the heatmap figure. I would like to have a column on the right of the chart which has the summed values for each row. Likewise, I would like to have a row on the bottom of the chart which has the summed values for each column.

fig = plt.figure(figsize = (20,10))
mask = np.zeros_like(pv)
mask[np.tril_indices_from(mask)] = True
#with sns.axes_style("white"):
ax = sns.heatmap(pv, annot=True, cmap="YlGnBu",mask=mask, linecolor='b', cbar = False)
ax.xaxis.tick_top()
plt.xticks(rotation=90)

Nevada Chart Output

like image 597
JL1515 Avatar asked Oct 27 '15 22:10

JL1515


People also ask

How to control the layout of Heatmaps in Seaborn using matplotlib?

We can use the subplot () feature of matplotlib.pyplot to control the layout of heatmaps in Seaborn. This will give you maximum control over the final graphic and allow for easy export of the image. Creating subplots using Matplotlib is as easy as defining their shape (2 subplots in 1 column in our case):

How do I use a bar plot in Seaborn?

Bar charts can be used for visualizing a time series, as well as just categorical data. Plotting a Bar Plot in Seaborn is as easy as calling the barplot () function on the sns instance, and passing in the categorical and continuous variables that we'd like to visualize: Here, we've got a few categorical variables in a list - A, B and C.

Can Seaborn generate Heatmaps for categorical data?

Unfortunately at the time of writing, Seaborn does not have the built-in ability to produce heatmaps for categorical data like this as it expects numerical input. Here's a code snippet that shows it is possible to "fake "it with a little palette and color bar hacking.

What is heatmap in Python?

Heatmap in Python is one of the many data visualization techniques. Data visualization refers to the graphical representation of data and may include graphs, charts, maps, and other visual elements. It is highly critical for analyzing humongous amounts of information and making data-driven decisions.


1 Answers

@Paul H subplot suggestion did work for my purposes. The code below got me the figure shown. Not sure if this is the most resource efficient method but it got me what I needed. enter image description here

fig = plt.figure(figsize=(20,15))
ax1 = plt.subplot2grid((20,20), (0,0), colspan=19, rowspan=19)
ax2 = plt.subplot2grid((20,20), (19,0), colspan=19, rowspan=1)
ax3 = plt.subplot2grid((20,20), (0,19), colspan=1, rowspan=19)

mask = np.zeros_like(pv)
mask[np.tril_indices_from(mask)] = True

sns.heatmap(pv, ax=ax1, annot=True, cmap="YlGnBu",mask=mask, linecolor='b', cbar = False)
ax1.xaxis.tick_top()
ax1.set_xticklabels(pv.columns,rotation=40)

sns.heatmap((pd.DataFrame(pv.sum(axis=0))).transpose(), ax=ax2,  annot=True, cmap="YlGnBu", cbar=False, xticklabels=False, yticklabels=False)
sns.heatmap(pd.DataFrame(pv.sum(axis=1)), ax=ax3,  annot=True, cmap="YlGnBu", cbar=False, xticklabels=False, yticklabels=False)
like image 80
JL1515 Avatar answered Oct 04 '22 13:10

JL1515