【发布时间】:2022-01-21 19:41:40
【问题描述】:
我正在尝试制作以下散点图:
对于 X 轴和 Y 轴范围相等以及在散点图上覆盖 45 度参考线的对象。 我有这段代码,但产生的情节不是我的预期。我该如何解决?
fig = plt.figure()
ax = fig.add_subplot(111)
#ax.set_aspect('equal', adjustable='box')
plt.scatter(actuals.cpu(), predictions.cpu(), cmap='viridis')
plt.xlabel('Ground Truth')
plt.ylabel('Predictions')
plt.axis('equal')
xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k', lw=3, scalex=False, scaley=False)
plt.savefig('predictions_actuals_scatterplot.png')
这是我的 Python 和 matplotlib 版本:
$ python
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'3.5.1'
如果我只使用以下代码,虽然原点与 45 度线相交,但仍然不是我想要的。
fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(actuals.cpu(), predictions.cpu(), cmap='viridis')
plt.xlabel('Ground Truth')
plt.ylabel('Predictions')
xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k', lw=3, scalex=False, scaley=False)
plt.savefig('predictions_actuals_scatterplot.png')
【问题讨论】:
-
您的期望是什么?你还没有告诉我们你想要什么。
-
@TimRoberts 我在标题中写下了我的期望,X 轴和 Y 轴的范围相等,并在散点图上覆盖 45 度参考线。
-
你有一个 45 度的参考线。您的 X 数据和 Y 数据的范围不相等。 X 范围非常非常窄。您当然可以手动设置坐标轴范围。
-
啊,我想我明白了。您实际上并不希望轴具有相等的范围。您希望数据填充绘图。您可能应该删除
plt.axis('equal'),让 matplotlib 确定范围。 -
请停止使用“等距”一词。这根本不是你想要的。您的 X 数据范围为 0.24 到 0.43。您的 Y 数据范围更大,从 -0.4 到 1.2。你想要的是填充情节。如果您向我们提供数据,我们会更容易提供帮助。
标签: python python-3.x matplotlib scatter-plot