Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to plot a rainbow cricle in matplotlib?
To plot a rainbow circle in Matplotlib, we can create concentric circles with different colors representing the colors of a rainbow. This creates a beautiful visual effect with nested circles in rainbow colors.
Steps to Create a Rainbow Circle
- Set the figure size and adjust the padding between and around the subplots
- Create a figure and a set of subplots
- Set the X and Y axes to equal scale for perfect circles
- Define a list of rainbow colors
- Create concentric circles with decreasing radius
- Add each circle to the axes with different colors
- Display the figure using show() method
Example
Here's how to create a rainbow circle using concentric circles with rainbow colors ?
import matplotlib.pyplot as plt
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 5.50]
plt.rcParams["figure.autolayout"] = True
# Create figure and subplot
fig, ax = plt.subplots()
plt.axis("equal")
# Set axis limits
ax.set(xlim=(-10, 10), ylim=(-10, 10))
# Define rainbow colors (ROYGBIV)
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
# Create concentric circles
for i in range(7):
circle = plt.Circle((0, 0), 7-i, fill=True, color=rainbow[i])
ax.add_artist(circle)
# Remove axes for cleaner look
ax.set_xticks([])
ax.set_yticks([])
plt.show()
Output
The code will produce a rainbow circle with concentric rings in rainbow colors, starting from red on the outside to violet on the inside.
Creating a Rainbow Arc
You can also create a rainbow arc (semicircle) for a more traditional rainbow appearance ?
import matplotlib.pyplot as plt
import numpy as np
# Create figure
fig, ax = plt.subplots(figsize=(8, 4))
ax.set_aspect('equal')
# Rainbow colors
rainbow = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
# Create rainbow arcs
for i, color in enumerate(rainbow):
radius = 7 - i
theta = np.linspace(0, np.pi, 100)
x = radius * np.cos(theta)
y = radius * np.sin(theta)
ax.fill_between(x, 0, y, color=color, alpha=0.8)
# Set limits and remove axes
ax.set_xlim(-8, 8)
ax.set_ylim(0, 8)
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
plt.show()
Customizing Rainbow Circles
You can customize the rainbow circle by adjusting transparency, adding borders, or using different color schemes ?
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 8))
plt.axis("equal")
# Custom rainbow colors with hex codes
rainbow = ['#FF0000', '#FF7F00', '#FFFF00', '#00FF00', '#0000FF', '#4B0082', '#9400D3']
# Create circles with transparency and borders
for i in range(7):
circle = plt.Circle((0, 0), 7-i, fill=True, color=rainbow[i],
alpha=0.8, linewidth=2, edgecolor='white')
ax.add_artist(circle)
ax.set(xlim=(-8, 8), ylim=(-8, 8))
ax.set_facecolor('black') # Black background
ax.set_xticks([])
ax.set_yticks([])
plt.title('Rainbow Circle with Transparency', color='white', fontsize=16, pad=20)
plt.show()
Conclusion
Creating rainbow circles in Matplotlib involves using concentric plt.Circle() objects with decreasing radii and rainbow colors. You can customize the appearance with transparency, borders, and different color schemes to create stunning visual effects.
