本文介绍了Matplotlib 的圆形标记的面积如何与其半径成比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

圆形标记的面积如何在 Matplotlib 中随标记半径缩放?我希望它按pi乘以半径平方的比例缩放,但事实并非如此.

我正在尝试创建一个图形来显示 N 个圆圈的紧密分布.这种分布恰好是规则的(它是六边形的),所以很容易知道每个圆的中心位置.我使用 matplotlib.pyplot.scatter()绘制这些图形,并使用 matplotlib/lib/matplotlib/markers.py 中的圆圈标记进行绘制.

现在为了将圆圈紧密排列,我需要设置圆形标记的区域,以便它们精确地相互接触.如果将标记区域设置为 numpy.pi *(L/2)** 2 ,其中 L 是每个圆的直径(以磅为单位),我希望会发生这种情况,如果它们要精确接触,则等于两个圆之间的距离.但这

从文档中:

s:标量或类似array_,形状(n,),可选,默认值:20大小,以磅为单位^ 2.

How does the area of the circular marker scale with the marker radius in Matplotlib? I would expect it to scale as pi times radius squared, but it does not.

I am trying to create a figure to show a closely packed distribution of N circles. This distribution happens to be regular (it is hexagonal) so it’s easy to know the locations of the centres of each of the circles. I plot these using matplotlib.pyplot.scatter(), using the circle marker from matplotlib/lib/matplotlib/markers.py for the circles.

Now in order to pack the circles closely, I need to set the area of the circular markers so that they precisely touch each other. I expect this to happen if I set the marker area to numpy.pi*(L/2)**2 where L is the diameter of each circle (in points), which is equal to the distance between two circles if they are to touch precisely. But this results in a plot in which the circles overlap. Here is the code that produces this plot:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

f = plt.figure(figsize=(7, 7), dpi=100)
ax = f.add_subplot(1,1,1)
ax.set_ylim(-105,105)
ax.set_xlim(-105,105)

L = 14.0 # Diameter of a circle in data units
# marker_radius is L/2 in points.
marker_radius = (ax.transData.transform((0,0))
                 -ax.transData.transform((L/2,0)))[0]
marker_area = np.pi*marker_radius**2
ax.scatter(x, y, color='#7fc97f', edgecolors='None', s=marker_area)

plt.savefig('figure.png',bbox_inches='tight')

Clearly the area of the circular marker in matplotlib/lib/matplotlib/markers.py does not scale as pi times radius squared (as it should). Upon trial and error, I found that it actually scales as roughly 2.3 times radius squared. When I set the marker_area to 2.3*marker_radius**2, I get a closely packed distribution as required.

I wonder if somebody could comment on why the circular marker size scales in this peculiar way. Also, what is the precise scaling? Is it really 2.3? Thanks!

解决方案

I quickly tried this code (changing only the marker from s to o), and from that it seems that the square root of the marker size equals the diameter (in points, see the post I referred to) of the circle:

From the documentation:

这篇关于Matplotlib 的圆形标记的面积如何与其半径成比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 19:38
查看更多