pyplot散点图标记大小

在matplotlib的散点图中,标记大小`s`是以点为单位的面积。要改变标记的宽度或高度,需要按比例增加`s`的值。实际中,面积的平方根与视觉上的大小变化成正比。如果希望圆的大小线性增加,应根据面积调整`s`。例如,`s`值的平方根变化将导致圆的视觉大小线性变化。
摘要由CSDN通过智能技术生成

本文翻译自:pyplot scatter plot marker size

In the pyplot document for scatter plot: 在散点图的pyplot文档中:

matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None,
                          vmin=None, vmax=None, alpha=None, linewidths=None,
                          faceted=True, verts=None, hold=None, **kwargs)

The marker size 标记大小

s: size in points^2. s:以磅为单位的大小^ 2。 It is a scalar or an array of the same length as x and y. 它是标量或与x和y长度相同的数组。

What kind of unit is points^2 ? points^2是什么样的单位? What does it mean? 这是什么意思? Does s=100 mean 10 pixel x 10 pixel ? s=100表示10 pixel x 10 pixel吗?

Basically I'm trying to make scatter plots with different marker sizes, and I want to figure out what does the s number mean. 基本上,我正在尝试制作具有不同标记大小的散点图,并且我想弄清楚s含义是什么。


#1楼

参考:https://stackoom.com/question/10DLe/pyplot散点图标记大小


#2楼

This can be a somewhat confusing way of defining the size but you are basically specifying the area of the marker. 这种定义大小的方法可能有些混乱,但是您基本上是在指定标记的区域 This means, to double the width (or height) of the marker you need to increase s by a factor of 4. [because A = W H => (2W) (2H)=4A] 这意味着,要使标记的宽度(或高度)加倍,您需要将s增加4倍。[因为A = W H =>(2W) (2H)= 4A]

There is a reason, however, that the size of markers is defined in this way. 但是,以这种方式定义标记的大小是有原因的。 Because of the scaling of area as the square of width, doubling the width actually appears to increase the size by more than a factor 2 (in fact it increases it by a factor of 4). 由于将面积缩放为宽度的平方,所以实际上加倍宽度似乎会使大小增加2倍以上(实际上,将其增加4倍)。 To see this consider the following two examples and the output they produce. 要查看此内容,请考虑以下两个示例以及它们产生的输出。

# doubling the width of markers
x = [0,2,4,6,8,10]
y = [0]*len(x)
s = [20*4**n for n in range(len(x))]
plt.scatter(x,y,s=s)
plt.show()

gives

在此处输入图片说明

Notice how the size increases very quickly. 请注意大小如何快速增加。 If instead we have 如果相反,我们有

# doubling the area of markers
x = [0,2,4,6,8,10]
y = [0]*len(x)
s = [20*2**n for n in range(len(x))]
plt.scatter(x,y,s=s)
plt.show()

gives

在此处输入图片说明

Now the apparent size of the markers increases roughly linearly in an intuitive fashion. 现在,标记的外观大小以直观的方式大致线性增加。

As for the exact meaning of what a 'point' is, it is fairly arbitrary for plotting purposes, you can just scale all of your sizes by a constant until they look reasonable. 至于“点”的确切含义,出于绘图目的,它是任意的,您可以按常数缩放所有大小,直到看起来合理为止。

Hope this helps! 希望这可以帮助!

Edit: (In response to comment from @Emma) 编辑:(以回应@Emma的评论)

It's probably confusing wording on my part. 我的措辞可能令人困惑。 The question asked about doubling the width of a circle so in the first picture for each circle (as we move from left to right) it's width is double the previous one so for the area this is an exponential with base 4. Similarly the second example each circle has area double the last one which gives an exponential with base 2. 问题被问到是否将圆的宽度加倍,因此在每个圆的第一张图片中(当我们从左向右移动时)它的宽度是前一个圆的两倍,因此对于面积而言,这是底数为4的指数。每个圆的面积是最后一个圆的两倍,最后一个圆的底数为2。

However it is the second example (where we are scaling area) that doubling area appears to make the circle twice as big to the eye. 但是,这是第二个示例(我们正在缩放区域),该区域看起来加倍,使圆眼大两倍。 Thus if we want a circle to appear a factor of n bigger we would increase the area by a factor n not the radius so the apparent size scales linearly with the area. 因此,如果我们想让一个圆出现一个较大的n倍,我们将使面积增加n倍而不是半径,因此外观尺寸会随该面积线性变化。


#3楼

If the size of the circles corresponds to the square of the parameter in s=parameter , then assign a square root to each element you append to your size array, like this: s=[1, 1.414, 1.73, 2.0, 2.24] such that when it takes these values and returns them, their relative size increase will be the square root of the squared progression, which returns a linear progression. 如果圆的大小对应于s=parameter的平方,则将平方根分配给附加到大小数组的每个元素,如下所示: s=[1, 1.414, 1.73, 2.0, 2.24]这样当它们取这些值并返回它们时,它们的相对大小增加将是平方级数的平方根,并返回线性级数。

If I were to square each one as it gets output to the plot: output=[1, 2, 3, 4, 5] . 如果我将每个平方取平方,则将其输出到绘图中: output=[1, 2, 3, 4, 5] Try list interpretation: s=[numpy.sqrt(i) for i in s] 尝试列表解释: s=[numpy.sqrt(i) for i in s]


#4楼

It is the area of the marker. 它是标记的区域 I mean if you have s1 = 1000 and then s2 = 4000 , the relation between the radius of each circle is: r_s2 = 2 * r_s1 . 我的意思是,如果您有s1 = 1000 ,然后s2 = 4000 ,则每个圆的半径之间的关系为: r_s2 = 2 * r_s1 See the following plot: 请参见下图:

plt.scatter(2, 1, s=4000, c='r')
plt.scatter(2, 1, s=1000 ,c='b')
plt.scatter(2, 1, s=10, c='g')

在此处输入图片说明

I had the same doubt when I saw the post, so I did this example then I used a ruler on the screen to measure the radii. 看到帖子时,我也有同样的疑问,所以我做了这个例子,然后在屏幕上用尺子测量半径。


#5楼

You can use markersize to specify the size of the circle in plot method 您可以在plot方法中使用markersize指定圆的大小

import numpy as np
import matplotlib.pyplot as plt

x1 = np.random.randn(20)
x2 = np.random.randn(20)
plt.figure(1)
# you can specify the marker size two ways directly:
plt.plot(x1, 'bo', markersize=20)  # blue circle with size 10 
plt.plot(x2, 'ro', ms=10,)  # ms is just an alias for markersize
plt.show()

From here 这里

在此处输入图片说明


#6楼

I also attempted to use 'scatter' initially for this purpose. 我也尝试为此目的最初使用“分散”。 After quite a bit of wasted time - I settled on the following solution. 在浪费大量时间后-我决定采用以下解决方案。

import matplotlib.pyplot as plt
input_list = [{'x':100,'y':200,'radius':50, 'color':(0.1,0.2,0.3)}]    
output_list = []   
for point in input_list:
    output_list.append(plt.Circle((point['x'], point['y']), point['radius'], color=point['color'], fill=False))
ax = plt.gca(aspect='equal')
ax.cla()
ax.set_xlim((0, 1000))
ax.set_ylim((0, 1000))
for circle in output_list:    
   ax.add_artist(circle)

在此处输入图片说明

This is based on an answer to this question 这是基于对这个问题的答案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值