Fabien Tribel
Fabien Tribel

Reputation: 21

plotting a numpy array : setting axis scale

I have some troubles trying to keep a well-scaled axis when plotting an image from a numpy array.

Here is my code:

def graphique(pixelisation):    
Couleur = {1 : "Vert", 2 : "Orange", 3 : "Rouge"}    
for c in range(1,2):
       liste = np.zeros((int(60//pixelisation),int(150//pixelisation)))
       for d in range(int(150//pixelisation)):
           for v in range(int(60//pixelisation)):
               my_input = {
                   "Distance" : d*pixelisation,
                   "Vitesse" :  v*pixelisation,
                   "Couleur" : c
                           }
               my_output = {
                   "Acceleration" : 0.0
                           }
               liste[v,d] = system.calculate(my_input, my_output).get("Acceleration")
       image = plt.matshow(liste)
       plt.title('a=f(d,v), feu '+Couleur.get(c))
       plt.xlabel('Distance(m)')
       plt.xaxis(0,150)
       plt.xticks(range(0,150,10))
       plt.ylabel('Vitesse(km/h)')
       plt.yticks(range(0,60,10))
       plt.colorbar(image)
       plt.show()    
return liste

The problem is that I created a parameter curtailing the size of my array. But if I divide the size by 2, the x and y axes go from 0 to half normal range.

I made some research in matplotlib database, but found nothing (I actually don't understand all functions).

Thanks for the help !

EDIT : For example : A = np.zeros((10,10)) plt.matshow(A) plt.show()

will give me an image with axes going from 0 to 10.

Now if : A = np.zeros((100,100)) And if I want axes to still show a scale from 0 to 10, how do I do ?

Upvotes: 1

Views: 3139

Answers (1)

hitzg
hitzg

Reputation: 12701

You can set the axis scale by calling one of the following functions / methods:

  1. With the member functions of the axes object:

    ax.set_xlim(0, 10)
    ax.set_ylim(0, 10)
    

    where ax is the axes object (you could get the current one via ax = plt.gca() ).

  2. With the module function plt.axis

    plt.axis([0, 10, 0, 10])
    

You'll find more information in the documentation of matplotlib

Upvotes: 1

Related Questions