Skip to content

Instantly share code, notes, and snippets.

@git-shogg
Last active February 8, 2023 09:59
Show Gist options
  • Save git-shogg/14df6b7010191d76a986d8d333fd51e3 to your computer and use it in GitHub Desktop.
Save git-shogg/14df6b7010191d76a986d8d333fd51e3 to your computer and use it in GitHub Desktop.
def sns_heatmap(dataframe, key_column, key_column_format, chart_title, c_pos_neg):
# Build out heatmap things
dataframe['Position'] = range(1,len(dataframe) + 1)
dataframe['y'] = [(x//10 + 1 if x%10 != 0 else (x//10)) for x in dataframe['Position']]
dataframe['x'] = [(x%10 if x%10 != 0 else 10) for x in dataframe['Position']]
# most_bought['normalized_dollars_bought_sold'] = (most_bought['dollars_bought_sold'] - most_bought['dollars_bought_sold'].min()) / (most_bought['dollars_bought_sold'].max()-most_bought['dollars_bought_sold'].min())
# most_bought['natural_log'] = np.log(most_bought['normalized_dollars_bought_sold'])
pivot_df = dataframe.pivot(index='y', columns='x', values=key_column)
ticker_labels = np.asarray(dataframe['Ticker']).reshape((10,10))
key_column_labels = np.asarray(dataframe[key_column]).reshape((10,10))
if key_column_format == '$M':
annot_labels = np.asarray(["{} \n ${:,.0f}M".format(ticker, dbs/1000000) for ticker, dbs in zip(ticker_labels.flatten(), key_column_labels.flatten())]).reshape((10,10))
elif key_column_format == '$B':
annot_labels = np.asarray(["{} \n ${:,.0f}B".format(ticker, dbs/1000000000) for ticker, dbs in zip(ticker_labels.flatten(), key_column_labels.flatten())]).reshape((10,10))
elif key_column_format == '#T':
annot_labels = np.asarray(["{} \n {:,.0f} Thousand".format(ticker, dbs/1000) for ticker, dbs in zip(ticker_labels.flatten(), key_column_labels.flatten())]).reshape((10,10))
elif key_column_format == '%':
annot_labels = np.asarray(["{} \n {:.2f} %".format(ticker, kcv) for ticker, kcv in zip(ticker_labels.flatten(), key_column_labels.flatten())]).reshape((10,10))
else:
annot_labels = np.asarray(["{}".format(ticker) for ticker in ticker_labels.flatten()]).reshape((10,10))
fig, ax = plt.subplots(1, 1, figsize = (50, 50), dpi=300)
cmap_val = 'BuGn' if c_pos_neg == 'pos' else 'OrRd_r'
sns.heatmap(pivot_df, annot=annot_labels, annot_kws={"size":5}, fmt="", cmap=cmap_val, linewidths=0.25, square=False, yticklabels=False, xticklabels=False, ax=ax)
# sns.set(font_scale=1.8)
ax.set_ylabel('')
ax.set_xlabel('')
ax.set_title(chart_title)
plt.show()
return
# --- Build out heatmaps ---
most_bought = ticker_dataframe[:100].reindex()
sns_heatmap(most_bought, 'dollars_bought_sold', '$M', 'Most Bought Asset by Dollar Value', c_pos_neg='pos')
most_sold = ticker_dataframe[-100:].reindex()
sns_heatmap(most_sold, 'dollars_bought_sold', '$M', 'Most Sold Asset by Dollar Value', c_pos_neg='neg')
most_bought_sold_by_allocation = ticker_dataframe.sort_values(by=['avg_bought_sold_weight_by_portfolio_pct'], ascending=False)[:100]
sns_heatmap(most_bought_sold_by_allocation, 'avg_bought_sold_weight_by_portfolio_pct', '%', 'Most Bought Asset by Portfolio Allocation', c_pos_neg='pos')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment