【发布时间】:2019-03-26 03:20:14
【问题描述】:
我有一个嵌套列表和一个函数:
a = [[2,3,4],[3,4,5],[5,7]]
def triple(a):
return (a**2)+3
我尝试绘制子列表中的每个点,例如第一个值连接到第二个,第二个值连接到第三个,然后在子列表中的最后一个值处停止。 x是子列表中的值,y是triple(y for y in sublist)
for g in a:
for t in g:
plt.scatter(t, triple(t))
for g in a:
for o in range(len(g)):
plt.plot([g[o],g[o+1]], [triple(g[o]), triple(g[o+1])], color = 'red')
plt.show()
我试过了,但没有得到我想要的输出。
如何绘制a 中的每个子列表并获得三种不同颜色的线条?
【问题讨论】:
-
这是否实现了你想要的:
for sublist in a: plt.plot(sublist, [triple(s) for s in sublist], 'o-')?
标签: python list loops matplotlib