1

I would like to show the index of a matrix on a matplotlib figure.

Let say that our matrix is square with index ['A','B','C']. I would like to see something like:

A B C
A fig1 fig2 fig3
B fig4 fig5 fig6
C fig7 fig8 fig9

My code is the following:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import itertools

string = 'ABC'
combinations = itertools.combinations(string, 2)
data = dict()
for combination in combinations:
    begin = np.random.random()
    data[combination] = np.linspace(begin, begin+1,20).reshape(10,2)

fig = plt.figure(figsize = (15,15))

all_plot_positions = range(1, len(string) * len(string)+1)
plot_positions_used = []
for key in data:
    X, Y = np.meshgrid(data[key][:,0], data[key][:,1])
    Z = 2*X + Y**2

    i, j = string.index(key[0]), string.index(key[1])

    ax = plt.subplot2grid((len(string), len(string)), (i, j))
    CS = ax.contourf(X, Y, Z)      
    cbar = plt.colorbar(CS)
    
    ax = plt.subplot2grid((len(string), len(string)), (j, i))
    CS = ax.contourf(X, Y, Z)
    cbar = plt.colorbar(CS)

for i in range(len(string)):
    ax = plt.subplot2grid((len(string), len(string)),( i,i))
    ax.set_xticks([])
    ax.set_yticks([])


fig.subplots_adjust(hspace = 0.5, wspace = 0.5)
fig.savefig('fig.pdf')

And my output:
enter image description here

As you see, There is not index.

In addition (but this could be a second question), I also would like to show only one colorbar that represent all the graphs.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    Check [this](https://stackoverflow.com/questions/25812255/row-and-column-headers-in-matplotlibs-subplots) – Bob Jan 07 '22 at 13:31

0 Answers0