python - Matplotlib 正在绘制两次图,但 plt.plot 只被调用一次?

标签 python matplotlib plot data-visualization

我正在制作一个包含三行的简单绘图,对于每一行,我都调用了一次 pyplot.plot。出于某种原因,每条线都被绘制了两次,每个图的重复线不代表数据。

Here's an image of the code and output.

如您所见,三行已正确绘制,但图例显示每行两条。此外,在图的底部,您可以看到失败的重复项都遵循相同的路径。

非常感谢任何帮助。

最佳答案

检查变量的维度,如果它们是 nx2,那么每次绘图调用将得到 2 行。引用pyplot plot documentation here.具体说到哪里:

绘制多组数据。

Alternatively, if your data is already a 2d array, you can pass it directly to x, y. A separate data set will be drawn for every column.

此外,在这里发布代码图片也不是很有用。您应该将实际代码添加到问题中,因为它更有用。

例子:

import matplotlib.pyplot as plt
import numpy as np
test1 = np.random.randn(10,2)
test2 = np.random.randn(10,2)
plt.plot(test1 ,color='blue',label='test1')
plt.plot(test2 ,color='green',label='test2')
plt.legend(loc='upper left')

enter image description here

关于python - Matplotlib 正在绘制两次图,但 plt.plot 只被调用一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54606873/

相关文章:

python - 打印用于登录 Python 的时间戳

python - 如何从 Jupyter 实验室笔记本下载 .py 文件?

python - 使用 csv 文件中的 matplotlib 在 python 中绘制时间序列图

r - 一种缓存 ggplot2 图的方法

python - Sphinx 将段落标签添加到无序列表项

Python 子进程将调用输出存储到文件

python - 应用 pandas df 中所有列的分布

python - 在 Matplotlib 极坐标图上设置径向轴

python - 将 plot() 添加到 imshow() 时避免更改图形大小

plot - 更改 seaborn corrplot 中的字体大小