Skip to content

Weird behaviour in scatter plot with small markersize and small alpha #4322

Closed

Description

@nordam

I was trying to get a feel for the look of a scatterplot with many small markers and low alpha. I used the following script to produce some tests:

import matplotlib
matplotlib.use('agg')
from matplotlib import pyplot as plt
import numpy as np

N     = int(5e5)
nrows = 4
ncols = 4
xmin  = -6
xmax  = 6
ymin  = xmin
ymax  = xmax

figs, axes = plt.subplots(nrows = nrows, ncols = ncols, figsize = (2*ncols, 2*nrows))

x = np.random.normal(loc = 0, scale = 0.5, size = N)
y = np.random.normal(loc = 0, scale = 0.5, size = N)

alpha = lambda i : 0.01 + 0.005*i
s     = lambda i : 0.01 + 0.005*i

for i in range(ncols):
    for j in range(nrows):
        axes[i,j].set_title('alpha = %s, s = %s' % (alpha(j), s(i)), size = 8)
        axes[i,j].scatter(x, y, marker = '.', edgecolor = '', color = 'k', lw = 0, alpha = alpha(j), s = s(i))
        axes[i,j].set_xlim((xmin, xmax))
        axes[i,j].set_ylim((ymin, ymax))
        axes[i,j].set_xticklabels([])
        axes[i,j].set_yticklabels([])

plt.suptitle('N = %s' % N)
plt.savefig('scattertest.png', dpi = 600)

First, I plotted 500.000 markers, and got this result, showing how the scatter plots look with different combinations of size and alpha:
scattertest_500000

Then, I thought it's weird that some of the combinations just don't show up at all, so I tried increasing the number of markers to 1.000.000. The only change I made in the script was to change 5e5 to 1e6, yet now the plot is empty.
scattertest_1000000_1

Then, I somewhat randomly commented out the line

plt.suptitle('N = %s' % N) 

and again made no other changes, and now the plot is no longer empty (but still, some combinations of s and alpha just don't show up).
scattertest_1000000_2

Is it expected that some combinations of small size and low alpha should lead to invisible markers?
Why does a seemingly irrelevant thing like a plot title change whether markers show up or not?

I'm using matplotlib version 1.4.2 (installed with pip), and python 3.4.2.

Activity

  1. mdboom commented on Apr 10, 2015

    @mdboom
    Member

    I think what you're seeing is an instance of #3371. The issue is that the image buffer only using 8-bits of alpha, and so really small values round down to zero after premultiplication. The solution is to allow an option to use floating-point image buffers. Agg is really close to having support for that (they currently claim they do) but it's broken and progress seems slow. I've spent some time looking into it, and unfortunately it's really hard to make heads or tails of the Agg code base.

  2. modified the milestones: , on Apr 12, 2015
  3. modified the milestones: , on Oct 3, 2017
  4. github-actions commented on Mar 11, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  5. chahak13 commented on Mar 15, 2023

    @chahak13
    Contributor

    Haven't dug deep but I don't think this is an issue anymore. The code plots this now

    image

    If someone who had more idea about the issue can confirm, maybe we can close this?

  6. QuLogic commented on Apr 12, 2023

    @QuLogic
    Member

    I can't reproduce on either main or v2.0.0, and I don't want to bisect any further back than that, but it seems fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions