11 Nisan 2022 Pazartesi

Boston Crime Data Analysis - 2: A better heatmap with row and column totals

 I've just shared a blog post with a heatmap of crime rates per district and days. However, I wanted to add the totals of rows and columns and came up with this. So, this version shows the most dangerous districts and days as well, by aggregating the numbers.

I am also adding the codes below. You will find the source at the top, which was useful for me to reach this solution.

If you add totals as a part of the table (as the last row or column), it ruins the colors -as expected- since the aggregated numbers in the totals column and row would be the only dark cells and make all other cells lighter without much difference..

So in seaborn, adding a total column/row would be possible with .subplot2grid method.


Code:

# Source:
# https://stackoverflow.com/questions/33379261/how-can-i-have-a-bar-next-to-python-seaborn-heatmap-which-shows-the-summation-of

fig = plt.figure(figsize=(16,12))
ax1 = plt.subplot2grid((14,10), (0,0), rowspan=12, colspan=7)
ax2 = plt.subplot2grid((14,10), (0,8), rowspan=12, colspan=1)
ax3 = plt.subplot2grid((14,10), (13,0), rowspan=1, colspan=7)

table_pivot= data.pivot_table("Hour",
                              ["DISTRICT"],
                              columns="DAY_OF_WEEK",
                              aggfunc = "count")

column_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

table_pivot2 = table_pivot.reindex(column_order, axis=1)
table_pivot2 = table_pivot2.drop("External", axis=0)
cmap = sns.cm.rocket_r

sns.heatmap(table_pivot2,
            ax=ax1,
            annot = True,
            fmt="0.1f",
            linewidths=.8,
            vmin=-0.05,
            cmap = cmap,
            cbar = False).set_title('Number of Crimes per District and Day of Week',
                                   fontsize=14,
                                   fontweight='bold')

y_axis_labels=['A1','A15','A7','B2','B3','C11','C6','D14','D4','D13','E8','E5']
ax1.xaxis.tick_bottom()
ax1.set_xticklabels(table_pivot2.columns,rotation=40)
ax1.yaxis.tick_right()
ax1.set_yticklabels(y_axis_labels, rotation='horizontal')

sns.heatmap((pd.DataFrame(table_pivot2.sum(axis=0))).transpose(),
            ax=ax3,
            annot=True,
            fmt='g',
            cmap=cmap,
            cbar=False,
            xticklabels=False,
            yticklabels=False).set(xlabel='', ylabel='Totals')
sns.heatmap(pd.DataFrame(table_pivot2.sum(axis=1)),
            ax=ax2,
            annot=True,
            fmt='g',
            cmap=cmap,
            cbar=False,
            xticklabels=False,
            yticklabels=False).set(xlabel='', ylabel='', title='Totals');

Another heatmap here (no. of crimes per district/hours):





Hiç yorum yok:

Yorum Gönder