0

I have this piece of code that displays the policies for a robot. The polices are essentially colourful squares that represent a value. Right now the squares have a colour and I have placed in a colour bar to show what those colours represent. But I would like to have it so that each value that is represented by the colour of the square is on each square itself. How could I do this?

def show_policy(self, policy=None):
    plt.title("Policies for Robot")
    plt.imshow(self.policy)
    plt.colorbar()
    plt.show()
   
SQB
  • 9
  • 2
  • 1
    Do you mean you want to have text on the legend or do you want it on the image itself? If you want it on the legend this might help: https://stackoverflow.com/a/60870122/12337195 – asdf101 Apr 19 '21 at 18:33
  • Hi, I want it on the image itself! But thank you. – SQB Apr 20 '21 at 09:33
  • I saw the question was marked duplicate but the answer there was barely documented and not very relevant (text on square vs. box in annotations), maybe this can help you better: ```python data = np.array([[1, 20, 40, 50],[8, 30, 1, 60],[80, 49, 19, 5]]) plt.imshow(data) for i, row in enumerate(data): for j, value in enumerate(row): plt.text(j, i, value, ha="center", va="center", backgroundcolor="#FFFFFF50") plt.colorbar() ``` I know your image is probably not a 1D array, but iteration and plt.text are the important parts here – asdf101 Apr 20 '21 at 09:56
  • comments don't allow newlines apparently :/ – asdf101 Apr 20 '21 at 09:58

0 Answers0