简体   繁体   English

Matplotlib 绘制了两次图,但 plt.plot 只被调用一次?

[英]Matplotlib is plotting plots twice, but plt.plot is only called once?

I'm making a simple plot with three separate lines, and for each line I call pyplot.plot a single time.我正在用三个单独的行制作一个简单的图,对于每一行,我都调用 pyplot.plot 一次。 For some reason, each line is being plotted twice, with the duplicate line for each plot not representing the data.出于某种原因,每条线都被绘制了两次,每个图的重复线不代表数据。

这是代码和输出的图像。

As you can see, the three lines are properly plotted, but the legend shows two of each line.如您所见,三条线已正确绘制,但图例显示每条线有两条。 Also, at the bottom of the plot, you can see the failed duplicates all following the same path.此外,在图的底部,您可以看到失败的重复项都遵循相同的路径。

Any help is much appreciated.任何帮助深表感谢。

Check the dimensions of your variables if they are nx2 then you will get 2 lines per plot call.检查变量的维度,如果它们是 nx2,那么每个绘图调用将得到 2 行。 Refer to the pyplot plot documentation here.请参阅 此处pyplot 图文档。 Specifically where it talks about:具体来说,它谈到:

Plotting multiple sets of data.绘制多组数据。

Alternatively, if your data is already a 2d array, you can pass it directly to x, y.或者,如果您的数据已经是一个二维数组,您可以将其直接传递给 x, y。 A separate data set will be drawn for every column.将为每一列绘制一个单独的数据集。

Also it is not very useful to post pictures of code here.此外,在此处发布代码图片也不是很有用。 You should add the actual code to the question as it is much more usefull.您应该将实际代码添加到问题中,因为它更有用。

Example:例子:

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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM