Search code examples
pythonnumpymatplotlibgraphingyaxis

How to change the scale of my matplotlib y axis to y^2?


Instead of the y-axis being 1,2,3,4... Is there a way to make it 1,4,9,16...? Can't find anything on the website.

Here's my code:

import matplotlib.pyplot as plt
import numpy as np

x = np.array(range(100))
y = 2 * np.pi * (x/9.81)**(1/2)

plt.figure()

plt.plot(x, y)
plt.title("Graph")
plt.grid(True)

plt.show()

Solution

  • IIUC, you just want to replace the tick labels. In that case, you can do the following.

    plt.plot(x, y)
    plt.title("Graph")
    plt.grid(True)
    
    yticks = np.arange(1,20,1)
    
    plt.yticks(yticks, yticks**2)
    
    plt.show()
    

    enter image description here