Making better scatter plot: sort data points for scatter plots to plot first most frequent data

When plotting scatter plot, less frequent data are hidden by the most frequent ones, leading to very crowded scatter plot, reducing the readability of the plot:

%pylab inline
data = numpy.genfromtxt('test.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]
scatter(x,y,c=z)
colorbar()

scatter plot not sorted

The function below sort scatter data from the less frequent one to the most frequent using a 3D histogram:

data_sorted = sort_scatter_data(data)
x = data_sorted[:,0]
y = data_sorted[:,1]
z = data_sorted[:,2]
scatter(x,y,c=z)
colorbar()

The resulting graph shows a larger range of z-values depicted by the colorbar:

scatter plot sorted

If you want to ask me a question or leave me a message add @bougui505 in your comment.