[英]Matplotlib 2 y values against each X
我有 2 個 RMSE 值,我想使用matplotlib
庫在y
軸上針對我持有的每個x
值進行 plot。 這是我目前的嘗試:
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 and y must be the same size
。 我找不到任何這樣的例子(或任何其他合適的圖表類型)。 有比將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()
我建議您將rmses
計算為 2×N 列表列表,但您可以使用 Python 習慣用法zip(*listoflists)
轉置 N×2 列表列表
正確初始化后
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();
可能這樣的事情可以工作:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.