In this demo, I’ll draw a beautiful, elegant, gradient-colored (themed) scatter plot using some data for x and y axis. The neat thing I want to demo here is not so much the data themselves, but how to apply a “colormap” to the graph using Python’s matplotlib library. Specifically, its pyplot module.

**What’s Desired:**

The graph we want is shown below. For all data points, the color starts from light (for lower values) and goes darker (to the highest values). The code below will draw just this…

**The Code:**

The code itself is simple, we generate values for X axis, and values for Y axis, which is X^^2 (x-squared). Everything else is really the formatting using colormap.

import matplotlib.pyplot as plt x=[] y=[] for i in range(1,1000): x.append(i) y.append(i**2 ); # make squares of digits in x plt.scatter(x, y, c=y, cmap=plt.cm.Blues, edgecolor='none', s=40) # beautify the chart... plt.title("Color Map Example\nSquared Numbers", fontsize=12) plt.xlabel("X Values", fontsize=9) plt.ylabel("Squared", fontsize=9) plt.tick_params(axis='both', which='major', labelsize=8) # show the chart... plt.show()

**Explanation Of Code:**

We are using matplotlib library of Python and so we import it. Then we create two lists of numeric values: one for x axis, another for y of course as x[] and y[].

x[] values are generated run-time in a for…range loop containing 1 to 1000 inclusive; and load up the x[] list with each number in the loop. Also, in the same loop, we calculate y as x-squared, which is also the same number controlling our loop using i, and load up the y[] list. After 1000 loops, we have x[] and y[] all loaded up and ready to plot!

**Plotting:**

With .scatter() function, using plt object (which we imported in the first line), we can pass it some arguments to let it plot. Most important ones are x and y lists. We use cmap (Python syntax for colormap) and desired values using Blue theme and s for size. *See https://matplotlib.org/… for official reference.*

Everything else is just personal customization of font size, tick-marks, labels, etc. which are optional but useful.

**Conclusion:**

What’s the point of using cmap and all this? The colormap allows you to customize the theme as per the rest of your report! Having the gradient points shows dynamics! Such details matter in the real-world.

Have fun exploring the documentation as stated above and use different cmaps. Happy coding!