python - Matplotlib 2 y 值针对每个 X
问题描述
我有 2 个 RMSE 值,我想使用库在y
轴上针对x
我持有的每个值绘制。matplotlib
这是我目前的尝试:
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]
与每个子数组的两个值配对更好的解决方案吗?
编辑:我很傻。我可以在两个单独的电话中使用rmses[i][0]
and 。这样做会导致不同的颜色点(我不想要;我希望每个点都用相同的颜色和图例标签表示),所以这是我完成的代码:rmses[i][1]
plt.scatter()
[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()
推荐阅读
- flutter - 从重启的 App Flutter 运行集成测试
- javascript - 如何将水平网格滚动到页面加载的特定位置 - 需要响应式解决方案
- ios - 在静态方法中快速设置委托
- php - (已解决)以编程方式更改 Yii2 ActiveForm/Widget Input 的值
- javascript - 在属性中组合两个字符串对象以在 .map 循环中使用
- variables - Mindstorms EV3 编程问题 - 颜色传感器和变量
- apache - 在 Apache 服务器上扫描文本文件以获取数据
- python - 在 VSCode 中跨环境自动化 PyTest
- symfony - SwiftMailer 附件 pdf 由 Dompdf 动态生成
- javascript - 如何在 AJax 调用的回调中连接到 signalR 服务器并将我的客户端方法注册到我的 signalR 服务器