在 Python 中的同一图表中绘制列表列表
Plotting list of lists in a same graph in Python
我正在尝试绘制 (x,y)
,其中 y = [[1,2,3],[4,5,6],[7,8,9]]
。
说,len(x) = len(y[1]) = len(y[2])
..
y 的长度由用户输入决定。我想在同一张图中绘制 y 的多个图,即 (x, y[1],y[2],y[3],...)
。当我尝试使用循环时,它说 dimension error
.
我也试过:plt.plot(x,y[i] for i in range(1,len(y)))
我该如何绘制?请帮忙。
for i in range(1,len(y)):
plt.plot(x,y[i],label = 'id %s'%i)
plt.legend()
plt.show()
假设 x 有一些示例值,下面是可以为您提供所需输出的代码。
import matplotlib.pyplot as plt
x = [1,2,3]
y = [[1,2,3],[4,5,6],[7,8,9]]
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(y[0])):
plt.plot(x,[pt[i] for pt in y],label = 'id %s'%i)
plt.legend()
plt.show()
假设:x
和 y
中的任何元素长度相同。
这个想法是逐个读取元素以构建列表 (x,y[0]'s)
、(x,y[1]'s)
和 (x,y[n]'s
.
已编辑:如果 y
包含更多列表,请调整代码。
下面是我为这个案例得到的情节:
使用 for 循环生成绘图并在 for 循环之后使用 .show()
方法。
import matplotlib.pyplot as plt
for impacts in impactData:
timefilteredForce = plt.plot(impacts)
timefilteredForce = plt.xlabel('points')
timefilteredForce = plt.ylabel('Force')
plt.show()
impactData 是一个列表列表。
我正在尝试绘制 (x,y)
,其中 y = [[1,2,3],[4,5,6],[7,8,9]]
。
说,len(x) = len(y[1]) = len(y[2])
..
y 的长度由用户输入决定。我想在同一张图中绘制 y 的多个图,即 (x, y[1],y[2],y[3],...)
。当我尝试使用循环时,它说 dimension error
.
我也试过:plt.plot(x,y[i] for i in range(1,len(y)))
我该如何绘制?请帮忙。
for i in range(1,len(y)):
plt.plot(x,y[i],label = 'id %s'%i)
plt.legend()
plt.show()
假设 x 有一些示例值,下面是可以为您提供所需输出的代码。
import matplotlib.pyplot as plt
x = [1,2,3]
y = [[1,2,3],[4,5,6],[7,8,9]]
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(y[0])):
plt.plot(x,[pt[i] for pt in y],label = 'id %s'%i)
plt.legend()
plt.show()
假设:x
和 y
中的任何元素长度相同。
这个想法是逐个读取元素以构建列表 (x,y[0]'s)
、(x,y[1]'s)
和 (x,y[n]'s
.
已编辑:如果 y
包含更多列表,请调整代码。
下面是我为这个案例得到的情节:
使用 for 循环生成绘图并在 for 循环之后使用 .show()
方法。
import matplotlib.pyplot as plt
for impacts in impactData:
timefilteredForce = plt.plot(impacts)
timefilteredForce = plt.xlabel('points')
timefilteredForce = plt.ylabel('Force')
plt.show()
impactData 是一个列表列表。