0

I have a dataframa as below:

 Symbol Change Yrows Xcols 0 ADANIPORTS 7.15 1 1 1 BAJAJ-AUTO 3.04 1 2 2 HINDUNILVR 3.00 1 3 3 M&M -2.69 2 1 4 UPL -2.92 2 2 5 NTPC -3.15 2 3 6 JSWSTEEL -3.30 3 1 7 COALINDIA -3.38 3 2 8 ONGC -3.48 3 3 

Here my Dataframe consists of 9 rows hence below code runs fine. But when dataframe size reduces to 8 it gives reshape error.

I need help on below 2 conditions.

  1. I want to plot the heat map without that 9th row when not available.
  2. How to automatically reshape of HeatMap table with available data of dataframe.

My Code as below:

df = pd.read_csv("Heatmap_data.csv") Yrows = [1,1,1,2,2,2,3,3,3] Xcols = [1,2,3,1,2,3,1,2,3] df = df.assign(Yrows=Yrows, Xcols=Xcols) symbol = ((np.asarray(df['Symbol'])).reshape(3, 3)) perchange = ((np.asarray(df['Change'])).reshape(3, 3)) result = df.pivot(index='Yrows', columns='Xcols', values='Change') labels = (np.asarray(["{0} \n {1:.2f}%".format(symb, value) for symb, value in zip(symbol.flatten(), perchange.flatten())])).reshape(3, 3) fig, ax = plt.subplots(figsize=(13, 7)) title = "Heat Map" plt.title(title, fontsize=18) ttl = ax.title ttl.set_position([0.5, 1.05]) ax.set_xticks([]) ax.set_yticks([]) ax.axis('off') sns.heatmap(result, annot=labels, fmt="", cmap='RdYlGn', linewidths=0.40, ax=ax) plt.show() 

I want my desired out put with dataframe of 8 rows as below:

output

1 Answer 1

1

This seems to work out of the box if you use pandas to reshape. This is because a pandas pivot will insert NaN values on missing entries, whereas numpy’s reshape method does not allow to reshape if there are not enough values (i.e. you can’t reshape an array of 8 elements into a 3x3 table).

Let’s try this with 8 entries:

>>> df Symbol Change 0 ADANIPORTS 7.15 1 BAJAJ-AUTO 3.04 2 HINDUNILVR 3.00 3 M&M -2.69 4 UPL -2.92 5 NTPC -3.15 6 JSWSTEEL -3.30 7 COALINDIA -3.38 

First, assign rows and columns as you did, so we can use pivot to make the 2D arrays:

>>> df = df.assign(row = np.arange(len(df)) // 3, col = np.arange(len(df)) % 3) >>> df Symbol Change row col 0 ADANIPORTS 7.15 0 0 1 BAJAJ-AUTO 3.04 0 1 2 HINDUNILVR 3.00 0 2 3 M&M -2.69 1 0 4 UPL -2.92 1 1 5 NTPC -3.15 1 2 6 JSWSTEEL -3.30 2 0 7 COALINDIA -3.38 2 1 >>> change = df.pivot('row', 'col', 'Change') >>> change col 0 1 2 row 0 7.15 3.04 3.00 1 -2.69 -2.92 -3.15 2 -3.30 -3.38 NaN 

For labels, let’s generate them from the series first and them pivot them in the same way:

>>> df['Label'] = df['Symbol'].str.cat(df['Change'].apply('{:.2f}%'.format), sep='\n') >>> labels = df.pivot('row', 'col', 'Label') >>> labels col 0 1 2 row 0 ADANIPORTS\n7.15% BAJAJ-AUTO\n3.04% HINDUNILVR\n3.00% 1 M&M\n-2.69% UPL\n-2.92% NTPC\n-3.15% 2 JSWSTEEL\n-3.30% COALINDIA\n-3.38% NaN 

Now we can use your plot call and it all works as intended:

>>> sns.heatmap(change, annot=symbol, fmt='', cmap='RdYlGn', linewidths=0.40) 

seaborn heatmap

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. Is there any way to bold few selected stocks with their percentage value ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.