# How to make a circular color gradient in Python?

A circular color gradient in Python is a visual representation of a continuous transition of colors in a circular shape. It is often used in data visualization to represent magnitude, direction, or other quantities that vary over a circular domain. The color gradient can be created by mapping a scalar value to a color on a color map, with the scalar value being a function of the position in the circular domain. The result is a smooth transition of colors that can be represented as an image, typically with a circular shape. The process of creating a circular color gradient in Python involves mapping scalar values to colors using a color map, generating a 2D matrix that represents the color gradient, and displaying the matrix using a library such as `matplotlib`.

## 6 Ways To Make A Circular Color Gradient In Python?

### 1. Using the `matplotlib` library – `imshow` function:

You can use the `matplotlib` library to create a circular color gradient in Python. Here’s an example that generates a circular color gradient and displays it using `matplotlib`:

````import numpy as np`
```
import matplotlib.pyplot as plt
return np.sin(3 * np.pi * x)
X, Y = np.meshgrid(np.linspace(0, 1, 256), np.linspace(0, 1, 256))
R = np.sqrt((X - 0.5)**2 + (Y - 0.5)**2)
plt.imshow(Z, cmap='inferno', origin='lower', extent=[0, 1, 0, 1])
plt.axis('equal')
plt.axis('off')
plt.show()``````

This code generates a 256×256 matrix `Z` that represents the circular color gradient, and displays it using the `imshow` function from `matplotlib`. The `gradient` function takes as input a scalar value `x` and returns the corresponding gradient value, in this case determined by the sine function. The `R` matrix is a measure of the distance from each point in the matrix to the center of the image. By combining `R` and the gradient function, you can generate a circular color gradient.

### 2. Using Matplotlib’s `pcolormesh` function:

`import numpy as np `
`import matplotlib.pyplot as plt `

`def gradient(x, y): `
`return np.sin(np.pi * np.sqrt(x**2 + y**2)) `

`X, Y = np.meshgrid(np.linspace(-1, 1, 256), np.linspace(-1, 1, 256)) `
`Z = gradient(X, Y) `

`plt.pcolormesh(X, Y, Z, cmap='inferno') `
`plt.axis('equal') `
`plt.axis('off') `
`plt.show() `

In this method, you define a gradient function that maps 2D coordinates to colors, and use it to generate a 2D matrix `Z` that represents the color gradient. Then, you display the matrix using the `pcolormesh` function from `matplotlib`.

### 3. Using Matplotlib’s `Circle` and `Rectangle` functions:

`import numpy as np `
`import matplotlib.pyplot as plt `
`from matplotlib.patches import Circle, Rectangle `

`def gradient(x, y, cx, cy, r): `
`return np.sin(np.pi * np.sqrt((x - cx)**2 + (y - cy)**2) / r) `

`X, Y = np.meshgrid(np.linspace(-1, 1, 256), np.linspace(-1, 1, 256)) `
`Z = gradient(X, Y, 0, 0, 1) `

`fig, ax = plt.subplots() `
`ax.imshow(Z, cmap='inferno', origin='lower', extent=[-1, 1, -1, 1])`
`circle = Circle((0, 0), 1, color='none', ec='k') `
`ax.add_patch(circle) `
`ax.set_xlim(-1, 1) `
`ax.set_ylim(-1, 1) `
`ax.axis('equal') `
`ax.axis`

### 4. Using Pyplot’s `scatter` function

`import numpy as np `
`import matplotlib.pyplot as plt `

`def gradient(x, y): `
`return np.sin(np.pi * np.sqrt(x**2 + y**2)) `

`N = 256 x = np.linspace(-1, 1, N) `
`y = np.linspace(-1, 1, N) `
`X, Y = np.meshgrid(x, y) `
`Z = gradient(X, Y) `

`fig, ax = plt.subplots() `
`ax.scatter(X, Y, c=Z, cmap='inferno', s=20) `
`ax.axis('equal') `
`ax.axis('off') `
`plt.show() `I

The `scatter` function from `matplotlib` to plot points at each location in the 2D grid, and color the points based on the value of the gradient function. The result is a smooth representation of the color gradient, with circular shape.

### 5. Using OpenCV

`import cv2 `
`import numpy as np `
`import matplotlib.pyplot as plt `

`def gradient(x, y, cx, cy, r): `
`return np.sin(np.pi * np.sqrt((x - cx)**2 + (y - cy)**2) / r) `

`N = 256 `
`x = np.linspace(-1, 1, N) `
`y = np.linspace(-1, 1, N) `
`X, Y = np.meshgrid(x, y) `
`Z = gradient(X, Y, 0, 0, 1) `

`image = (Z * 255).astype(np.uint8) `
`mask = np.zeros_like(image) `
`cv2.circle(mask, (N//2, N//2), N//2, (255, 255, 255), -1) `
`image = cv2.bitwise_and(image, mask) `

`plt.imshow(image, cmap='inferno') `
`plt.axis('equal') `
`plt.axis('off') `
`plt.show() `

Use the `cv2.circle` function from the OpenCV library to create a circular mask that is applied to the 2D grid representing the color gradient.

### 6. Using the `numpy` library:

`import numpy as np `
`import matplotlib.pyplot as plt `

`N = 256 `
`cx, cy = N//2, N//2 `
`r = N//2 `
`y, x = np.ogrid[-cy:N-cy, -cx:N-cx] `
`mask = x*x + y*y <= r*r `

`Z = np.sin(np.pi * np.sqrt(x**2 + y**2) / r) `
`Z = np.ma.array(Z, mask=np.logical_not(mask)) `

`plt.imshow(Z, cmap='inferno') `
`plt.axis('equal') `
`plt.axis('off') `
`plt.show() `

These are the ways to create a circular color gradient in Python. You can use any of these methods, or a combination of them, to generate the desired color gradient depending on your needs and preferences.