Python Matplotlib针对每个X的2个y值
我有两个RMSE值,我想使用Python Matplotlib针对每个X的2个y值,python,numpy,matplotlib,Python,Numpy,Matplotlib,我有两个RMSE值,我想使用matplotlib库,在y轴上绘制我持有的每个x值。 以下是我目前的尝试: def plotRMSE(rmses): #Both values in each subarray must be plotted on the y axis at the same x point #rmses = [[15, 30], [10, 25], [18.45, 23.1], [3.2, 6.36], [2.4, 1.86e-10], [1.627, 3.3e-
matplotlib
库,在y
轴上绘制我持有的每个x
值。
以下是我目前的尝试:
def plotRMSE(rmses):
#Both values in each subarray must be plotted on the y axis at the same x point
#rmses = [[15, 30], [10, 25], [18.45, 23.1], [3.2, 6.36], [2.4, 1.86e-10], [1.627, 3.3e-10]]
x = [0, 1, 2, 3, 5, 10]
for i in range(len(x)):
plt.scatter(x[i], rmses[i])
plt.show()
这当然会返回ValueError:x和y的大小必须相同。我找不到任何这样的例子(或任何其他合适的图形类型)。有比将x[i]
的每个值与每个子阵列的两个值配对更好的解决方案吗
编辑:
我太傻了。我可以在两个独立的plt.scatter()
调用中使用rmses[I][0]
和rmses[I][1]
。这样做将导致不同的彩色点(我不希望这样;我希望每个[0]
和[1]
都用相同的颜色和图例标签表示),因此下面是我完成的代码:
def plotRMSE(rmses):
x = [0, 1, 2, 3, 5, 10]
plt.xticks(x)
for i in range(len(x)):
plt.scatter(x[i], rmses[i][0], c="#555FFF")
plt.scatter(x[i], rmses[i][1], c="#CC0000")
plt.legend(["Train RMSE", "Test RMSE"])
plt.show()
干杯
def plotRMSE(rmses):
#Both values in each subarray must be plotted on the y axis at the same x point
x = [0, 1, 2, 3, 5, 10]
for i in range(len(x)):
print(x[i])
print(rmses[i][0])
plt.scatter(x[i], rmses[i][0])
plt.scatter(x[i], rmses[i][1])
plt.show()
rmses = [[15, 30], [10, 25], [18.45, 23.1], [3.2, 6.36], [2.4, 1.86e-10], [1.627, 3.3e-10]]
plotRMSE(rmses)
这是你想要的吗
这似乎是在不改变数据格式的情况下最简单的解决方案,但是我通常会有两个响应(y)向量,y_1和y_2。
那么就做:
plt.scatter(x, y_1)
plt.scatter(x, y_2)
plt.show()
也许类似的方法可以奏效:
def plotRMSE(rmses):
x = [0, 1, 2, 3, 5, 10]
for i in range(len(x)):
for j in range(len(rmses[i])):
plt.scatter(x[i], rmses[i][j])
plt.show()
我建议您将rmses
计算为2×N列表,但是
可以使用Python习语zip(*listoflists)
在正确初始化之后
x = [0, 1, 2, 3, 5, 10]
rmses = [[15, 30],
[10, 25],
[18.45, 23.1],
[3.2, 6.36],
[2.4, 1.86e-10],
[1.627, 3.3e-10]]
这相对容易
for rms in zip(*rmses): plt.scatter(x, rms)
添加一个传奇稍微复杂一些,但没有那么复杂
for rms, lbl in zip(zip(*rmses),('first', 'second')):
plt.scatter(x, rms, label=lbl)
plt.legend();
您也可以发布一些示例输入数据吗?它在代码注释中,为什么不将其作为实际源代码放入?使用下面的Augstin解决方案,或者将行更改为plt.scatter([x[i],x[i]],rmses[i])
。