Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a color for a value in Imshow

I am a beginner using Python graphs, and I have a question about the matplotlib.pyplot.imshow function. My goal is to model the expansion of an oil spill in the sea by using the method of the random walk in a grid made of a list of lists. Then, I use imshow to display the results so I have a map made of colors to see where there are more particles and where there are less. This part seems to work. The values go from 0 to 5000.

However, now I want to make a "coast" in my map so I've decided to put a list of n values -10 in the last list of the original map but I don't know how to make it different on the graph showed by imshow because the value -10 is in the same color of the value 0. The picture shows what imshow displays; The coast is not very noticeable at the bottom.

My python graph

For example it is possible to make the 'coast' brown?

Moreover, I think I used the colormap 'viridis' and I can't choose for the coast a value of -1000 for example because it will make all the other values in the same color because in a scale from -1000 to 5000, 0 and 100 for example will be close so will appear in the same color.

Tell me if I'm not very clear. Thank you vey much for your time and your help. MelindaP

My code is:

def propacote(c0,D,taille,n):
    """c0 est la concentration initiale, D le taux de particules qui changent de case, taille le nombre de cases par ligne du quadrillage, n le nombre de parcours de tout le tableau"""

    import random as rd

    carte=[[0 for k in range(taille)] for k in range(taille)]

    carte[0][0]=c0

    particule=[carte[0][0]]

    carte[-1]=[100 for cote in range(taille)] #coast creation

    for passage in range(n):

        for ligne in range(len(carte)):
            for case in range(len(carte[0])):

                    for k in range(int(D*carte[ligne][case])):

Then I use the random walk method for example the general case:

deplacement= rd.randint(1,4)

                            if deplacement==1: #moving down
                                carte[ligne][case]=carte[ligne][case]-1
                                carte[ligne+1][case]=carte[ligne+1][case]+1

                            elif deplacement==2: #move to the left
                                carte[ligne][case]=carte[ligne][case]-1
                                carte[ligne][case+1]=carte[ligne][case+1]+1

                            elif deplacement==3: #moving up
                                carte[ligne][case]=carte[ligne][case]-1
                                carte[ligne-1][case]=carte[ligne-1][case]+1

                            else:  #move to the left
                                carte[ligne][case]=carte[ligne][case]-1
                                carte[ligne][case-1]=carte[ligne][case-1]+1

Finally, I use imshow to display the graph:

#graph
    import numpy as np
    import matplotlib.pyplot as plt
    plt.figure()


    if passage==n-1:
        for cote in range(len(carte[-1])):

                                    if carte[-1][cote]!=100: #coast is impacted

                                        carte[-1][cote]=-20
    quadrillage = np.array(carte)
    print(quadrillage)


    plt.imshow(quadrillage,interpolation='none')
    plt.colorbar()
    plt.show()

return carte
like image 447
MelindaP Avatar asked Nov 06 '22 03:11

MelindaP


1 Answers

The sections TwoSlopeNorm and Custom normalization in the following link seem to be just what you are looking for

like image 142
tetrisforjeff Avatar answered Nov 14 '22 23:11

tetrisforjeff