Python:matplotlib.pyplot 不允许我同时绘制多条线

问题描述 投票:0回答:1

我正在尝试比较 python 中的四个数据,但是当我绘制它们时只显示一个

我希望所有 4 条线都被绘制出来,这样我就可以比较它们,但只有最后一条出现了

for i in range(0,49):
    plt.plot(t[i],order_parameter(y1[:,i]),'ro',label='complete')
    plt.plot(t[i],order_parameter(y2[:,i]),'go',label='erdos-renyi small no. of connections')
    plt.plot(t[i],order_parameter(y3[:,i]),'bo',label='erdos-renyi large no. of connections')
    plt.plot(t[i],order_parameter(y4[:,i]),'yo',label='watts-strogatz')
plt.ylabel('r, order parameter')
plt.legend(['complete','erdos renyi small','erdos renyi large','watts-strogatz'])
plt.show

之前我这样画的时候,所有的线都显示出来了,所以我不知道这次我做错了什么。其他人能看出我哪里出错了吗?

This is the graph I'm getting

谢谢!

python matplotlib
1个回答
0
投票

我不确定其余代码是什么样的,所以我可能会遗漏一些东西;但是,尝试:

import matplotlib.pyplot as plt
•
•
•
styles = ['ro', 'go', 'bo', 'yo']
labels = ['complete', 'erdos-renyi small no. of connections',
          'erdos-renyi large no. of connections', 'watts-strogatz']

for i in range(49):
    _y1 = order_parameter(y1[:,i])
    _y2 = order_parameter(y2[:,i])
    _y3 = order_parameter(y3[:,i])
    _y4 = order_parameter(y4[:,i])
    y_data = [_y1, _y2, _y3, _y4]
    for y, style, label in zip(y_data, styles, labels):
        plt.plot(t[i], y, style, label)

plt.ylabel('r, order parameter')
plt.legend(labels)
plt.show()

使用一些虚拟数据,这为我绘制了所有具有相应颜色的 y 数据 :)

我很确定这与在迭代中多次调用 plt.plot() 有关。

© www.soinside.com 2019 - 2024. All rights reserved.