plot 方法绘制列表后移一位,而 scatter 则可以

The plot method plots the list shifted back by one, while scatter is ok

您好,以下代码表示前 10 个整数的立方体。 scatter 方法工作正常,plot 方法将所有内容向左移动一位。 轴在我看来是正确的。

我试图弄明白,但我不知道哪里出错了。 谢谢。

import matplotlib.pyplot as plt

n_values = range(1,11,1)
n_cubes = [n**3 for n in n_values]

fig, ax = plt.subplots()
ax.plot(n_cubes)
ax.scatter(n_values, n_cubes, c=n_cubes, cmap=plt.cm.Reds, s=20)
ax.axis([1, 12, 0, 1100])

print(n_cubes, n_values)

plt.style.use('seaborn')
plt.show()

如果您只用一个参数调用 ax.plot(),它将生成自己的 x-axis 值。在 python 中,这些从零开始。所以,一切都转移了。 所以,你需要像这样调用函数:

ax.plot(n_values, n_cubes)