python散点图面积大小比例轴长度 [英] python scatter plot area size proportional axis length

查看:884
本文介绍了python散点图面积大小比例轴长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此感到非常绝望,到目前为止,我在www上找不到任何内容.

I'm getting quite desperate about this, I couldn't find anything on the www so far.

这里是情况:

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

到目前为止,一切都按照我的意愿进行.这是困扰我的事情:

So far, everything works how I want it to. Here is what's bothering me:

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

我正在努力寻找的是绘图点与轴的长度之比.我需要处理点,因为据我所知,散点图的圆圈大小以点值给出.因此,如果说我的轴从0变为10,我需要知道图中的点之间有多少点.

What I am struggling with is to find out the ratio of plot points to the length in an axis. I need to work with points because as far as I can see, the circle size of the scatter plot is given in point values. So if let's say my axis goes from 0 to 10, I need to know how many points there are in between in the plot.

有人可以帮助我吗?还是有其他方法可以做到这一点? 预先感谢.

Can anybody help me? Or is there another way of doing this? Thanks in advance.

推荐答案

我正在从您的其他对当前问题的答案提出的方法将无法完全按照您的意愿工作,原因如下:

I'm jumping in from your other stackoverflow question. I think the approach you presented as an answer to the present question won't work exactly as you want for the following reasons:

  • 首先,标记的大小以磅为单位,而不是以像素为单位.在排版中,该点是最小的度量单位,在matplotlib中对应于固定长度的1/72英寸.相反,像素的大小将随数字dpi和大小而变化.
  • 第二,plt.scatter中标记的大小与圆的直径有关,与半径无关.
  • First, the size of the markers is in points, not in pixels. In typography, the point is the smallest unit of measure and correspond in matplotlib to a fixed length of 1/72 inch. In contrast, the size of a pixel will vary following the figure dpi and size.
  • Second, the size of the markers in plt.scatter are related to the diameter of the circles, not the radius.

因此,每个标记的磅数应计算为:

So the size in points of each marker should be calculated as:

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

此外,如下面的MWE中所示,可以直接使用

Moreover, as shown in the MWE below, it is possible to calculate the size of the marker radius in pixels directly with matplotlib transformations, without having to generate an empty figure beforehand:

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):

A point at (1, 1) with radius 0.5 assigned will result in a circle in the plot centered at (1, 1) and with the border going throught the points (1.5, 1), (1, 1.5), (0.5, 1) and (1, 0.5):

这篇关于python散点图面积大小比例轴长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆