【问题标题】:Plot multiple lines froma list of coordinates in one figure从一个图中的坐标列表中绘制多条线
【发布时间】:2020-01-08 10:46:13
【问题描述】:

我一直在尝试使用 Matplotlib.pyplot.figure 在一个图中绘制多条线。 我可以使用pyl.plot 来做到这一点,但我想使用 pyplot.fig 来做到这一点

pyl.plot 代码:

import matplotlib.pylab as pyl

obstListX = [[90, 90, 100, 100, 200, 200, 240, 240], [300, 300, 308, 308, 300, 300, 330, 330], [170, 206, 226, 260]]
obstListY = [[90, 112, 112, 100, 100, 125, 125, 121], [145, 80, 80, 70, 70, 50, 50, 135], [70, 69, 91, 90]]

pyl.plot(x, y, 'r')
for x, y in zip(obstListX, obstListY):
    pyl.plot(x, y, 'r',color='b')
pyl.xlim(0, 480)
pyl.ylim(0, 320)
pyl.grid(which='major', linestyle='-', linewidth='0.5', color='grey')
pyl.show()

结果:

当我想使用 mpld3 将图形保存到 HTML 时,如何将线条绘制为一个图形(如上图)? 在下面的代码中,3 行作为 3 个不同的数字出现,就像它在 for 循环中一样,只有最后一个被转换为 html。

import matplotlib.pylab as pyl
import mpld3
from mpld3 import plugins, utils
obstListX = [[90, 90, 100, 100, 200, 200, 240, 240], [300, 300, 308, 308, 300, 300, 330, 330], [170, 206, 226, 260]]
obstListY = [[90, 112, 112, 100, 100, 125, 125, 121], [145, 80, 80, 70, 70, 50, 50, 135], [70, 69, 91, 90]]
for x, y in zip(obstListX, obstListY):
    fig, ax = pyl.subplots()
    ax.plot(x, y)

ax.set(xlabel='time (s)', ylabel='voltage (Label)',
       title='Title goes here')
ax.grid()

mpld3.save_html(fig, "/home/pi/Desktop/fig1.html", template_type='general')
pyl.show()

请注意,obstListX ,obstListY 是 x 和 y 坐标列表的列表。

【问题讨论】:

    标签: python matplotlib plot figure fig


    【解决方案1】:

    我将 fig, ax = pyl.subplots() 移出 for 循环,它起作用了。

    fig, ax = pyl.subplots()
    for x, y in zip(obstListX, obstListY):
        ax.plot(x, y)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多