Improved the algorithm for scatter_plot.py