陶哲轩:不懂数学别想靠ChatGPT飞升!
数学不好的人,其实AI的帮助可能也没那么大
pip install matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Define the range for x
x = np.linspace(0, 1, 400)
# Define the functions
functions = [lambda x: x, lambda x: x**2, lambda x: x**3, lambda x: x**4]
# Create a figure and axis
fig, ax = plt.subplots()
line, = ax.plot(x, functions[0](x))
# Set axis labels and title
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
ax.set_title('Animated Functions')
# Initialize the animation function
def init():
line.set_ydata([np.nan] * len(x))
return line,
# Update the animation function
def update(frame):
line.set_ydata(functions[frame](x))
ax.set_title(f'Function: $x^{frame+1}$')
return line,
# Create the animation
ani = animation.FuncAnimation(
fig, update, frames=len(functions), init_func=init, blit=True, repeat=True, interval=1000
)
# Save the animation as a gif
ani.save('animated_functions.gif', writer='imagemagick')
plt.show()
pip install matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Define the range for x
x = np.linspace(0, 1, 400)
# Define the functions
functions = [lambda x: x, lambda x: x**2, lambda x: x**3, lambda x: x**4]
# Create a figure and axis
fig, ax = plt.subplots()
line1, = ax.plot(x, functions[0](x), label='$x$')
line2, = ax.plot(x, functions[1](x), label='$x^2$')
# Set axis labels and title
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
ax.legend()
ax.set_title('Animated Functions')
# Initialize the animation function
def init():
line1.set_ydata([np.nan] * len(x))
line2.set_ydata([np.nan] * len(x))
return line1, line2
# Update the animation function
def update(frame):
line1.set_ydata(functions[frame](x))
line2.set_ydata(functions[frame+1](x))
ax.legend([f'$x^{frame+1}$', f'$x^{frame+2}$'])
return line1, line2
# Create the animation
ani = animation.FuncAnimation(
fig, update, frames=len(functions)-1, init_func=init, blit=True, repeat=True, interval=1000
)
# Save the animation as a gif
ani.save('animated_double_functions.gif', writer='imagemagick')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Define the range for x
x = np.linspace(0, 1, 400)
# Define the functions
functions = [lambda x: x, lambda x: x**2, lambda x: x**3, lambda x: x**4]
# Create a figure and axis
fig, ax = plt.subplots()
line1, = ax.plot(x, functions[0](x), label='$x$')
line2, = ax.plot(x, functions[1](x), label='$x^2$')
# Set axis labels and title
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
ax.set_title('Animated Functions')
# Initialize the animation function
def init():
line1.set_ydata([np.nan] * len(x))
line2.set_ydata([np.nan] * len(x))
return line1, line2
# Update the animation function
def update(frame):
line1.set_ydata(functions[frame](x))
line2.set_ydata(functions[frame+1](x))
ax.legend([f'$x^{frame+1}$', f'$x^{frame+2}$'])
return line1, line2
# Create the animation
ani = animation.FuncAnimation(
fig, update, frames=len(functions)-1, init_func=init, blit=True, repeat=True, interval=1000
)
# Save the animation as a gif
ani.save('animated_double_functions.gif', writer='imagemagick')
plt.show()
ChatGPT解释道:更新函数中的ax.legend()调用,就可以清除现有图例,并且使用每帧的新标签进行更新了。这样就可以确保,图例反映的是当前显示函数的功能。
pip install matplotlib
import numpy as np
import matplotlib.pyplot as plt
# Define the numerical values for the x and y positions
x_values = [1, 2, 3]
y_values = [1, 4, 9]
# Define the corresponding string labels for x and y axes
x_labels = ["Today", "Next week", "Next year"]
y_labels = ["Beginner", "Intermediate", "Expert"]
# Define the function to be plotted (y = x^2)
x = np.linspace(1, 3, 100)
y = x**2
# Create a figure and axis
fig, ax = plt.subplots()
# Plot the function
ax.plot(x, y, label='$y=x^2$')
# Set the x and y axis labels
ax.set_xlabel('Time')
ax.set_ylabel('Level')
# Set the x and y axis tick positions and labels
ax.set_xticks(x_values)
ax.set_xticklabels(x_labels)
ax.set_yticks(y_values)
ax.set_yticklabels(y_labels)
# Add a legend
ax.legend()
# Set the title
ax.set_title('Parabola $y=x^2$ with Custom Axis Labels')
# Display the plot
plt.show()
网友:图表存在「幻觉」
「资深用户」陶哲轩
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章