Build larger dots below and less on top

I am looking for a way to create a graph scatterin pythonwhere smaller graphs will be drawn above large ones to improve the “readability” of the figure (is there a similar image for the image?

Here's a simple MWE:

import numpy as np
import matplotlib.pyplot as plt


def random_data(N):
    # Generate some random data.
    return np.random.uniform(70., 250., N)

# Data lists.
N = 1000
x = random_data(N)
y = random_data(N)
z1 = random_data(N)
z2 = random_data(N)

cm = plt.cm.get_cmap('RdYlBu')
plt.scatter(x, y, s=z1, c=z2, cmap=cm)
plt.colorbar()

plt.show()

which produces:

enter image description here

I would like the smaller glasses to be drawn last so that they do not hide behind the large dots. How can i do this?

+4
source share
1 answer

Apply sortbefore building

order = np.argsort(-z1) # for desc
x = np.take(x, order)
y = np.take(y, order)
z1 = np.take(z1, order)
z2 = np.take(z2, order)

The indicator using is alphamore readable.

import numpy as np
import matplotlib.pyplot as plt


def random_data(N):
    # Generate some random data.
    return np.random.uniform(70., 250., N)

# Data lists.
N = 1000
x = random_data(N)
y = random_data(N)
z1 = random_data(N)
z2 = random_data(N)

order = np.argsort(-z1)
x = np.take(x, order)
y = np.take(y, order)
z1 = np.take(z1, order)
z2 = np.take(z2, order)

cm = plt.cm.get_cmap('RdYlBu')
plt.scatter(x, y, s=z1, c=z2, cmap=cm, alpha=0.7) # alpha can be 0 ~ 1
plt.colorbar()

plt.show()

Output signal

enter image description here

+6
source

Source: https://habr.com/ru/post/1538867/


All Articles