1

I have a dataframe df containing string values

a b c d

b c d a

I would like to produce a pdf plot based on the data in the df, with 4 cols and 2 rows, where each cell in the table plot has a color depending on the value in the df, a=blue, b=red, c=yellow, d=green.

Like this

Thanks in advance!

3
  • By "table" you mean a plt.table or a subplot grid? I think you would benefit from describing much more in detail the desired output and also what other resources have not helped you achieve what you want. Commented Nov 13, 2018 at 13:20
  • @ImportanceOfBeingErnest I added a picture of what I am looking for :) Commented Nov 13, 2018 at 13:36
  • I would probably map the characters to numbers first, then plot an imshow plot of the data. You will need a custom colormap with the 4 colors in it. Commented Nov 13, 2018 at 13:40

1 Answer 1

1

You can do in this way:

from matplotlib import colors as c

color_map = {'a':1,'b':2,'c':3, 'd':4}
cMap = c.ListedColormap(['g','b','y','r'])
df = df.replace(color_map)
fig, ax = plt.subplots()
ax.pcolor(df,cmap=cMap)
plt.show()

enter image description here

And If you want to remove the ticks, add plt.xticks([]) and plt.yticks([])

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.