【问题标题】:python scatter plot area size proportional axis lengthpython散点图面积大小比例轴长度
【发布时间】:2016-07-27 06:55:44
【问题描述】:

我对此感到非常绝望,到目前为止我在 www 上找不到任何东西。

情况如下:

  • 我正在使用 Python。
  • 我有 3 个数组:x 坐标、y 坐标和半径。
  • 我想用给定的 x 和 y 坐标创建散点图。

到目前为止,一切都按照我的意愿进行。以下是困扰我的问题:

  • 散点图中每个点的圆大小应由半径数组定义。
  • 坐标值和半径值单位相同。更明确地说:假设我在 (1, 1) 有一个点,半径为 0.5。然后我想在图中画一个圆,以 (1, 1) 为中心,边界穿过点 (1.5, 1)、(1, 1.5)、(0.5, 1) 和 (1, 0.5)李>

我正在努力找出绘图点与轴长度的比率。我需要使用点,因为据我所知,散点图的圆大小以点值给出。因此,如果假设我的轴从 0 变为 10,我需要知道图中之间有多少个点。

有人可以帮我吗?还是有另一种方法可以做到这一点? 提前致谢。

【问题讨论】:

标签: python axis scatter-plot area scatter


【解决方案1】:

我从你的另一个stackoverflow question 跳进来。我认为您作为answer to the present question 提出的方法不会完全按照您的意愿工作,原因如下:

  • 首先,标记的大小以点为单位,而不是以像素为单位。在排版中,the point 是最小的度量单位,在 matplotlib 中对应于 1/72 英寸的固定长度。相比之下,像素的大小会随着图形的dpi和大小而变化。
  • 其次,plt.scatter中标记的大小与圆的直径有关,与半径无关。

所以每个标记点的大小应该计算为:

size_in_points = (2 * radius_in_pixels / fig_dpi * 72 points/inch)**2

另外,如下面的MWE所示,可以直接用matplotlib transformations计算标记半径的大小(以像素为单位),而不必事先生成一个空图:

import numpy as np
import matplotlib.pyplot as plt

plt.close('all')

# Generate some data :
N = 25
x = np.random.rand(N) + 0.5
y = np.random.rand(N) + 0.5
r = np.random.rand(N)/10

# Plot the data :
fig = plt.figure(facecolor='white', figsize=(7, 7))
ax = fig.add_subplot(111, aspect='equal')
ax.grid(True)
scat = ax.scatter(x, y, s=0, alpha=0.5, clip_on=False)
ax.axis([0, 2, 0, 2])

# Draw figure :
fig.canvas.draw()

# Calculate radius in pixels :
rr_pix = (ax.transData.transform(np.vstack([r, r]).T) -
          ax.transData.transform(np.vstack([np.zeros(N), np.zeros(N)]).T))
rpix, _ = rr_pix.T

# Calculate and update size in points:
size_pt = (2*rpix/fig.dpi*72)**2
scat.set_sizes(size_pt)

# Save and show figure:
fig.savefig('scatter_size_axes.png')
plt.show()

在 (1, 1) 处指定半径为 0.5 的点将在图中产生一个圆,该圆以 (1, 1) 为中心,边界穿过点 (1.5, 1)、(1, 1.5)、 (0.5, 1) 和 (1, 0.5):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2018-02-22
    • 2012-11-12
    • 2018-10-08
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多