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

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

问题描述

我对此感到非常绝望,到目前为止我在 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,我需要知道图中中间有多少个点.

有人可以帮我吗?或者有另一种方法吗?提前致谢.

解决方案

I'm jump in from your other

在 (1, 1) 处指定半径为 0.5 的点将导致图中以 (1, 1) 为中心,边界穿过点 (1.5, 1), (1, 1.5),(0.5, 1) 和 (1, 0.5):

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

Here's the situation:

  • I am working with Python.
  • I have 3 arrays: the x-coordinates, the y-coordinates and the radius.
  • I want to create a scatter plot with the given x- and y-coordinates.

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

  • The circle size of each point in the scatter plot should be defined by the radius array.
  • The the values of the coordinates and the radius are in same units. More explicitly: Let's assume I have a point at (1, 1) with radius 0.5 assigned. Then I want to get 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)

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:

  • 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

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

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天全站免登陆