1

I have two arrays which I am plotting on a separate grid of nodes using colors. One represents some clusters and the other some other values I'll refer to as my features. Example code:

import numpy as np
import matplotlib.pyplot as plt

clusters = np.array([[0,2,1], [0,3,1], [3,3,1]]) # make cluster data
features= np.array([[0,0.4,0.7], [0.1,0.3,0.7], [0.5,0.4,0.8]]) # make data of features

# plot clusters
plt.figure()
plt.pcolor(clusters, cmap='jet') # color in nodes
plt.colorbar(ticks=[i for i in range(0, np.amax(clusters)+1)]) # make colorbar legend per cluster
plt.show()

# plot feature grid
plt.figure()
plt.pcolor(features, cmap='bone', vmin=0, vmax=1) # color in nodes
plt.colorbar() # make colorbar legend
plt.show()

In this second grey coloured grid with my feature data I would like to display the border between my clusters by means of gridlines. The expected result would look something like this, where the red line indicates the borders between the clusters:

Required end result

Is there any way to automatically draw these gridlines using the data of the cluster array? Any help would be appreciated!

Bas R
  • 175
  • 7

1 Answers1

1

This works:

vlines, hlines = [], []
# loop over array with clusters to obtain positions of vertical and horizontal lines
for row_idx, row in enumerate(clusters): # loop over array rows
    for col_idx, col in enumerate(row): # loop over array columns per row
        if col_idx+1 < clusters.shape[1]: # skip final column, has no right-side neighbouring node
            # save tuple if it requires a vertical line indicating a different cluster right of the node
            if clusters[row_idx, col_idx] != clusters[row_idx, col_idx+1]: vlines.append((row_idx, col_idx+1))
        if row_idx+1 < clusters.shape[0]: # skip final row, has no bottom neighbouring node
            # save a tuple if it requires a horizontal line indicating a different cluster below the node
            if clusters[row_idx, col_idx] != clusters[row_idx+1, col_idx]: hlines.append((row_idx+1, col_idx))

# make features plot
plt.figure()
plt.pcolor(features, cmap='bone', vmin=0, vmax=1) # color in nodes
plt.colorbar() # make colorbar legend
for vline in vlines: # plot all vertical lines
    plt.axvline(vline[1], ymin=vline[0]/clusters.shape[0], ymax=vline[0]/clusters.shape[0] + 1/clusters.shape[0], color='red')
for hline in hlines: # plot all horizontal lines
    plt.axhline(hline[0], xmin=hline[1]/clusters.shape[0], xmax=hline[1]/clusters.shape[0] + 1/clusters.shape[0], color='red')
plt.show()
Bas R
  • 175
  • 7