【问题标题】:drawing a 45 degrees reference line as well as making x and y axis equal range绘制 45 度参考线以及使 x 和 y 轴等范围
【发布时间】: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


【解决方案1】:

这是你想要的吗?

import numpy as np
import matplotlib.pyplot as plt

# Generate random data.

x = np.random.random(120) * 0.21 + 0.24
y = np.random.random(120) * 1.4 - 0.2
ticks = np.arange( -0.2, 1.2, step=0.2 )

plt.scatter(x, y, cmap='viridis')
plt.xticks( ticks )
plt.yticks( ticks )
plt.plot( [-.2,1.2], [-.2,1.2], linestyle='--', color='k' )
plt.show()

输出:

【讨论】:

  • 新的plt.axline(),例如plt.axline((0,0), slope=1) 在这里可能也很有趣。这将画一条线,直到它触及边界。
  • 我不知道。 Matplotlib 几乎可以做任何事情,只要你能描述你想要什么......
【解决方案2】:

问题是在渲染图形时应用了 plt.axis('equal') 请求,这是在您调用 plt.xlim() 之后。我建议的解决方案如下,它同时使用 plt.xlim() 和 plt.ylim()。这样,您不必提前知道输入数据的范围。

import matplotlib.pyplot as plt
import numpy as np

rng = np.array([0.2, 1.5])
offset = np.array([0.25, -0.2])

xy = np.random.rand(30, 2) * rng + offset

plt.scatter(xy[:, 0], xy[:, 1], label="points")
plt.axis("equal")

left, right = plt.xlim()
bottom, top = plt.ylim()
axlim = (min(bottom, left), max(top, right))

# you can see the problem here: you've requested
# axes equal, but that has not yet been applied
# to the axes.
print(f"bottom={bottom}, top={top}, left={left}, right={right}")
print(f"Axis limits: {axlim}")

# better solution
xpoints2 = ypoints2 = np.linspace(axlim[0], axlim[1])
plt.plot(xpoints2, ypoints2, color="m", label="xlim and ylim")

# equivalent to the non-working solution
xpoints = ypoints = np.linspace(left, right)
plt.plot(xpoints, ypoints, color="g", label="xlim")

print(f"{left} {right}")
plt.grid()
plt.legend()
plt.show()

示例输出:

【讨论】:

  • 你能在答案中显示你的代码结果吗?
【解决方案3】:

您是否尝试过使用plt.xticks()plt.yticks(),然后传递以逗号分隔的轴限制值?

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 2015-10-17
  • 2014-07-10
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 2011-11-06
相关资源
最近更新 更多