Instagram
youtube
Facebook
Twitter

Control Axes Limits and Aspect Ratio

Description:
This code sets custom axis limits and controls the aspect ratio to adjust how the plot is displayed.

Code Explanation:

  • plt.xlim() and plt.ylim() are used to manually set the range for X and Y axes.

  • This helps focus on specific parts of the data.

  • plt.gca().set_aspect() controls how the x and y scales appear:

    • 'auto' = automatic scaling.

    • 'equal' = same scale for both axes (square units).

  • Useful for keeping plots balanced and visually accurate, especially when comparing shapes or curves.


    Program:

    import matplotlib.pyplot as plt
    
    # Sample data
    x = [0, 1, 2, 3, 4, 5]
    y = [0, 1, 4, 9, 16, 25]
    
    # Plotting the data
    plt.plot(x, y, label='y = x²', color='purple')
    plt.title('Parabola Curve')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.legend()
    
    # Set limits for x and y axes
    plt.xlim(0, 6)
    plt.ylim(0, 30)
    
    # Set aspect ratio (equal makes x and y scale equally)
    plt.gca().set_aspect('auto')  # Use 'equal' for square scaling
    
    # Show the plot
    plt.grid(True)
    plt.show()
    


    Output: