1

I want to convert my input and output vectors into an array and plot with colormap which works fine. See example below:

x = np.round(np.array([-1, 0,     1,  2, 3, 2, 0, 0, 1, 2, 3]))
y = np.round(np.array([-1, 1.2, -3.1, 2, 3, 2, 1, 1, 1, 3, 3]))

xrange = np.arange(np.min(x),np.max(x) + 1)
yrange = np.arange(np.min(y),np.max(y) + 1)
a = np.zeros((len(yrange), len(xrange)))
for i in range(len(x)):
    a[yrange == y[i],xrange == x[i]] = a[yrange == y[i],xrange == x[i]] + 1

fig, ax = plt.subplots()
im = ax.imshow(a,cmap='Wistia',origin="lower")
for i in range(len(yrange)):
    for j in range(len(xrange)):
        text = ax.text(j, i, str(int(a[i, j])),
                       ha="center", va="center", color="k")
ax.set(xlabel = 'Input', ylabel = 'Output')
fig.tight_layout()
plt.show()

Which gives me this:

enter image description here

The only problem is that I like to have a column-based colormap. Meaning that for example in the first column I want 1 to be treated as the highest number and have the same color as 3 in the second column, and this goes for all columns.

1 Answer 1

0

A simple way is to convert a to DataFrame and use the below function to create a column-wise cmap.

import pandas as pd
df = pd.DataFrame(a)

df.style.background_gradient(cmap="Wistia")

With this, you can have more control over specific columns as well by using the "subset" parameter.

3
  • Thanks for your response. Can I please have the codes that do this? I am not familiar with pandas dataframes.
    – Rash
    Oct 28, 2021 at 4:44
  • I am adding it as edit to my current answer Oct 28, 2021 at 7:43
  • Thanks, I'd appreciate it. Do I need to do anything to plot? The code runs with no error but there is no plot.
    – Rash
    Oct 28, 2021 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.