简体   繁体   English

如何在 matplotlib 中制作不透明散射 plot

[英]How to make opaque scatter plot in matplotlib

When making a scatter plot in matplotlib, I found that when I change the size of the dots, they become transparent.在 matplotlib 中制作散点图 plot 时,我发现当我改变点的大小时,它们会变得透明。 How can I prevent this from happening?我怎样才能防止这种情况发生?

I've already tried alpha=1 , facecolor='k' but none of these seem to work我已经尝试过alpha=1facecolor='k'但这些似乎都不起作用

Example code示例代码

X = np.random.normal(0,1,5000)
Y = np.random.normal(0,1,5000)

fig, ax = plt.subplots(1,2, figsize=(6,3), dpi=288)

ax[0].scatter(X,Y, s=.01, c='k')
ax[1].scatter(X,Y, s=1, c='k')
plt.show()

Result:结果:

结果

Notice how the larger dots are opaque and filled in, whilst the smaller ones seem transparent, and have a white facecolor.注意较大的点是如何不透明并被填充的,而较小的点看起来是透明的,并且具有白色的面色。 How do I get the smaller ones to be filled in as well?我如何让较小的也被填写?

If you want the size to scale correctly, and print down to the pixel limit, the dpi and figure size need to be adjusted.如果您希望大小正确缩放,并打印到像素限制,则需要调整 dpi 和图形大小。 Then s will behave correctly, starting with the smallest pixel being s=1.然后 s 将正常运行,从 s=1 的最小像素开始。 You also probably want to change the marker from the default.您可能还想更改默认标记。 Try this:尝试这个:

#divide dpi by four, multiply figsize components by four to keep same area
fig, ax = plt.subplots(1,2, figsize=(24,12), dpi=72)

ax[0].scatter(X,Y, s=1, marker=".", c='k')
ax[1].scatter(X,Y, s=100, marker=".", c='k')

This will make the axes labels smaller, however.但是,这将使轴标签更小。

Edit: We can also ensure that the s-value is never sub-minimal-pixel size, as pointed out in comments.编辑:正如评论中所指出的,我们还可以确保 s 值永远不会是亚最小像素大小。 Here's a function that allows one to play around with these settings using this:这是一个 function 允许使用以下设置来玩转这些设置:

def scat_rand(size=(24,12), scale=72, smin=1, smax=100, sedge=True, min_pix=1):
    X = np.random.normal(0,1,5000)
    Y = np.random.normal(0,1,5000)
    fig, ax = plt.subplots(1,2, figsize=(size), dpi=scale)

    slimit = (min_pix * 72 / fig.dpi) ** 2
    if smin < slimit + .0001:
        smin = slimit
        print(f"smin too small, reset to {slimit}")

    if sedge:
        ax[0].scatter(X,Y, s=smin, c='k')
        ax[1].scatter(X,Y, s=smax, c='k')
    else:
        ax[0].scatter(X,Y, s=smin, c='k', edgecolor='none')
        ax[1].scatter(X,Y, s=smax, c='k', edgecolor='none')
    plt.suptitle(f"Pixel= {size[0] * scale} x {size[1] * scale}", fontsize=14) 
    plt.show()

Here's a little driver if you want to play around with it:如果你想玩它,这里有一个小驱动程序:

if __name__ == "__main__":
    temp = int(input("Enter size(1-24): "))
    size = (2 * temp, temp)
    scale = int(input("Enter dpi scale (72, 144, 288, 576, 1152): "))
    limit = (72 / scale) ** 2
    smin = float(input(f"Enter smallest point (limit = {limit}): "))
    smax = float(input("Enter largest point: "))
    edge = input("edgecolor (y/n): ")
    if edge == 'y':
        sedge = True
    else: 
        sedge = False
    scat_rand(size, scale, smin, smax, sedge)

See also: Relationship between dpi and figure size另请参阅: dpi 和图形大小之间的关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM