[Python] ValueError: x and y must have same first dimension, but have shapes (100,) and ((1,))

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to plot a simple function using python, numpy and matplotlib but when I execute the script it returns a ValueError described in the title.

    This is my code:

    """Geometrical interpretation: In Python, plot the function y = f(x) = x**3 − (1/x)
    and plot its tangent line at x = 1 and at x = 2."""

    import numpy as np
    import matplotlib.pyplot as plt


    def plot(func):
    plt.figure(figsize=(12, 8))
    x = np.linspace(-100, 100, 100)
    plt.plot(x, func, '-', color='pink')
    plt.show()
    plt.close()


    plot(lambda x: x ** 3 - (1 / x))


    Please send this beginer some help :)

    Continue reading...

Compartilhe esta Página